diff --git a/Cargo.toml b/Cargo.toml index 6e6bd61..8071f50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,8 @@ path = "src/main.rs" [dependencies] actix-web = "4" actix-http = "3" -futures-util = "0.3.30" -maxminddb = "0.24.0" +futures-util = "0.3" +maxminddb = "0.24" reqwest = { version = "0.11", features = ["stream"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/src/db_refresher.rs b/src/db_refresher.rs index 35fd80c..a4fcaa2 100644 --- a/src/db_refresher.rs +++ b/src/db_refresher.rs @@ -10,7 +10,7 @@ pub trait UpdatableDB: Send + Sync { ) -> impl Future>> + Send; } -pub fn start_db_update_daemon(data: web::Data, interval: u64) { +pub async fn start_db_update_daemon(data: web::Data, interval: u64) { let success_update_sleep = Duration::from_secs(interval); let failure_update_sleep = Duration::from_secs(5 * 60); @@ -29,5 +29,7 @@ pub fn start_db_update_daemon(data: web::Data, inter println!("Updater sleeping for {:?}", duration); sleep(duration).await; } - }); + }) + .await + .expect("DB Update daemon crashed"); } diff --git a/src/lib.rs b/src/lib.rs index 75ba4fd..e1a9fa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,21 +6,23 @@ pub mod models; pub mod network_utils; pub mod services; +use std::error::Error; + use actix_web::{web, App, HttpServer}; use maxmind_db::MaxmindDB; -use std::io::Result; use utoipa_swagger_ui::SwaggerUi; -pub async fn init_db(db_path: &str, db_variant: &str) -> web::Data { - let maxmind_db = MaxmindDB::init(db_variant, db_path) - .await - .expect("Failed to load database"); +pub async fn init_db( + db_path: &str, + db_variant: &str, +) -> Result, Box> { + let maxmind_db = MaxmindDB::init(db_variant, db_path).await?; - web::Data::new(maxmind_db) + Ok(web::Data::new(maxmind_db)) } -pub fn start_db_refresher(maxmind_db_arc: web::Data, update_interval: u64) { - db_refresher::start_db_update_daemon(maxmind_db_arc.clone(), update_interval) +pub async fn start_db_refresher(maxmind_db_arc: web::Data, update_interval: u64) { + db_refresher::start_db_update_daemon(maxmind_db_arc.clone(), update_interval).await; } pub async fn start_server( @@ -28,7 +30,7 @@ pub async fn start_server( host: &str, port: u16, swagger_ui_enabled: bool, -) -> Result<()> { +) { // Start HTTP Server HttpServer::new(move || { let reader_data = maxmind_db_arc.clone(); @@ -47,7 +49,9 @@ pub async fn start_server( app } }) - .bind((host, port))? + .bind((host, port)) + .expect("Cannot bind to specified host and port") .run() .await + .expect("HTTP Server crashed"); } diff --git a/src/main.rs b/src/main.rs index ac3ea7c..c71da49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,15 +32,25 @@ async fn main() -> Result<()> { match subcommand.as_deref() { Some("server") | None => { // Load or Initialize MaxMind database - let maxmind_db_arc = atlas_rs::init_db(&db_path, &db_variant).await; - // Start Database Updater Daemon - atlas_rs::start_db_refresher(maxmind_db_arc.clone(), update_interval); - // Start Server - atlas_rs::start_server(maxmind_db_arc, &host, port, swagger_ui_enabled).await + let maxmind_db_arc = atlas_rs::init_db(&db_path, &db_variant) + .await + .expect("Failed to load/initialize database"); + + tokio::select! { + // Start Database Updater Daemon + _ = atlas_rs::start_db_refresher(maxmind_db_arc.clone(), update_interval) => {} + // Start Server + _ = atlas_rs::start_server(maxmind_db_arc, &host, port, swagger_ui_enabled) => {} + } + + Ok(()) } Some("init") => { - let _db = atlas_rs::init_db(&db_path, &db_variant).await; - println!("Initiation was successful"); + match atlas_rs::init_db(&db_path, &db_variant).await { + Ok(_) => println!("Database initiation was successful"), + Err(reason) => print!("Failed to initialize database {reason:?}"), + } + Ok(()) } Some("spec") => { diff --git a/tests/lookup_service.rs b/tests/lookup_service.rs index 577ecd8..aedcb3b 100644 --- a/tests/lookup_service.rs +++ b/tests/lookup_service.rs @@ -11,7 +11,9 @@ async fn setup() -> ( >, Data, ) { - let app_data = atlas_rs::init_db("tests-data/", "GeoIP2-City-Test").await; + let app_data = atlas_rs::init_db("tests-data/", "GeoIP2-City-Test") + .await + .unwrap(); let service = test::init_service( App::new() .app_data(app_data.clone())