From 0a058ba2064d323451462a79c71580dea7d8ec8c Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 4 Mar 2024 21:37:55 +0100 Subject: Closes #19. Added OpenApi through `utoipa` --- src/main.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 19 deletions(-) (limited to 'src/main.rs') 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 @@ -use crate::config::Config; -use crate::db::init_db_pool; -use crate::routes::device; -use crate::routes::start::start; -use crate::routes::status::status; -use crate::services::ping::StatusMap; -use axum::middleware::from_fn_with_state; -use axum::routing::{get, put}; -use axum::{routing::post, Router}; +use crate::{ + config::Config, + db::init_db_pool, + routes::{device, start, status}, + services::ping::{BroadcastCommand, StatusMap}, +}; +use axum::{ + middleware::from_fn_with_state, + routing::{get, post}, + Router, +}; use dashmap::DashMap; -use services::ping::BroadcastCommand; use sqlx::PgPool; -use std::env; -use std::sync::Arc; +use std::{env, sync::Arc}; use tokio::sync::broadcast::{channel, Sender}; use tracing::{info, level_filters::LevelFilter}; -use tracing_subscriber::fmt::time::UtcTime; -use tracing_subscriber::{fmt, prelude::*, EnvFilter}; +use tracing_subscriber::{ + fmt::{self, time::UtcTime}, + prelude::*, + EnvFilter, +}; +use utoipa::{ + openapi::security::{ApiKey, ApiKeyValue, SecurityScheme}, + Modify, OpenApi, +}; +use utoipa_swagger_ui::SwaggerUi; mod config; mod db; @@ -25,7 +33,47 @@ mod routes; mod services; mod wol; +#[derive(OpenApi)] +#[openapi( + paths( + start::start, + device::get, + device::get_path, + device::post, + device::put, + ), + components( + schemas( + start::Payload, + start::Response, + device::PutDevicePayload, + device::GetDevicePayload, + device::PostDevicePayload, + db::DeviceSchema, + ) + ), + modifiers(&SecurityAddon), + tags( + (name = "Webol", description = "Webol API") + ) +)] +struct ApiDoc; + +struct SecurityAddon; + +impl Modify for SecurityAddon { + fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) { + if let Some(components) = openapi.components.as_mut() { + components.add_security_scheme( + "api_key", + SecurityScheme::ApiKey(ApiKey::Header(ApiKeyValue::new("Authorization"))), + ); + } + } +} + #[tokio::main] +#[allow(deprecated)] async fn main() -> color_eyre::eyre::Result<()> { color_eyre::install()?; @@ -67,12 +115,15 @@ async fn main() -> color_eyre::eyre::Result<()> { }; let app = Router::new() - .route("/start", post(start)) - .route("/device", get(device::get)) - .route("/device", put(device::put)) - .route("/device", post(device::post)) - .route("/status", get(status)) + .route("/start", post(start::start)) + .route( + "/device", + post(device::post).get(device::get).put(device::put), + ) + .route("/device/:id", get(device::get_path)) + .route("/status", get(status::status)) .route_layer(from_fn_with_state(shared_state.clone(), extractors::auth)) + .merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi())) .with_state(Arc::new(shared_state)); let addr = config.serveraddr; -- cgit v1.2.3