diff options
Diffstat (limited to '')
| -rw-r--r-- | src/routes/mod.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/routes/mod.rs b/src/routes/mod.rs new file mode 100644 index 0000000..1cb0e54 --- /dev/null +++ b/src/routes/mod.rs | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | pub mod accounts; | ||
| 2 | pub mod backups; | ||
| 3 | pub mod dashboard; | ||
| 4 | pub mod instances; | ||
| 5 | pub mod players; | ||
| 6 | |||
| 7 | use askama::Template; | ||
| 8 | use axum::{Router, response::Html}; | ||
| 9 | use tower_http::services::ServeDir; | ||
| 10 | |||
| 11 | use crate::auth::CurrentUser; | ||
| 12 | use crate::config::Config; | ||
| 13 | use crate::db::Database; | ||
| 14 | use crate::docker::DockerManager; | ||
| 15 | |||
| 16 | #[derive(Clone)] | ||
| 17 | pub struct AppState { | ||
| 18 | pub config: Config, | ||
| 19 | pub db: Database, | ||
| 20 | pub docker: DockerManager, | ||
| 21 | } | ||
| 22 | |||
| 23 | pub fn build_router(state: AppState) -> Router { | ||
| 24 | let auth_required = | ||
| 25 | axum::middleware::from_fn_with_state(state.clone(), crate::auth::require_auth); | ||
| 26 | |||
| 27 | let protected = Router::new() | ||
| 28 | .route("/change-password", axum::routing::get(change_password_page)) | ||
| 29 | .route( | ||
| 30 | "/change-password", | ||
| 31 | axum::routing::post(accounts::change_password), | ||
| 32 | ) | ||
| 33 | .merge(dashboard::router()) | ||
| 34 | .merge(instances::router()) | ||
| 35 | .merge(players::router()) | ||
| 36 | .merge(accounts::router()) | ||
| 37 | .merge(backups::router()) | ||
| 38 | .layer(auth_required); | ||
| 39 | |||
| 40 | Router::new() | ||
| 41 | .route("/login", axum::routing::get(login_page)) | ||
| 42 | .route("/login", axum::routing::post(crate::auth::authenticate)) | ||
| 43 | .route("/logout", axum::routing::post(crate::auth::logout)) | ||
| 44 | .merge(protected) | ||
| 45 | .nest_service("/static", ServeDir::new("static")) | ||
| 46 | .with_state(state) | ||
| 47 | } | ||
| 48 | |||
| 49 | #[derive(Template)] | ||
| 50 | #[template(path = "login.html")] | ||
| 51 | struct LoginPageTemplate; | ||
| 52 | |||
| 53 | async fn login_page() -> Html<String> { | ||
| 54 | let template = LoginPageTemplate; | ||
| 55 | Html(template.render().unwrap_or_default()) | ||
| 56 | } | ||
| 57 | |||
| 58 | #[derive(Template)] | ||
| 59 | #[template(path = "change_password.html")] | ||
| 60 | struct ChangePasswordPageTemplate { | ||
| 61 | username: String, | ||
| 62 | is_superuser: bool, | ||
| 63 | password_change_required: bool, | ||
| 64 | error: String, | ||
| 65 | } | ||
| 66 | |||
| 67 | async fn change_password_page(CurrentUser(account): CurrentUser) -> Html<String> { | ||
| 68 | let template = ChangePasswordPageTemplate { | ||
| 69 | username: account.username, | ||
| 70 | is_superuser: account.is_superuser, | ||
| 71 | password_change_required: account.password_change_required, | ||
| 72 | error: String::new(), | ||
| 73 | }; | ||
| 74 | Html(template.render().unwrap_or_default()) | ||
| 75 | } | ||
