summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs20
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;
4use crate::routes::start::start; 4use crate::routes::start::start;
5use crate::routes::status::status; 5use crate::routes::status::status;
6use crate::services::ping::StatusMap; 6use crate::services::ping::StatusMap;
7use axum::middleware::from_fn_with_state;
7use axum::routing::{get, put}; 8use axum::routing::{get, put};
8use axum::{routing::post, Router}; 9use axum::{routing::post, Router};
9use dashmap::DashMap; 10use dashmap::DashMap;
10use services::ping::BroadcastCommand; 11use services::ping::BroadcastCommand;
11use sqlx::PgPool; 12use sqlx::PgPool;
12use tracing_subscriber::fmt::time::UtcTime;
13use std::env; 13use std::env;
14use std::sync::Arc; 14use std::sync::Arc;
15use tokio::sync::broadcast::{channel, Sender}; 15use tokio::sync::broadcast::{channel, Sender};
16use tracing::{info, level_filters::LevelFilter}; 16use tracing::{info, level_filters::LevelFilter};
17use tracing_subscriber::{ 17use tracing_subscriber::fmt::time::UtcTime;
18 fmt, 18use tracing_subscriber::{fmt, prelude::*, EnvFilter};
19 prelude::*,
20 EnvFilter,
21};
22 19
23mod auth;
24mod config; 20mod config;
25mod db; 21mod db;
26mod error; 22mod error;
23mod extractors;
27mod routes; 24mod routes;
28mod services; 25mod services;
29mod wol; 26mod wol;
@@ -31,7 +28,6 @@ mod wol;
31#[tokio::main] 28#[tokio::main]
32async fn main() -> color_eyre::eyre::Result<()> { 29async 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)]
85pub struct AppState { 83pub struct AppState {
86 db: PgPool, 84 db: PgPool,
87 config: Config, 85 config: Config,