summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-25 15:53:04 +0100
committerGitHub <[email protected]>2024-02-25 15:53:04 +0100
commitf0dc13f907a72ffef44f89b5e197567db129b020 (patch)
treed560273df2eece276cbda021cb4e95c044bb19df /src/main.rs
parentc663810817183c8f92a4279236ca84d271365088 (diff)
parent91cd665671d564620bce13e693cd7ecaad697db9 (diff)
downloadwebol-f0dc13f907a72ffef44f89b5e197567db129b020.tar
webol-f0dc13f907a72ffef44f89b5e197567db129b020.tar.gz
webol-f0dc13f907a72ffef44f89b5e197567db129b020.zip
Merge pull request #17 from FxQnLr/0.3.2
0.3.2
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs60
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 @@
1use std::env;
2use std::sync::Arc;
3use axum::{Router, routing::post};
4use axum::routing::{get, put};
5use dashmap::DashMap;
6use sqlx::PgPool;
7use time::util::local_offset;
8use tokio::sync::broadcast::{channel, Sender};
9use tracing::{info, level_filters::LevelFilter};
10use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*};
11use crate::config::Config; 1use crate::config::Config;
12use crate::db::init_db_pool; 2use crate::db::init_db_pool;
13use crate::routes::device; 3use crate::routes::device;
14use crate::routes::start::start; 4use crate::routes::start::start;
15use crate::routes::status::status; 5use crate::routes::status::status;
16use crate::services::ping::{BroadcastCommands, StatusMap}; 6use crate::services::ping::StatusMap;
7use axum::middleware::from_fn_with_state;
8use axum::routing::{get, put};
9use axum::{routing::post, Router};
10use dashmap::DashMap;
11use services::ping::BroadcastCommand;
12use sqlx::PgPool;
13use std::env;
14use std::sync::Arc;
15use tokio::sync::broadcast::{channel, Sender};
16use tracing::{info, level_filters::LevelFilter};
17use tracing_subscriber::fmt::time::UtcTime;
18use tracing_subscriber::{fmt, prelude::*, EnvFilter};
17 19
18mod auth;
19mod config; 20mod config;
20mod routes;
21mod wol;
22mod db; 21mod db;
23mod error; 22mod error;
23mod extractors;
24mod routes;
24mod services; 25mod services;
26mod wol;
25 27
26#[tokio::main] 28#[tokio::main]
27async fn main() -> color_eyre::eyre::Result<()> { 29async 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)]
79pub struct AppState { 87pub 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}