diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/src/main.rs b/src/main.rs index 4ef129b..d17984f 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,42 +1,44 @@ | |||
1 | use std::env; | ||
2 | use std::sync::Arc; | ||
3 | use axum::{Router, routing::post}; | ||
4 | use axum::routing::{get, put}; | ||
5 | use dashmap::DashMap; | ||
6 | use sqlx::PgPool; | ||
7 | use time::util::local_offset; | ||
8 | use tokio::sync::broadcast::{channel, Sender}; | ||
9 | use tracing::{info, level_filters::LevelFilter}; | ||
10 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | ||
11 | use crate::config::Config; | 1 | use crate::config::Config; |
12 | use crate::db::init_db_pool; | 2 | use crate::db::init_db_pool; |
13 | use crate::routes::device; | 3 | use crate::routes::device; |
14 | use crate::routes::start::start; | 4 | use crate::routes::start::start; |
15 | use crate::routes::status::status; | 5 | use crate::routes::status::status; |
16 | use crate::services::ping::{BroadcastCommands, StatusMap}; | 6 | use crate::services::ping::StatusMap; |
7 | use axum::middleware::from_fn_with_state; | ||
8 | use axum::routing::{get, put}; | ||
9 | use axum::{routing::post, Router}; | ||
10 | use dashmap::DashMap; | ||
11 | use services::ping::BroadcastCommand; | ||
12 | use sqlx::PgPool; | ||
13 | use std::env; | ||
14 | use std::sync::Arc; | ||
15 | use tokio::sync::broadcast::{channel, Sender}; | ||
16 | use tracing::{info, level_filters::LevelFilter}; | ||
17 | use tracing_subscriber::fmt::time::UtcTime; | ||
18 | use tracing_subscriber::{fmt, prelude::*, EnvFilter}; | ||
17 | 19 | ||
18 | mod auth; | ||
19 | mod config; | 20 | mod config; |
20 | mod routes; | ||
21 | mod wol; | ||
22 | mod db; | 21 | mod db; |
23 | mod error; | 22 | mod error; |
23 | mod extractors; | ||
24 | mod routes; | ||
24 | mod services; | 25 | mod services; |
26 | mod wol; | ||
25 | 27 | ||
26 | #[tokio::main] | 28 | #[tokio::main] |
27 | async fn main() -> color_eyre::eyre::Result<()> { | 29 | async fn main() -> color_eyre::eyre::Result<()> { |
28 | |||
29 | color_eyre::install()?; | 30 | color_eyre::install()?; |
30 | 31 | ||
31 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } | ||
32 | let time_format = | 32 | let time_format = |
33 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | 33 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); |
34 | let loc = LocalTime::new(time_format); | 34 | let loc = UtcTime::new(time_format); |
35 | |||
36 | let file_appender = tracing_appender::rolling::daily("logs", "webol.log"); | ||
37 | let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); | ||
35 | 38 | ||
36 | tracing_subscriber::registry() | 39 | tracing_subscriber::registry() |
37 | .with(fmt::layer() | 40 | .with(fmt::layer().with_writer(non_blocking).with_ansi(false)) |
38 | .with_timer(loc) | 41 | .with(fmt::layer().with_timer(loc)) |
39 | ) | ||
40 | .with( | 42 | .with( |
41 | EnvFilter::builder() | 43 | EnvFilter::builder() |
42 | .with_default_directive(LevelFilter::INFO.into()) | 44 | .with_default_directive(LevelFilter::INFO.into()) |
@@ -56,8 +58,13 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
56 | let (tx, _) = channel(32); | 58 | let (tx, _) = channel(32); |
57 | 59 | ||
58 | let ping_map: StatusMap = DashMap::new(); | 60 | let ping_map: StatusMap = DashMap::new(); |
59 | 61 | ||
60 | let shared_state = Arc::new(AppState { db, config: config.clone(), ping_send: tx, ping_map }); | 62 | let shared_state = AppState { |
63 | db, | ||
64 | config: config.clone(), | ||
65 | ping_send: tx, | ||
66 | ping_map, | ||
67 | }; | ||
61 | 68 | ||
62 | let app = Router::new() | 69 | let app = Router::new() |
63 | .route("/start", post(start)) | 70 | .route("/start", post(start)) |
@@ -65,20 +72,21 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
65 | .route("/device", put(device::put)) | 72 | .route("/device", put(device::put)) |
66 | .route("/device", post(device::post)) | 73 | .route("/device", post(device::post)) |
67 | .route("/status", get(status)) | 74 | .route("/status", get(status)) |
68 | .with_state(shared_state); | 75 | .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) |
76 | .with_state(Arc::new(shared_state)); | ||
69 | 77 | ||
70 | let addr = config.serveraddr; | 78 | let addr = config.serveraddr; |
71 | info!("start server on {}", addr); | 79 | info!("start server on {}", addr); |
72 | let listener = tokio::net::TcpListener::bind(addr) | 80 | let listener = tokio::net::TcpListener::bind(addr).await?; |
73 | .await?; | ||
74 | axum::serve(listener, app).await?; | 81 | axum::serve(listener, app).await?; |
75 | 82 | ||
76 | Ok(()) | 83 | Ok(()) |
77 | } | 84 | } |
78 | 85 | ||
86 | #[derive(Clone)] | ||
79 | pub struct AppState { | 87 | pub struct AppState { |
80 | db: PgPool, | 88 | db: PgPool, |
81 | config: Config, | 89 | config: Config, |
82 | ping_send: Sender<BroadcastCommands>, | 90 | ping_send: Sender<BroadcastCommand>, |
83 | ping_map: StatusMap, | 91 | ping_map: StatusMap, |
84 | } | 92 | } |