diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/src/main.rs b/src/main.rs index 4ef129b..7d8c1da 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::routing::{get, put}; | ||
8 | use axum::{routing::post, Router}; | ||
9 | use dashmap::DashMap; | ||
10 | use services::ping::BroadcastCommand; | ||
11 | use sqlx::PgPool; | ||
12 | use tracing_subscriber::fmt::time::UtcTime; | ||
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::{ | ||
18 | fmt, | ||
19 | prelude::*, | ||
20 | EnvFilter, | ||
21 | }; | ||
17 | 22 | ||
18 | mod auth; | 23 | mod auth; |
19 | mod config; | 24 | mod config; |
20 | mod routes; | ||
21 | mod wol; | ||
22 | mod db; | 25 | mod db; |
23 | mod error; | 26 | mod error; |
27 | mod routes; | ||
24 | mod services; | 28 | mod services; |
29 | mod wol; | ||
25 | 30 | ||
26 | #[tokio::main] | 31 | #[tokio::main] |
27 | async fn main() -> color_eyre::eyre::Result<()> { | 32 | async fn main() -> color_eyre::eyre::Result<()> { |
28 | |||
29 | color_eyre::install()?; | 33 | color_eyre::install()?; |
34 | |||
30 | 35 | ||
31 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } | ||
32 | let time_format = | 36 | let time_format = |
33 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | 37 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); |
34 | let loc = LocalTime::new(time_format); | 38 | let loc = UtcTime::new(time_format); |
35 | 39 | ||
36 | tracing_subscriber::registry() | 40 | tracing_subscriber::registry() |
37 | .with(fmt::layer() | 41 | .with(fmt::layer().with_timer(loc)) |
38 | .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 = Arc::new(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)) |
@@ -69,8 +76,7 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
69 | 76 | ||
70 | let addr = config.serveraddr; | 77 | let addr = config.serveraddr; |
71 | info!("start server on {}", addr); | 78 | info!("start server on {}", addr); |
72 | let listener = tokio::net::TcpListener::bind(addr) | 79 | let listener = tokio::net::TcpListener::bind(addr).await?; |
73 | .await?; | ||
74 | axum::serve(listener, app).await?; | 80 | axum::serve(listener, app).await?; |
75 | 81 | ||
76 | Ok(()) | 82 | Ok(()) |
@@ -79,6 +85,6 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
79 | pub struct AppState { | 85 | pub struct AppState { |
80 | db: PgPool, | 86 | db: PgPool, |
81 | config: Config, | 87 | config: Config, |
82 | ping_send: Sender<BroadcastCommands>, | 88 | ping_send: Sender<BroadcastCommand>, |
83 | ping_map: StatusMap, | 89 | ping_map: StatusMap, |
84 | } | 90 | } |