summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index 9d30548..4ef129b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,4 @@
1use std::env; 1use std::env;
2use std::net::SocketAddr;
3use std::sync::Arc; 2use std::sync::Arc;
4use axum::{Router, routing::post}; 3use axum::{Router, routing::post};
5use axum::routing::{get, put}; 4use axum::routing::{get, put};
@@ -9,7 +8,7 @@ use time::util::local_offset;
9use tokio::sync::broadcast::{channel, Sender}; 8use tokio::sync::broadcast::{channel, Sender};
10use tracing::{info, level_filters::LevelFilter}; 9use tracing::{info, level_filters::LevelFilter};
11use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; 10use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*};
12use crate::config::SETTINGS; 11use crate::config::Config;
13use crate::db::init_db_pool; 12use crate::db::init_db_pool;
14use crate::routes::device; 13use crate::routes::device;
15use crate::routes::start::start; 14use crate::routes::start::start;
@@ -47,16 +46,18 @@ async fn main() -> color_eyre::eyre::Result<()> {
47 46
48 let version = env!("CARGO_PKG_VERSION"); 47 let version = env!("CARGO_PKG_VERSION");
49 48
49 let config = Config::load()?;
50
50 info!("start webol v{}", version); 51 info!("start webol v{}", version);
51 52
52 let db = init_db_pool().await; 53 let db = init_db_pool(&config.database_url).await;
53 sqlx::migrate!().run(&db).await.unwrap(); 54 sqlx::migrate!().run(&db).await.unwrap();
54 55
55 let (tx, _) = channel(32); 56 let (tx, _) = channel(32);
56 57
57 let ping_map: StatusMap = DashMap::new(); 58 let ping_map: StatusMap = DashMap::new();
58 59
59 let shared_state = Arc::new(AppState { db, ping_send: tx, ping_map }); 60 let shared_state = Arc::new(AppState { db, config: config.clone(), ping_send: tx, ping_map });
60 61
61 let app = Router::new() 62 let app = Router::new()
62 .route("/start", post(start)) 63 .route("/start", post(start))
@@ -66,9 +67,9 @@ async fn main() -> color_eyre::eyre::Result<()> {
66 .route("/status", get(status)) 67 .route("/status", get(status))
67 .with_state(shared_state); 68 .with_state(shared_state);
68 69
69 let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); 70 let addr = config.serveraddr;
70 info!("start server on {}", addr); 71 info!("start server on {}", addr);
71 let listener = tokio::net::TcpListener::bind(addr.parse::<SocketAddr>()?) 72 let listener = tokio::net::TcpListener::bind(addr)
72 .await?; 73 .await?;
73 axum::serve(listener, app).await?; 74 axum::serve(listener, app).await?;
74 75
@@ -77,6 +78,7 @@ async fn main() -> color_eyre::eyre::Result<()> {
77 78
78pub struct AppState { 79pub struct AppState {
79 db: PgPool, 80 db: PgPool,
81 config: Config,
80 ping_send: Sender<BroadcastCommands>, 82 ping_send: Sender<BroadcastCommands>,
81 ping_map: StatusMap, 83 ping_map: StatusMap,
82} 84}