refactor: more lean main

This commit is contained in:
2024-02-28 16:10:41 +01:00
parent 55ad8a15f6
commit eebf8b01a0
7 changed files with 34 additions and 25 deletions

7
src/db.rs Normal file
View 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))
}

View File

@@ -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<templates::Templates>) -> 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

View File

@@ -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<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 {
fn get_templates(&self) -> &templates::Templates {
&self.templates
}
fn get_registry(&self) -> SharedRegistry {
fn get_registry(&self) -> SharedAdminRegistry {
self.admin.clone()
}
}