summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs89
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 @@
1use crate::config::Config; 1use crate::{
2use crate::db::init_db_pool; 2 config::Config,
3use crate::routes::device; 3 db::init_db_pool,
4use crate::routes::start::start; 4 routes::{device, start, status},
5use crate::routes::status::status; 5 services::ping::{BroadcastCommand, StatusMap},
6use crate::services::ping::StatusMap; 6};
7use axum::middleware::from_fn_with_state; 7use axum::{
8use axum::routing::{get, put}; 8 middleware::from_fn_with_state,
9use axum::{routing::post, Router}; 9 routing::{get, post},
10 Router,
11};
10use dashmap::DashMap; 12use dashmap::DashMap;
11use services::ping::BroadcastCommand;
12use sqlx::PgPool; 13use sqlx::PgPool;
13use std::env; 14use std::{env, sync::Arc};
14use std::sync::Arc;
15use tokio::sync::broadcast::{channel, Sender}; 15use tokio::sync::broadcast::{channel, Sender};
16use tracing::{info, level_filters::LevelFilter}; 16use tracing::{info, level_filters::LevelFilter};
17use tracing_subscriber::fmt::time::UtcTime; 17use tracing_subscriber::{
18use tracing_subscriber::{fmt, prelude::*, EnvFilter}; 18 fmt::{self, time::UtcTime},
19 prelude::*,
20 EnvFilter,
21};
22use utoipa::{
23 openapi::security::{ApiKey, ApiKeyValue, SecurityScheme},
24 Modify, OpenApi,
25};
26use utoipa_swagger_ui::SwaggerUi;
19 27
20mod config; 28mod config;
21mod db; 29mod db;
@@ -25,7 +33,47 @@ mod routes;
25mod services; 33mod services;
26mod wol; 34mod 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)]
60struct ApiDoc;
61
62struct SecurityAddon;
63
64impl 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)]
29async fn main() -> color_eyre::eyre::Result<()> { 77async 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;