From a246b203c60c2536c4db1dd131999209cce129f9 Mon Sep 17 00:00:00 2001 From: eraden Date: Sun, 31 Dec 2023 06:01:38 +0100 Subject: [PATCH] Fix migration --- bins/identity-agent/src/main.rs | 8 +++++--- bins/sessions-agent/src/main.rs | 3 +-- crates/agent/Cargo.toml | 1 + crates/database/src/lib.rs | 16 ++++++++-------- crates/web/src/lib.rs | 29 ++++++++++++++++++++++++++++- crates/zt/src/new_cmd.rs | 1 - 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/bins/identity-agent/src/main.rs b/bins/identity-agent/src/main.rs index 6da8f22..fb21034 100644 --- a/bins/identity-agent/src/main.rs +++ b/bins/identity-agent/src/main.rs @@ -4,7 +4,9 @@ fn main() { let event_bus = events::Bus::new( "identity-agent", std::env::var("EVENT_BUS_BIND").ok().as_deref(), - std::env::var("EVENT_BUS_PORT").ok().and_then(|s| s.parse().ok()), - move |topic, event| async { - }); + std::env::var("EVENT_BUS_PORT") + .ok() + .and_then(|s| s.parse().ok()), + move |topic, event| async {}, + ); } diff --git a/bins/sessions-agent/src/main.rs b/bins/sessions-agent/src/main.rs index ce4ffdc..5d34394 100644 --- a/bins/sessions-agent/src/main.rs +++ b/bins/sessions-agent/src/main.rs @@ -1,3 +1,2 @@ #[actix::main] -async fn main() { -} +async fn main() {} diff --git a/crates/agent/Cargo.toml b/crates/agent/Cargo.toml index ab62035..a6afa6c 100644 --- a/crates/agent/Cargo.toml +++ b/crates/agent/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [features] +default = ["with-events", "with-database", "with-web"] with-web = ["web"] with-database = ["database"] with-events = ["events"] diff --git a/crates/database/src/lib.rs b/crates/database/src/lib.rs index 1d1a6e5..e92ac32 100644 --- a/crates/database/src/lib.rs +++ b/crates/database/src/lib.rs @@ -4,6 +4,7 @@ pub use sea_orm_migration; pub use chrono; pub use sea_orm::prelude::*; pub use uuid; +pub use sea_orm::DatabaseConnection; fn sqlite_file_path() -> String { let file = std::env::current_dir() @@ -25,15 +26,14 @@ pub trait DatabaseUrl { fn provided_url(&self) -> Option<&String>; } -pub async fn run_migration( +pub async fn run_migration( opts: &impl DatabaseUrl, -) { - let _connection = db_connect(&opts.database_url()) - .await - .expect("Failed to connect to database"); - // Migrator ::default().up(&connection) - // .await - // .expect("Failed to run migration"); +) where Migrator: Default + sea_orm_migration::MigratorTrait { + let connection = db_connect(opts) + .await; + Migrator ::up(&connection, None) + .await + .expect("Failed to run migration"); } pub async fn db_connect(opts: &impl DatabaseUrl) -> DatabaseConnection { diff --git a/crates/web/src/lib.rs b/crates/web/src/lib.rs index 0e673e4..083dc68 100644 --- a/crates/web/src/lib.rs +++ b/crates/web/src/lib.rs @@ -1,4 +1,31 @@ -pub struct Web {} +pub struct Web; impl Web { + pub fn new(bind: &'static str, port: u16, configure: Config) -> Self + where + Config: Fn(&mut actix_web::web::ServiceConfig) + Clone + Send + 'static, + { + tokio::spawn(async move { + start_server(bind, port, configure.clone()).await; + }); + Self + } +} + +async fn start_server(bind: &'static str, port: u16, configure: Config) +where + Config: Fn(&mut actix_web::web::ServiceConfig) + Clone + Send + 'static, +{ + use actix_web::{App, HttpServer}; + + let config = configure.clone(); + HttpServer::new(move || { + let config = config.clone(); + App::new().configure(config) + }) + .bind((bind, port)) + .unwrap_or_else(|e| panic!("Failed to bind server at {bind}:{port} : {e}")) + .run() + .await + .expect("Failed to run web server"); } diff --git a/crates/zt/src/new_cmd.rs b/crates/zt/src/new_cmd.rs index 714f792..d199065 100644 --- a/crates/zt/src/new_cmd.rs +++ b/crates/zt/src/new_cmd.rs @@ -131,7 +131,6 @@ impl RunCmd for NewCmd { } } - #[derive(Debug, Options)] pub struct NewOpts { help: bool,