diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 89 |
1 files changed, 70 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs index d17984f..8978e58 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,21 +1,29 @@ | |||
1 | use crate::config::Config; | 1 | use crate::{ |
2 | use crate::db::init_db_pool; | 2 | config::Config, |
3 | use crate::routes::device; | 3 | db::init_db_pool, |
4 | use crate::routes::start::start; | 4 | routes::{device, start, status}, |
5 | use crate::routes::status::status; | 5 | services::ping::{BroadcastCommand, StatusMap}, |
6 | use crate::services::ping::StatusMap; | 6 | }; |
7 | use axum::middleware::from_fn_with_state; | 7 | use axum::{ |
8 | use axum::routing::{get, put}; | 8 | middleware::from_fn_with_state, |
9 | use axum::{routing::post, Router}; | 9 | routing::{get, post}, |
10 | Router, | ||
11 | }; | ||
10 | use dashmap::DashMap; | 12 | use dashmap::DashMap; |
11 | use services::ping::BroadcastCommand; | ||
12 | use sqlx::PgPool; | 13 | use sqlx::PgPool; |
13 | use std::env; | 14 | use std::{env, sync::Arc}; |
14 | use std::sync::Arc; | ||
15 | use tokio::sync::broadcast::{channel, Sender}; | 15 | use tokio::sync::broadcast::{channel, Sender}; |
16 | use tracing::{info, level_filters::LevelFilter}; | 16 | use tracing::{info, level_filters::LevelFilter}; |
17 | use tracing_subscriber::fmt::time::UtcTime; | 17 | use tracing_subscriber::{ |
18 | use tracing_subscriber::{fmt, prelude::*, EnvFilter}; | 18 | fmt::{self, time::UtcTime}, |
19 | prelude::*, | ||
20 | EnvFilter, | ||
21 | }; | ||
22 | use utoipa::{ | ||
23 | openapi::security::{ApiKey, ApiKeyValue, SecurityScheme}, | ||
24 | Modify, OpenApi, | ||
25 | }; | ||
26 | use utoipa_swagger_ui::SwaggerUi; | ||
19 | 27 | ||
20 | mod config; | 28 | mod config; |
21 | mod db; | 29 | mod db; |
@@ -25,7 +33,47 @@ mod routes; | |||
25 | mod services; | 33 | mod services; |
26 | mod wol; | 34 | mod wol; |
27 | 35 | ||
36 | #[derive(OpenApi)] | ||
37 | #[openapi( | ||
38 | paths( | ||
39 | start::start, | ||
40 | device::get, | ||
41 | device::get_path, | ||
42 | device::post, | ||
43 | device::put, | ||
44 | ), | ||
45 | components( | ||
46 | schemas( | ||
47 | start::Payload, | ||
48 | start::Response, | ||
49 | device::PutDevicePayload, | ||
50 | device::GetDevicePayload, | ||
51 | device::PostDevicePayload, | ||
52 | db::DeviceSchema, | ||
53 | ) | ||
54 | ), | ||
55 | modifiers(&SecurityAddon), | ||
56 | tags( | ||
57 | (name = "Webol", description = "Webol API") | ||
58 | ) | ||
59 | )] | ||
60 | struct ApiDoc; | ||
61 | |||
62 | struct SecurityAddon; | ||
63 | |||
64 | impl Modify for SecurityAddon { | ||
65 | fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { | ||
66 | if let Some(components) = openapi.components.as_mut() { | ||
67 | components.add_security_scheme( | ||
68 | "api_key", | ||
69 | SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new("Authorization"))), | ||
70 | ); | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
28 | #[tokio::main] | 75 | #[tokio::main] |
76 | #[allow(deprecated)] | ||
29 | async fn main() -> color_eyre::eyre::Result<()> { | 77 | async fn main() -> color_eyre::eyre::Result<()> { |
30 | color_eyre::install()?; | 78 | color_eyre::install()?; |
31 | 79 | ||
@@ -67,12 +115,15 @@ async fn main() -> color_eyre::eyre::Result<()> { | |||
67 | }; | 115 | }; |
68 | 116 | ||
69 | let app = Router::new() | 117 | let app = Router::new() |
70 | .route("/start", post(start)) | 118 | .route("/start", post(start::start)) |
71 | .route("/device", get(device::get)) | 119 | .route( |
72 | .route("/device", put(device::put)) | 120 | "/device", |
73 | .route("/device", post(device::post)) | 121 | post(device::post).get(device::get).put(device::put), |
74 | .route("/status", get(status)) | 122 | ) |
123 | .route("/device/:id", get(device::get_path)) | ||
124 | .route("/status", get(status::status)) | ||
75 | .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) | 125 | .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) |
126 | .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) | ||
76 | .with_state(Arc::new(shared_state)); | 127 | .with_state(Arc::new(shared_state)); |
77 | 128 | ||
78 | let addr = config.serveraddr; | 129 | let addr = config.serveraddr; |