aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs41
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 @@
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,11 +7,10 @@ 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};
17use tracing::{info, level_filters::LevelFilter}; 13use tracing::{info, level_filters::LevelFilter, trace};
18use tracing_subscriber::{ 14use tracing_subscriber::{
19 fmt::{self, time::OffsetTime}, 15 fmt::{self, time::OffsetTime},
20 prelude::*, 16 prelude::*,
@@ -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, 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)]
80async fn main() -> color_eyre::eyre::Result<()> { 71async 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)]
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,