diff options
author | FxQnLr <[email protected]> | 2024-02-12 16:00:45 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-02-12 16:00:45 +0100 |
commit | c663810817183c8f92a4279236ca84d271365088 (patch) | |
tree | 0c844cc883e5e474a9cdad30004108852f13f903 /src/main.rs | |
parent | da6367885d31698464e1bec122e3e673974427c6 (diff) | |
parent | 9139d76cb1cf462820b2ddfa80d9a8d55bb30996 (diff) | |
download | webol-c663810817183c8f92a4279236ca84d271365088.tar webol-c663810817183c8f92a4279236ca84d271365088.tar.gz webol-c663810817183c8f92a4279236ca84d271365088.zip |
Merge pull request #14 from FxQnLr/axum7
Axum7 & config changes
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs index e96b736..4ef129b 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -8,12 +8,12 @@ use time::util::local_offset; | |||
8 | use tokio::sync::broadcast::{channel, Sender}; | 8 | use tokio::sync::broadcast::{channel, Sender}; |
9 | use tracing::{info, level_filters::LevelFilter}; | 9 | use tracing::{info, level_filters::LevelFilter}; |
10 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; | 10 | use tracing_subscriber::{EnvFilter, fmt::{self, time::LocalTime}, prelude::*}; |
11 | use crate::config::SETTINGS; | 11 | use crate::config::Config; |
12 | use crate::db::init_db_pool; | 12 | use crate::db::init_db_pool; |
13 | use crate::routes::device::{get_device, post_device, put_device}; | 13 | use crate::routes::device; |
14 | use crate::routes::start::start; | 14 | use crate::routes::start::start; |
15 | use crate::routes::status::status; | 15 | use crate::routes::status::status; |
16 | use crate::services::ping::{BroadcastCommands, PingMap}; | 16 | use crate::services::ping::{BroadcastCommands, StatusMap}; |
17 | 17 | ||
18 | mod auth; | 18 | mod auth; |
19 | mod config; | 19 | mod config; |
@@ -24,7 +24,10 @@ mod error; | |||
24 | mod services; | 24 | mod services; |
25 | 25 | ||
26 | #[tokio::main] | 26 | #[tokio::main] |
27 | async fn main() { | 27 | async fn main() -> color_eyre::eyre::Result<()> { |
28 | |||
29 | color_eyre::install()?; | ||
30 | |||
28 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } | 31 | unsafe { local_offset::set_soundness(local_offset::Soundness::Unsound); } |
29 | let time_format = | 32 | let time_format = |
30 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | 33 | time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); |
@@ -43,35 +46,39 @@ async fn main() { | |||
43 | 46 | ||
44 | let version = env!("CARGO_PKG_VERSION"); | 47 | let version = env!("CARGO_PKG_VERSION"); |
45 | 48 | ||
49 | let config = Config::load()?; | ||
50 | |||
46 | info!("start webol v{}", version); | 51 | info!("start webol v{}", version); |
47 | 52 | ||
48 | let db = init_db_pool().await; | 53 | let db = init_db_pool(&config.database_url).await; |
49 | sqlx::migrate!().run(&db).await.unwrap(); | 54 | sqlx::migrate!().run(&db).await.unwrap(); |
50 | 55 | ||
51 | let (tx, _) = channel(32); | 56 | let (tx, _) = channel(32); |
52 | 57 | ||
53 | let ping_map: PingMap = DashMap::new(); | 58 | let ping_map: StatusMap = DashMap::new(); |
54 | 59 | ||
55 | 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 }); |
56 | 61 | ||
57 | let app = Router::new() | 62 | let app = Router::new() |
58 | .route("/start", post(start)) | 63 | .route("/start", post(start)) |
59 | .route("/device", get(get_device)) | 64 | .route("/device", get(device::get)) |
60 | .route("/device", put(put_device)) | 65 | .route("/device", put(device::put)) |
61 | .route("/device", post(post_device)) | 66 | .route("/device", post(device::post)) |
62 | .route("/status", get(status)) | 67 | .route("/status", get(status)) |
63 | .with_state(shared_state); | 68 | .with_state(shared_state); |
64 | 69 | ||
65 | let addr = SETTINGS.get_string("serveraddr").unwrap_or("0.0.0.0:7229".to_string()); | 70 | let addr = config.serveraddr; |
66 | info!("start server on {}", addr); | 71 | info!("start server on {}", addr); |
67 | axum::Server::bind(&addr.parse().unwrap()) | 72 | let listener = tokio::net::TcpListener::bind(addr) |
68 | .serve(app.into_make_service()) | 73 | .await?; |
69 | .await | 74 | axum::serve(listener, app).await?; |
70 | .unwrap(); | 75 | |
76 | Ok(()) | ||
71 | } | 77 | } |
72 | 78 | ||
73 | pub struct AppState { | 79 | pub struct AppState { |
74 | db: PgPool, | 80 | db: PgPool, |
81 | config: Config, | ||
75 | ping_send: Sender<BroadcastCommands>, | 82 | ping_send: Sender<BroadcastCommands>, |
76 | ping_map: PingMap, | 83 | ping_map: StatusMap, |
77 | } \ No newline at end of file | 84 | } |