aurcache/backend/src/main.rs

122 lines
3.8 KiB
Rust
Raw Normal View History

2023-12-23 19:27:36 +01:00
mod api;
mod aur;
mod builder;
mod db;
2024-02-15 20:33:42 +01:00
mod package;
2023-12-23 19:27:36 +01:00
mod pkgbuild;
mod repo;
mod scheduler;
mod utils;
2023-12-23 19:27:36 +01:00
use crate::api::backend;
2023-12-29 18:13:51 +01:00
#[cfg(feature = "static")]
use crate::api::embed::CustomHandler;
2023-12-23 19:27:36 +01:00
use crate::builder::types::Action;
use crate::db::migration::Migrator;
use crate::scheduler::aur_version_update::start_aur_version_checking;
2024-02-15 22:17:39 +01:00
use flate2::read::GzEncoder;
use flate2::Compression;
2023-12-23 19:27:36 +01:00
use rocket::config::Config;
use rocket::fs::FileServer;
2023-12-23 19:27:36 +01:00
use rocket::futures::future::join_all;
use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig};
use sea_orm::{Database, DatabaseConnection};
use sea_orm_migration::MigratorTrait;
use std::fs;
2024-02-15 22:17:39 +01:00
use std::fs::File;
use tokio::fs::symlink;
2023-12-23 19:27:36 +01:00
use tokio::sync::broadcast;
fn main() {
let t = tokio::runtime::Runtime::new().unwrap();
let (tx, _) = broadcast::channel::<Action>(32);
t.block_on(async move {
// create folder for db stuff
if !fs::metadata("./db").is_ok() {
fs::create_dir("./db").unwrap();
}
2023-12-23 19:27:36 +01:00
let db: DatabaseConnection = Database::connect("sqlite://db/db.sqlite?mode=rwc")
2023-12-23 19:27:36 +01:00
.await
.unwrap();
Migrator::up(&db, None).await.unwrap();
// create repo folder
2023-12-23 19:27:36 +01:00
if !fs::metadata("./repo").is_ok() {
fs::create_dir("./repo").unwrap();
2024-02-15 22:17:39 +01:00
let tar_gz = File::create("./repo/repo.db.tar.gz").unwrap();
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.finish().expect("failed to create repo archive");
symlink("repo.db.tar.gz", "./repo/repo.db")
2024-02-15 22:17:39 +01:00
.await
.expect("failed to create repo symlink");
let tar_gz = File::create("./repo/repo.files.tar.gz").unwrap();
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.finish().expect("failed to create repo archive");
symlink("repo.files.tar.gz", "./repo/repo.files")
2024-02-15 22:17:39 +01:00
.await
.expect("failed to create repo symlink");
2023-12-23 19:27:36 +01:00
}
let db2 = db.clone();
let tx2 = tx.clone();
tokio::spawn(async move {
builder::builder::init(db2, tx2).await;
});
start_aur_version_checking(db.clone());
2023-12-23 19:27:36 +01:00
let backend_handle = tokio::spawn(async {
let mut config = Config::default();
config.address = "0.0.0.0".parse().unwrap();
config.port = 8081;
let rock = rocket::custom(config)
2023-12-23 19:27:36 +01:00
.manage(db)
.manage(tx)
2023-12-30 16:46:13 +01:00
.mount("/api/", backend::build_api())
2023-12-23 19:27:36 +01:00
.mount(
"/docs/",
make_swagger_ui(&SwaggerUIConfig {
2023-12-30 16:46:13 +01:00
url: "../api/openapi.json".to_owned(),
2023-12-23 19:27:36 +01:00
..Default::default()
}),
);
#[cfg(feature = "static")]
let rock = rock.mount("/", CustomHandler {});
2023-12-29 18:13:51 +01:00
let rock = rock.launch().await;
match rock {
2023-12-23 19:27:36 +01:00
Ok(_) => println!("Rocket shut down gracefully."),
Err(err) => println!("Rocket had an error: {}", err),
};
});
let repo_handle = tokio::spawn(async {
let mut config = Config::default();
config.address = "0.0.0.0".parse().unwrap();
config.port = 8080;
let launch_result = rocket::custom(config)
.mount("/", FileServer::from("./repo"))
2023-12-23 19:27:36 +01:00
.launch()
.await;
match launch_result {
Ok(_) => println!("Rocket shut down gracefully."),
Err(err) => println!("Rocket had an error: {}", err),
};
});
join_all([repo_handle, backend_handle]).await;
});
return;
}