diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..83dc378 --- /dev/null +++ b/src/app.rs @@ -0,0 +1,20 @@ +use { + crate::db::Database, + askama::Template, + axum::{extract::State, routing::get, Router}, +}; + +pub fn routes() -> Router { + Router::new().route("/", get(root)) +} + +#[derive(Template)] +#[template(path = "index.html")] +struct IndexTemplate<'a> { + title: &'a str, +} + +async fn root<'a>(State(database): State) -> IndexTemplate<'a> { + println!("Found {} users", database.log_num_users().await); + IndexTemplate { title: "LocalHub" } +} diff --git a/src/main.rs b/src/main.rs index 748d7f9..074fa66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,6 @@ -use { - askama::Template, - axum::{extract::State, routing::get, Router}, - thiserror::Error, - tower_http::services::ServeDir, -}; +use {thiserror::Error, tower_http::services::ServeDir}; +mod app; mod config; mod db; @@ -29,7 +25,8 @@ fn main() { Ok(()) => 0, Err(err) => { eprintln!("ERROR: {}", err); - 1}, + 1 + } }) } @@ -38,8 +35,7 @@ async fn localhub_main() -> Result<(), Error> { let db_pool = Database::create_pool(&config.database_url, 2)?; - let app = Router::new() - .route("/", get(root)) + let app = app::routes() .with_state(db_pool) .nest_service("/static", ServeDir::new(&config.static_file_path)); @@ -47,14 +43,3 @@ async fn localhub_main() -> Result<(), Error> { axum::serve(listener, app).await?; Ok(()) } - -#[derive(Template)] -#[template(path = "index.html")] -struct IndexTemplate<'a> { - title: &'a str, -} - -async fn root<'a>(State(database): State) -> IndexTemplate<'a> { - println!("Found {} users", database.log_num_users().await); - IndexTemplate { title: "LocalHub" } -}