From eebf8b01a0d876b744cd0d6278372bd18a448dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Wed, 28 Feb 2024 16:10:41 +0100 Subject: [PATCH] refactor: more lean main --- Cargo.lock | 14 ++++++++++---- Cargo.toml | 1 - rear/Cargo.toml | 2 +- rear/src/admin/state.rs | 4 ++-- src/db.rs | 7 +++++++ src/main.rs | 19 +++++++++++-------- src/state.rs | 12 +++--------- 7 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 src/db.rs diff --git a/Cargo.lock b/Cargo.lock index 8ed30e1..4570051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1765,7 +1765,6 @@ dependencies = [ "minijinja", "minijinja-autoreload", "once_cell", - "pulldown-cmark", "rear", "rust-embed", "sea-orm", @@ -2243,16 +2242,23 @@ dependencies = [ [[package]] name = "pulldown-cmark" -version = "0.9.3" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a1a2f1f0a7ecff9c31abbe177637be0e97a0aef46cf8738ece09327985d998" +checksum = "dce76ce678ffc8e5675b22aa1405de0b7037e2fdf8913fea40d1926c6fe1e6e7" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.4.0", "getopts", "memchr", + "pulldown-cmark-escape", "unicase", ] +[[package]] +name = "pulldown-cmark-escape" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5d8f9aa0e3cbcfaf8bf00300004ee3b72f74770f9cbac93f6928771f613276b" + [[package]] name = "quote" version = "1.0.35" diff --git a/Cargo.toml b/Cargo.toml index e8b8cfc..311ff1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,6 @@ minijinja = { version = "1.0.11", features = [ ] } minijinja-autoreload = "1.0.8" once_cell = "1.18.0" -pulldown-cmark = "0.9.3" rust-embed = { version = "8.0.0", features = [ "axum", "tokio", diff --git a/rear/Cargo.toml b/rear/Cargo.toml index 32c0dc2..4a9adff 100644 --- a/rear/Cargo.toml +++ b/rear/Cargo.toml @@ -18,7 +18,7 @@ minijinja = { version = "1.0.11", features = [ "deserialization", ] } minijinja-autoreload = "1.0.8" -pulldown-cmark = "0.9.3" +pulldown-cmark = "0.10" serde = { version = "1.0.188", features = ["derive"] } tokio = { version = "1.32.0", features = ["full"] } log = "0.4.20" diff --git a/rear/src/admin/state.rs b/rear/src/admin/state.rs index 2306219..5464eb7 100644 --- a/rear/src/admin/state.rs +++ b/rear/src/admin/state.rs @@ -7,10 +7,10 @@ use std::sync::Arc; pub trait AdminState { fn get_templates(&self) -> &Templates; - fn get_registry(&self) -> SharedRegistry; + fn get_registry(&self) -> SharedAdminRegistry; } -pub type SharedRegistry = Arc; +pub type SharedAdminRegistry = Arc; // main registry. pub struct AdminRegistry { diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..3fd5b6d --- /dev/null +++ b/src/db.rs @@ -0,0 +1,7 @@ +use std::env; + +pub async fn establish_connection() -> sea_orm::DatabaseConnection { + let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); + let db = sea_orm::Database::connect(&database_url).await; + db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) +} diff --git a/src/main.rs b/src/main.rs index bf6fad5..b9ad04f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod admin_examples; +mod db; mod embed; mod howto; mod state; @@ -35,12 +36,6 @@ async fn hello_world(templates: State) -> impl IntoRespons templates.render_html("hello_world.html", ()) } -pub async fn establish_connection() -> sea_orm::DatabaseConnection { - let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); - let db = sea_orm::Database::connect(&database_url).await; - db.unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) -} - #[tokio::main] async fn main() { // Load environment @@ -48,14 +43,18 @@ async fn main() { env_logger::init(); info!("Miniweb starting..."); - let db_connection = establish_connection().await; + // Database Configuration. + let db_connection = db::establish_connection().await; - // Prepare App State + // Prepare Application State Members let tmpl = templates::Templates::initialize().expect("Template Engine could not be loaded."); let mut admin = admin::state::AdminRegistry::new("admin"); + + // Register Admin Apps static_repository::register(&mut admin); user_repository::register(&mut admin, db_connection); + // Create global Application State. let state: AppState = AppState { templates: tmpl, admin: Arc::new(admin), @@ -122,6 +121,7 @@ async fn main() { .unwrap_or("3000".to_string()) .parse() .expect("Port expected in APP_PORT"); + // the listen_addr is the address we bind to. This might be multiple domains, like 0.0.0.0 let listen_addr = SocketAddr::from((app_host, app_port)); // the server addr is a concrete address the user can connect to. @@ -130,11 +130,14 @@ async fn main() { } else { listen_addr.clone() }; + info!("listening on {}", listen_addr); info!("admin on: http://{}/admin", server_addr); + let listener = TcpListener::bind(&listen_addr) .await .expect("Could not bind TCP Listener."); + axum::serve(listener, app.into_make_service()) .with_graceful_shutdown(shutdown_signal()) .await diff --git a/src/state.rs b/src/state.rs index 4b2d1df..40783db 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,12 +1,12 @@ use axum::extract::FromRef; -use rear::admin::state::{AdminState, SharedRegistry}; +use rear::admin::state::{AdminState, SharedAdminRegistry}; use rear::service::templates; #[derive(Clone)] pub struct AppState { pub templates: templates::Templates, - pub admin: SharedRegistry, + pub admin: SharedAdminRegistry, } impl FromRef for templates::Templates { @@ -15,18 +15,12 @@ impl FromRef for templates::Templates { } } -impl FromRef for SharedRegistry { - fn from_ref(app_state: &AppState) -> SharedRegistry { - app_state.admin.clone() - } -} - impl AdminState for AppState { fn get_templates(&self) -> &templates::Templates { &self.templates } - fn get_registry(&self) -> SharedRegistry { + fn get_registry(&self) -> SharedAdminRegistry { self.admin.clone() } }