diff options
Diffstat (limited to 'src/handlers/mod.rs')
| -rw-r--r-- | src/handlers/mod.rs | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 4a31909..34481e7 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,3 +1,6 @@ +use std::sync::Arc; +use tokio::sync::Mutex; + use axum::extract::DefaultBodyLimit; use axum::middleware::{self}; use axum::routing::{get, post}; @@ -6,7 +9,7 @@ use sqlx::SqlitePool; use tower_http::add_extension::AddExtensionLayer; use tower_http::services::ServeDir; -use admin::{admin_healthcheck, admin_middleware, upload_story}; +use admin::{admin_healthcheck, admin_middleware, generate_token, upload_story}; use frontend_builder::home; use self::frontend_builder::{build_page, DummyProduct}; @@ -19,11 +22,24 @@ struct ApiContext { pool: SqlitePool, } +#[derive(Clone)] +struct AppState { + admin_token: Arc<Mutex<String>>, +} + pub async fn router(pool: SqlitePool) -> Router { + let state = AppState { + admin_token: Arc::new(Mutex::new(generate_token())), + }; + Router::new() .route("/admin/story", post(upload_story)) .route("/admin", get(admin_healthcheck)) - .layer(middleware::from_fn(admin_middleware)) + .layer(middleware::from_fn_with_state( + state.clone(), + admin_middleware, + )) + .with_state(state) .route("/", get(home)) .route("/stories", get(build_page::<DummyProduct>)) .nest_service("/static", ServeDir::new("static")) |
