Move route handlers into app module
This commit is contained in:
parent
3402cb799d
commit
d18ce42938
|
|
@ -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" }
|
||||||
|
}
|
||||||
25
src/main.rs
25
src/main.rs
|
|
@ -1,10 +1,6 @@
|
||||||
use {
|
use {thiserror::Error, tower_http::services::ServeDir};
|
||||||
askama::Template,
|
|
||||||
axum::{extract::State, routing::get, Router},
|
|
||||||
thiserror::Error,
|
|
||||||
tower_http::services::ServeDir,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
mod app;
|
||||||
mod config;
|
mod config;
|
||||||
mod db;
|
mod db;
|
||||||
|
|
||||||
|
|
@ -29,7 +25,8 @@ fn main() {
|
||||||
Ok(()) => 0,
|
Ok(()) => 0,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("ERROR: {}", 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 db_pool = Database::create_pool(&config.database_url, 2)?;
|
||||||
|
|
||||||
let app = Router::new()
|
let app = app::routes()
|
||||||
.route("/", get(root))
|
|
||||||
.with_state(db_pool)
|
.with_state(db_pool)
|
||||||
.nest_service("/static", ServeDir::new(&config.static_file_path));
|
.nest_service("/static", ServeDir::new(&config.static_file_path));
|
||||||
|
|
||||||
|
|
@ -47,14 +43,3 @@ async fn localhub_main() -> Result<(), Error> {
|
||||||
axum::serve(listener, app).await?;
|
axum::serve(listener, app).await?;
|
||||||
Ok(())
|
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" }
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue