diff options
author | FxQnLr <[email protected]> | 2024-04-11 09:20:04 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2024-04-11 09:20:04 +0200 |
commit | 6b05d1a437a49db98056de7b029923e8aedf1a5a (patch) | |
tree | bc70f14cae1760e91369705273904c0de1bfbf75 /src/main.rs | |
parent | 907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff) | |
parent | 2476e182f61d209768635e8eca6e75b4acfbd007 (diff) | |
download | webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.tar webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.tar.gz webol-6b05d1a437a49db98056de7b029923e8aedf1a5a.zip |
Merge pull request #32 from FxQnLr/0.4.0
0.4.0
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 41 |
1 files changed, 12 insertions, 29 deletions
diff --git a/src/main.rs b/src/main.rs index 70c67cf..204c318 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,8 +1,5 @@ | |||
1 | use crate::{ | 1 | use crate::{ |
2 | config::Config, | 2 | config::Config, routes::{device, start, status}, services::ping::{BroadcastCommand, StatusMap}, storage::Device |
3 | db::init_db_pool, | ||
4 | routes::{device, start, status}, | ||
5 | services::ping::{BroadcastCommand, StatusMap}, | ||
6 | }; | 3 | }; |
7 | use axum::{ | 4 | use axum::{ |
8 | middleware::from_fn_with_state, | 5 | middleware::from_fn_with_state, |
@@ -10,11 +7,10 @@ use axum::{ | |||
10 | Router, | 7 | Router, |
11 | }; | 8 | }; |
12 | use dashmap::DashMap; | 9 | use dashmap::DashMap; |
13 | use sqlx::PgPool; | ||
14 | use std::{env, sync::Arc}; | 10 | use std::{env, sync::Arc}; |
15 | use time::UtcOffset; | 11 | use time::UtcOffset; |
16 | use tokio::sync::broadcast::{channel, Sender}; | 12 | use tokio::sync::broadcast::{channel, Sender}; |
17 | use tracing::{info, level_filters::LevelFilter}; | 13 | use tracing::{info, level_filters::LevelFilter, trace}; |
18 | use tracing_subscriber::{ | 14 | use tracing_subscriber::{ |
19 | fmt::{self, time::OffsetTime}, | 15 | fmt::{self, time::OffsetTime}, |
20 | prelude::*, | 16 | prelude::*, |
@@ -26,10 +22,10 @@ use utoipa::{ | |||
26 | }; | 22 | }; |
27 | use utoipa_swagger_ui::SwaggerUi; | 23 | use utoipa_swagger_ui::SwaggerUi; |
28 | 24 | ||
25 | mod auth; | ||
29 | mod config; | 26 | mod config; |
30 | mod db; | 27 | mod storage; |
31 | mod error; | 28 | mod error; |
32 | mod auth; | ||
33 | mod routes; | 29 | mod routes; |
34 | mod services; | 30 | mod services; |
35 | mod wol; | 31 | mod wol; |
@@ -39,20 +35,16 @@ mod wol; | |||
39 | paths( | 35 | paths( |
40 | start::post, | 36 | start::post, |
41 | start::get, | 37 | start::get, |
42 | start::start_payload, | ||
43 | device::get, | 38 | device::get, |
44 | device::get_payload, | ||
45 | device::post, | 39 | device::post, |
46 | device::put, | 40 | device::put, |
47 | ), | 41 | ), |
48 | components( | 42 | components( |
49 | schemas( | 43 | schemas( |
50 | start::PayloadOld, | 44 | start::SPayload, |
51 | start::Payload, | ||
52 | start::Response, | 45 | start::Response, |
53 | device::DevicePayload, | 46 | device::DPayload, |
54 | device::GetDevicePayload, | 47 | storage::DeviceSchema, |
55 | db::DeviceSchema, | ||
56 | ) | 48 | ) |
57 | ), | 49 | ), |
58 | modifiers(&SecurityAddon), | 50 | modifiers(&SecurityAddon), |
@@ -76,7 +68,6 @@ impl Modify for SecurityAddon { | |||
76 | } | 68 | } |
77 | 69 | ||
78 | #[tokio::main] | 70 | #[tokio::main] |
79 | #[allow(deprecated)] | ||
80 | async fn main() -> color_eyre::eyre::Result<()> { | 71 | async fn main() -> color_eyre::eyre::Result<()> { |
81 | color_eyre::install()?; | 72 | color_eyre::install()?; |
82 | 73 | ||
@@ -98,35 +89,28 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
98 | .from_env_lossy(), | 89 | .from_env_lossy(), |
99 | ) | 90 | ) |
100 | .init(); | 91 | .init(); |
92 | trace!("logging initialized"); | ||
101 | 93 | ||
102 | let version = env!("CARGO_PKG_VERSION"); | 94 | Device::setup()?; |
103 | |||
104 | info!("start webol v{}", version); | ||
105 | 95 | ||
106 | let db = init_db_pool(&config.database_url).await; | 96 | let version = env!("CARGO_PKG_VERSION"); |
107 | sqlx::migrate!().run(&db).await.unwrap(); | 97 | info!(?version, "start webol"); |
108 | 98 | ||
109 | let (tx, _) = channel(32); | 99 | let (tx, _) = channel(32); |
110 | 100 | ||
111 | let ping_map: StatusMap = DashMap::new(); | 101 | let ping_map: StatusMap = DashMap::new(); |
112 | 102 | ||
113 | let shared_state = AppState { | 103 | let shared_state = AppState { |
114 | db, | ||
115 | config: config.clone(), | 104 | config: config.clone(), |
116 | ping_send: tx, | 105 | ping_send: tx, |
117 | ping_map, | 106 | ping_map, |
118 | }; | 107 | }; |
119 | 108 | ||
120 | let app = Router::new() | 109 | let app = Router::new() |
121 | .route("/start", post(start::start_payload)) | ||
122 | .route("/start/:id", post(start::post).get(start::get)) | 110 | .route("/start/:id", post(start::post).get(start::get)) |
123 | .route( | 111 | .route("/device", post(device::post).put(device::put)) |
124 | "/device", | ||
125 | post(device::post).get(device::get_payload).put(device::put), | ||
126 | ) | ||
127 | .route("/device/:id", get(device::get)) | 112 | .route("/device/:id", get(device::get)) |
128 | .route("/status", get(status::status)) | 113 | .route("/status", get(status::status)) |
129 | // TODO: Don't load on `None` Auth | ||
130 | .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) | 114 | .route_layer(from_fn_with_state(shared_state.clone(), auth::auth)) |
131 | .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) | 115 | .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) |
132 | .with_state(Arc::new(shared_state)); | 116 | .with_state(Arc::new(shared_state)); |
@@ -141,7 +125,6 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
141 | 125 | ||
142 | #[derive(Clone)] | 126 | #[derive(Clone)] |
143 | pub struct AppState { | 127 | pub struct AppState { |
144 | db: PgPool, | ||
145 | config: Config, | 128 | config: Config, |
146 | ping_send: Sender<BroadcastCommand>, | 129 | ping_send: Sender<BroadcastCommand>, |
147 | ping_map: StatusMap, | 130 | ping_map: StatusMap, |