diff options
author | FxQnLr <[email protected]> | 2024-02-12 15:05:03 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-02-12 15:05:03 +0100 |
commit | ca395b81bcadb9dde9214098f2cab0c7a3561574 (patch) | |
tree | 055bae9ef4e65f53b826b240f0b8e3b15b73693e /src/main.rs | |
parent | e4832b4cf36ba0eaed298ee458498eddd7176590 (diff) | |
parent | e50a868f69b602cc0bc84d51e9940878924f49ef (diff) | |
download | webol-ca395b81bcadb9dde9214098f2cab0c7a3561574.tar webol-ca395b81bcadb9dde9214098f2cab0c7a3561574.tar.gz webol-ca395b81bcadb9dde9214098f2cab0c7a3561574.zip |
Merge pull request #13 from FxQnLr/config
Config
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 14 |
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 @@ | |||
1 | use std::env; | 1 | use std::env; |
2 | use std::net::SocketAddr; | ||
3 | use std::sync::Arc; | 2 | use std::sync::Arc; |
4 | use axum::{Router, routing::post}; | 3 | use axum::{Router, routing::post}; |
5 | use axum::routing::{get, put}; | 4 | use axum::routing::{get, put}; |
@@ -9,7 +8,7 @@ use time::util::local_offset; | |||
9 | use tokio::sync::broadcast::{channel, Sender}; | 8 | use tokio::sync::broadcast::{channel, Sender}; |
10 | use tracing::{info, level_filters::LevelFilter}; | 9 | use tracing::{info, level_filters::LevelFilter}; |
11 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 10 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
12 | use crate::config::SETTINGS; | 11 | use crate::config::Config; |
13 | use crate::db::init_db_pool; | 12 | use crate::db::init_db_pool; |
14 | use crate::routes::device; | 13 | use crate::routes::device; |
15 | use crate::routes::start::start; | 14 | use 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 | ||
78 | pub struct AppState { | 79 | pub 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 | } |