summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-10 00:16:55 +0200
committerFxQnLr <[email protected]>2024-04-10 00:16:55 +0200
commit3428a637ce420baef9aa9f9803e71bd587867005 (patch)
treea1ad8234ae9bf3709794324a41e38c2f7fa58d0d /src/main.rs
parent907e5cb5bc48899b444f7fedd85af7b5974d9a2e (diff)
downloadwebol-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.rs33
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 @@
1use crate::{ 1use 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};
7use axum::{ 4use 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};
12use dashmap::DashMap; 9use dashmap::DashMap;
13use sqlx::PgPool;
14use std::{env, sync::Arc}; 10use std::{env, sync::Arc};
15use time::UtcOffset; 11use time::UtcOffset;
16use tokio::sync::broadcast::{channel, Sender}; 12use tokio::sync::broadcast::{channel, Sender};
@@ -26,10 +22,10 @@ use utoipa::{
26}; 22};
27use utoipa_swagger_ui::SwaggerUi; 23use utoipa_swagger_ui::SwaggerUi;
28 24
25mod auth;
29mod config; 26mod config;
30mod db; 27mod storage;
31mod error; 28mod error;
32mod auth;
33mod routes; 29mod routes;
34mod services; 30mod services;
35mod wol; 31mod 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)]
143pub struct AppState { 127pub 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,