diff options
author | FxQnLr <[email protected]> | 2024-04-10 00:16:55 +0200 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-04-10 00:16:55 +0200 |
commit | 3428a637ce420baef9aa9f9803e71bd587867005 (patch) | |
tree | a1ad8234ae9bf3709794324a41e38c2f7fa58d0d /src/main.rs | |
parent | 907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff) | |
download | webol-3428a637ce420baef9aa9f9803e71bd587867005.tar webol-3428a637ce420baef9aa9f9803e71bd587867005.tar.gz webol-3428a637ce420baef9aa9f9803e71bd587867005.zip |
Closes #24. Changed postgres to json directory storage
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 33 |
1 files changed, 8 insertions, 25 deletions
diff --git a/src/main.rs b/src/main.rs index 70c67cf..cf0d39b 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,7 +7,6 @@ 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}; |
@@ -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, | ||
51 | start::Payload, | 44 | start::Payload, |
52 | start::Response, | 45 | start::Response, |
53 | device::DevicePayload, | 46 | device::Payload, |
54 | device::GetDevicePayload, | 47 | storage::DeviceSchema, |
55 | db::DeviceSchema, | ||
56 | ) | 48 | ) |
57 | ), | 49 | ), |
58 | modifiers(&SecurityAddon), | 50 | modifiers(&SecurityAddon), |
@@ -99,34 +91,26 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
99 | ) | 91 | ) |
100 | .init(); | 92 | .init(); |
101 | 93 | ||
102 | let version = env!("CARGO_PKG_VERSION"); | 94 | Device::setup()?; |
103 | 95 | ||
96 | let version = env!("CARGO_PKG_VERSION"); | ||
104 | info!("start webol v{}", version); | 97 | info!("start webol v{}", version); |
105 | 98 | ||
106 | let db = init_db_pool(&config.database_url).await; | ||
107 | sqlx::migrate!().run(&db).await.unwrap(); | ||
108 | |||
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, |