diff options
author | FxQnLr <[email protected]> | 2024-02-25 15:15:19 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-25 15:15:19 +0100 |
commit | 9058f191b69ecafc8fdeace227ac113412d03888 (patch) | |
tree | 88ae071fa31c9a5831722ec82878ccf8fd2b224a /src/main.rs | |
parent | 2f9f18b80a9e2134f674f345e48a5f21de5efadd (diff) | |
download | webol-9058f191b69ecafc8fdeace227ac113412d03888.tar webol-9058f191b69ecafc8fdeace227ac113412d03888.tar.gz webol-9058f191b69ecafc8fdeace227ac113412d03888.zip |
Closes #16. Impl auth as extractor
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 20 |
1 files changed, 9 insertions, 11 deletions
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; | |||
4 | use crate::routes::start::start; | 4 | use crate::routes::start::start; |
5 | use crate::routes::status::status; | 5 | use crate::routes::status::status; |
6 | use crate::services::ping::StatusMap; | 6 | use crate::services::ping::StatusMap; |
7 | use axum::middleware::from_fn_with_state; | ||
7 | use axum::routing::{get, put}; | 8 | use axum::routing::{get, put}; |
8 | use axum::{routing::post, Router}; | 9 | use axum::{routing::post, Router}; |
9 | use dashmap::DashMap; | 10 | use dashmap::DashMap; |
10 | use services::ping::BroadcastCommand; | 11 | use services::ping::BroadcastCommand; |
11 | use sqlx::PgPool; | 12 | use sqlx::PgPool; |
12 | use tracing_subscriber::fmt::time::UtcTime; | ||
13 | use std::env; | 13 | use std::env; |
14 | use std::sync::Arc; | 14 | use std::sync::Arc; |
15 | use tokio::sync::broadcast::{channel, Sender}; | 15 | use tokio::sync::broadcast::{channel, Sender}; |
16 | use tracing::{info, level_filters::LevelFilter}; | 16 | use tracing::{info, level_filters::LevelFilter}; |
17 | use tracing_subscriber::{ | 17 | use tracing_subscriber::fmt::time::UtcTime; |
18 | fmt, | 18 | use tracing_subscriber::{fmt, prelude::*, EnvFilter}; |
19 | prelude::*, | ||
20 | EnvFilter, | ||
21 | }; | ||
22 | 19 | ||
23 | mod auth; | ||
24 | mod config; | 20 | mod config; |
25 | mod db; | 21 | mod db; |
26 | mod error; | 22 | mod error; |
23 | mod extractors; | ||
27 | mod routes; | 24 | mod routes; |
28 | mod services; | 25 | mod services; |
29 | mod wol; | 26 | mod wol; |
@@ -31,7 +28,6 @@ mod wol; | |||
31 | #[tokio::main] | 28 | #[tokio::main] |
32 | async fn main() -> color_eyre::eyre::Result<()> { | 29 | async fn main() -> color_eyre::eyre::Result<()> { |
33 | color_eyre::install()?; | 30 | color_eyre::install()?; |
34 | |||
35 | 31 | ||
36 | let time_format = | 32 | let time_format = |
37 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | 33 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); |
@@ -59,12 +55,12 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
59 | 55 | ||
60 | let ping_map: StatusMap = DashMap::new(); | 56 | let ping_map: StatusMap = DashMap::new(); |
61 | 57 | ||
62 | let shared_state = Arc::new(AppState { | 58 | let shared_state = AppState { |
63 | db, | 59 | db, |
64 | config: config.clone(), | 60 | config: config.clone(), |
65 | ping_send: tx, | 61 | ping_send: tx, |
66 | ping_map, | 62 | ping_map, |
67 | }); | 63 | }; |
68 | 64 | ||
69 | let app = Router::new() | 65 | let app = Router::new() |
70 | .route("/start", post(start)) | 66 | .route("/start", post(start)) |
@@ -72,7 +68,8 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
72 | .route("/device", put(device::put)) | 68 | .route("/device", put(device::put)) |
73 | .route("/device", post(device::post)) | 69 | .route("/device", post(device::post)) |
74 | .route("/status", get(status)) | 70 | .route("/status", get(status)) |
75 | .with_state(shared_state); | 71 | .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) |
72 | .with_state(Arc::new(shared_state)); | ||
76 | 73 | ||
77 | let addr = config.serveraddr; | 74 | let addr = config.serveraddr; |
78 | info!("start server on {}", addr); | 75 | info!("start server on {}", addr); |
@@ -82,6 +79,7 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
82 | Ok(()) | 79 | Ok(()) |
83 | } | 80 | } |
84 | 81 | ||
82 | #[derive(Clone)] | ||
85 | pub struct AppState { | 83 | pub struct AppState { |
86 | db: PgPool, | 84 | db: PgPool, |
87 | config: Config, | 85 | config: Config, |