Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join DB refresher future with server #9

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions src/db_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait UpdatableDB: Send + Sync {
) -> impl Future<Output = Result<(), Box<dyn Error>>> + Send;
}

pub fn start_db_update_daemon(data: web::Data<impl UpdatableDB + 'static>, interval: u64) {
pub async fn start_db_update_daemon(data: web::Data<impl UpdatableDB + 'static>, interval: u64) {
let success_update_sleep = Duration::from_secs(interval);
let failure_update_sleep = Duration::from_secs(5 * 60);

Expand All @@ -29,5 +29,7 @@ pub fn start_db_update_daemon(data: web::Data<impl UpdatableDB + 'static>, inter
println!("Updater sleeping for {:?}", duration);
sleep(duration).await;
}
});
})
.await
.expect("DB Update daemon crashed");
}
24 changes: 14 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@ 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<MaxmindDB> {
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<web::Data<MaxmindDB>, Box<dyn Error>> {
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<MaxmindDB>, 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<MaxmindDB>, update_interval: u64) {
db_refresher::start_db_update_daemon(maxmind_db_arc.clone(), update_interval).await;
}

pub async fn start_server(
maxmind_db_arc: web::Data<MaxmindDB>,
host: &str,
port: u16,
swagger_ui_enabled: bool,
) -> Result<()> {
) {
// Start HTTP Server
HttpServer::new(move || {
let reader_data = maxmind_db_arc.clone();
Expand All @@ -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");
}
24 changes: 17 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") => {
Expand Down
4 changes: 3 additions & 1 deletion tests/lookup_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async fn setup() -> (
>,
Data<MaxmindDB>,
) {
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())
Expand Down