From 9058f191b69ecafc8fdeace227ac113412d03888 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 15:15:19 +0100 Subject: Closes #16. Impl auth as extractor --- src/main.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 7d8c1da..eae89f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,26 +4,23 @@ use crate::routes::device; use crate::routes::start::start; use crate::routes::status::status; use crate::services::ping::StatusMap; +use axum::middleware::from_fn_with_state; use axum::routing::{get, put}; use axum::{routing::post, Router}; use dashmap::DashMap; use services::ping::BroadcastCommand; use sqlx::PgPool; -use tracing_subscriber::fmt::time::UtcTime; use std::env; use std::sync::Arc; use tokio::sync::broadcast::{channel, Sender}; use tracing::{info, level_filters::LevelFilter}; -use tracing_subscriber::{ - fmt, - prelude::*, - EnvFilter, -}; +use tracing_subscriber::fmt::time::UtcTime; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; -mod auth; mod config; mod db; mod error; +mod extractors; mod routes; mod services; mod wol; @@ -31,7 +28,6 @@ mod wol; #[tokio::main] async fn main() -> color_eyre::eyre::Result<()> { color_eyre::install()?; - let time_format = time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); @@ -59,12 +55,12 @@ async fn main() -> color_eyre::eyre::Result<()> { let ping_map: StatusMap = DashMap::new(); - let shared_state = Arc::new(AppState { + let shared_state = AppState { db, config: config.clone(), ping_send: tx, ping_map, - }); + }; let app = Router::new() .route("/start", post(start)) @@ -72,7 +68,8 @@ async fn main() -> color_eyre::eyre::Result<()> { .route("/device", put(device::put)) .route("/device", post(device::post)) .route("/status", get(status)) - .with_state(shared_state); + .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) + .with_state(Arc::new(shared_state)); let addr = config.serveraddr; info!("start server on {}", addr); @@ -82,6 +79,7 @@ async fn main() -> color_eyre::eyre::Result<()> { Ok(()) } +#[derive(Clone)] pub struct AppState { db: PgPool, config: Config, -- cgit v1.2.3