Move route handlers into app module

This commit is contained in:
Matthew Gordon 2024-02-12 21:59:29 -04:00
parent 3402cb799d
commit d18ce42938
2 changed files with 25 additions and 20 deletions

20
src/app.rs Normal file
View File

@ -0,0 +1,20 @@
use {
crate::db::Database,
askama::Template,
axum::{extract::State, routing::get, Router},
};
pub fn routes() -> Router<Database> {
Router::new().route("/", get(root))
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate<'a> {
title: &'a str,
}
async fn root<'a>(State(database): State<Database>) -> IndexTemplate<'a> {
println!("Found {} users", database.log_num_users().await);
IndexTemplate { title: "LocalHub" }
}

View File

@ -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<Database>) -> IndexTemplate<'a> {
println!("Found {} users", database.log_num_users().await);
IndexTemplate { title: "LocalHub" }
}