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