summaryrefslogtreecommitdiff
path: root/src/handlers/mod.rs
diff options
context:
space:
mode:
authorWriterV2 <primary-v2@protonmail.com>2024-11-03 16:36:25 +0100
committerWriterV2 <primary-v2@protonmail.com>2024-11-03 16:36:25 +0100
commiteb326116f4abc6b12ec81d06586d7cf08755538c (patch)
tree1ed04c8c4947dfcccba054aabad4038323dc7a4a /src/handlers/mod.rs
parent6242294a4c45f46bfee5d1588e67885f73d36cfb (diff)
Feat: Use random token for admin authentication
Diffstat (limited to 'src/handlers/mod.rs')
-rw-r--r--src/handlers/mod.rs20
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"))