refactor: more lean main
This commit is contained in:
parent
55ad8a15f6
commit
eebf8b01a0
14
Cargo.lock
generated
14
Cargo.lock
generated
@ -1765,7 +1765,6 @@ dependencies = [
|
|||||||
"minijinja",
|
"minijinja",
|
||||||
"minijinja-autoreload",
|
"minijinja-autoreload",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"pulldown-cmark",
|
|
||||||
"rear",
|
"rear",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
"sea-orm",
|
"sea-orm",
|
||||||
@ -2243,16 +2242,23 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pulldown-cmark"
|
name = "pulldown-cmark"
|
||||||
version = "0.9.3"
|
version = "0.10.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "77a1a2f1f0a7ecff9c31abbe177637be0e97a0aef46cf8738ece09327985d998"
|
checksum = "dce76ce678ffc8e5675b22aa1405de0b7037e2fdf8913fea40d1926c6fe1e6e7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 1.3.2",
|
"bitflags 2.4.0",
|
||||||
"getopts",
|
"getopts",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
"pulldown-cmark-escape",
|
||||||
"unicase",
|
"unicase",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pulldown-cmark-escape"
|
||||||
|
version = "0.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d5d8f9aa0e3cbcfaf8bf00300004ee3b72f74770f9cbac93f6928771f613276b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.35"
|
version = "1.0.35"
|
||||||
|
@ -38,7 +38,6 @@ minijinja = { version = "1.0.11", features = [
|
|||||||
] }
|
] }
|
||||||
minijinja-autoreload = "1.0.8"
|
minijinja-autoreload = "1.0.8"
|
||||||
once_cell = "1.18.0"
|
once_cell = "1.18.0"
|
||||||
pulldown-cmark = "0.9.3"
|
|
||||||
rust-embed = { version = "8.0.0", features = [
|
rust-embed = { version = "8.0.0", features = [
|
||||||
"axum",
|
"axum",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
@ -18,7 +18,7 @@ minijinja = { version = "1.0.11", features = [
|
|||||||
"deserialization",
|
"deserialization",
|
||||||
] }
|
] }
|
||||||
minijinja-autoreload = "1.0.8"
|
minijinja-autoreload = "1.0.8"
|
||||||
pulldown-cmark = "0.9.3"
|
pulldown-cmark = "0.10"
|
||||||
serde = { version = "1.0.188", features = ["derive"] }
|
serde = { version = "1.0.188", features = ["derive"] }
|
||||||
tokio = { version = "1.32.0", features = ["full"] }
|
tokio = { version = "1.32.0", features = ["full"] }
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
|
@ -7,10 +7,10 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
pub trait AdminState {
|
pub trait AdminState {
|
||||||
fn get_templates(&self) -> &Templates;
|
fn get_templates(&self) -> &Templates;
|
||||||
fn get_registry(&self) -> SharedRegistry;
|
fn get_registry(&self) -> SharedAdminRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type SharedRegistry = Arc<AdminRegistry>;
|
pub type SharedAdminRegistry = Arc<AdminRegistry>;
|
||||||
|
|
||||||
// main registry.
|
// main registry.
|
||||||
pub struct AdminRegistry {
|
pub struct AdminRegistry {
|
||||||
|
7
src/db.rs
Normal file
7
src/db.rs
Normal file
@ -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))
|
||||||
|
}
|
19
src/main.rs
19
src/main.rs
@ -1,4 +1,5 @@
|
|||||||
mod admin_examples;
|
mod admin_examples;
|
||||||
|
mod db;
|
||||||
mod embed;
|
mod embed;
|
||||||
mod howto;
|
mod howto;
|
||||||
mod state;
|
mod state;
|
||||||
@ -35,12 +36,6 @@ async fn hello_world(templates: State<templates::Templates>) -> impl IntoRespons
|
|||||||
templates.render_html("hello_world.html", ())
|
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]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Load environment
|
// Load environment
|
||||||
@ -48,14 +43,18 @@ async fn main() {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
info!("Miniweb starting...");
|
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 tmpl = templates::Templates::initialize().expect("Template Engine could not be loaded.");
|
||||||
let mut admin = admin::state::AdminRegistry::new("admin");
|
let mut admin = admin::state::AdminRegistry::new("admin");
|
||||||
|
|
||||||
|
// Register Admin Apps
|
||||||
static_repository::register(&mut admin);
|
static_repository::register(&mut admin);
|
||||||
user_repository::register(&mut admin, db_connection);
|
user_repository::register(&mut admin, db_connection);
|
||||||
|
|
||||||
|
// Create global Application State.
|
||||||
let state: AppState = AppState {
|
let state: AppState = AppState {
|
||||||
templates: tmpl,
|
templates: tmpl,
|
||||||
admin: Arc::new(admin),
|
admin: Arc::new(admin),
|
||||||
@ -122,6 +121,7 @@ async fn main() {
|
|||||||
.unwrap_or("3000".to_string())
|
.unwrap_or("3000".to_string())
|
||||||
.parse()
|
.parse()
|
||||||
.expect("Port expected in APP_PORT");
|
.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
|
// 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));
|
let listen_addr = SocketAddr::from((app_host, app_port));
|
||||||
// the server addr is a concrete address the user can connect to.
|
// the server addr is a concrete address the user can connect to.
|
||||||
@ -130,11 +130,14 @@ async fn main() {
|
|||||||
} else {
|
} else {
|
||||||
listen_addr.clone()
|
listen_addr.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("listening on {}", listen_addr);
|
info!("listening on {}", listen_addr);
|
||||||
info!("admin on: http://{}/admin", server_addr);
|
info!("admin on: http://{}/admin", server_addr);
|
||||||
|
|
||||||
let listener = TcpListener::bind(&listen_addr)
|
let listener = TcpListener::bind(&listen_addr)
|
||||||
.await
|
.await
|
||||||
.expect("Could not bind TCP Listener.");
|
.expect("Could not bind TCP Listener.");
|
||||||
|
|
||||||
axum::serve(listener, app.into_make_service())
|
axum::serve(listener, app.into_make_service())
|
||||||
.with_graceful_shutdown(shutdown_signal())
|
.with_graceful_shutdown(shutdown_signal())
|
||||||
.await
|
.await
|
||||||
|
12
src/state.rs
12
src/state.rs
@ -1,12 +1,12 @@
|
|||||||
use axum::extract::FromRef;
|
use axum::extract::FromRef;
|
||||||
|
|
||||||
use rear::admin::state::{AdminState, SharedRegistry};
|
use rear::admin::state::{AdminState, SharedAdminRegistry};
|
||||||
use rear::service::templates;
|
use rear::service::templates;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub templates: templates::Templates,
|
pub templates: templates::Templates,
|
||||||
pub admin: SharedRegistry,
|
pub admin: SharedAdminRegistry,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for templates::Templates {
|
impl FromRef<AppState> for templates::Templates {
|
||||||
@ -15,18 +15,12 @@ impl FromRef<AppState> for templates::Templates {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromRef<AppState> for SharedRegistry {
|
|
||||||
fn from_ref(app_state: &AppState) -> SharedRegistry {
|
|
||||||
app_state.admin.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AdminState for AppState {
|
impl AdminState for AppState {
|
||||||
fn get_templates(&self) -> &templates::Templates {
|
fn get_templates(&self) -> &templates::Templates {
|
||||||
&self.templates
|
&self.templates
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_registry(&self) -> SharedRegistry {
|
fn get_registry(&self) -> SharedAdminRegistry {
|
||||||
self.admin.clone()
|
self.admin.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user