From 13004b4c43d4c6e158b47a7260b73108c9880039 Mon Sep 17 00:00:00 2001 From: Simeon Romanov Date: Sun, 14 Jan 2024 03:03:04 +0300 Subject: [PATCH] tweak flags --- backend/Cargo.toml | 3 ++- backend/src/lib.rs | 22 +++++++++++----------- backend/src/models/shared.rs | 13 +++++++------ backend/src/router.rs | 25 +++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 1312f658..06a97674 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -51,7 +51,8 @@ cli = { path = "../cli" } proc-macro = { path = "../proc_macro" } [features] -default = [ "swagger" ] +# default = [ "swagger" ] +default = [ ] swagger = [ "dep:utoipa", "dep:utoipa-swagger-ui" , "dep:utoipa-redoc", "dep:utoipa-rapidoc" ] gen_api = [ "dep:utoipa" ] diff --git a/backend/src/lib.rs b/backend/src/lib.rs index bbfa960e..c009b995 100644 --- a/backend/src/lib.rs +++ b/backend/src/lib.rs @@ -12,8 +12,7 @@ pub mod models; pub mod router; pub mod services; -#[derive(OpenApi)] -#[cfg_attr(not(all(feature = "swagger", debug_assertions)), openapi())] +#[cfg_attr(all(feature = "swagger", debug_assertions), derive(OpenApi))] #[cfg_attr(all(feature = "swagger", debug_assertions), openapi( paths(root, services::auth::login, services::auth::logout), components( @@ -92,8 +91,7 @@ pub mod prelude { pub use axum::{ async_trait, headers::authorization::Basic, - response::Result as AxumResult, - response::{IntoResponse, Response}, + response::{IntoResponse, Response, Result as AxumResult}, Extension, Json, }; pub use error_stack::{Context, Report, Result, ResultExt}; @@ -101,6 +99,7 @@ pub mod prelude { pub use serde::{Deserialize, Serialize}; pub use std::{collections::HashMap, hash::Hash, marker::PhantomData, str::FromStr}; pub use thiserror::Error; + #[cfg(all(feature = "swagger", debug_assertions))] pub use utoipa::{IntoParams, OpenApi, ToSchema}; pub use uuid::Uuid; } @@ -108,20 +107,21 @@ pub mod prelude { pub mod main { pub mod prelude { pub use crate::{ - config::ConfigExt, + config::{ConfigExt, LoggerExt}, models::shared::RequestTimeout, prelude::*, root, router::{ApiV1, ApiVersion, NoApi, RouterApiExt}, - services::api_router_v1, - services::auth::{ - require_auth, AuthState, BobUser, HttpBobClient, InMemorySessionStore, + services::{ + api_router_v1, + auth::{require_auth, AuthState, BobUser, HttpBobClient, InMemorySessionStore}, }, ApiDoc, }; - pub use axum::error_handling::HandleErrorLayer; - pub use axum::middleware::from_fn_with_state; - pub use axum::{BoxError, Extension, Router}; + pub use axum::{ + error_handling::HandleErrorLayer, middleware::from_fn_with_state, BoxError, Extension, + Router, + }; pub use cli::Parser; pub use error_stack::{Result, ResultExt}; pub use hyper::{Method, StatusCode}; diff --git a/backend/src/models/shared.rs b/backend/src/models/shared.rs index eeca6367..50f2f763 100644 --- a/backend/src/models/shared.rs +++ b/backend/src/models/shared.rs @@ -1,7 +1,8 @@ use super::prelude::*; use std::result::Result; -#[derive(ToSchema, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[cfg_attr(all(feature = "swagger", debug_assertions), derive(ToSchema))] pub struct Hostname( #[serde( deserialize_with = "hyper_serde::deserialize", @@ -63,8 +64,9 @@ impl ToString for Hostname { } /// Data needed to connect to a BOB cluster -#[derive(IntoParams, ToSchema, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(IntoParams, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] #[schema(example = json!({"hostname": "0.0.0.0:7000", "credentials": {"login": "archeoss", "password": "12345"}}))] +#[cfg_attr(all(feature = "swagger", debug_assertions), derive(ToSchema))] pub struct BobConnectionData { /// Address to connect to pub hostname: Hostname, @@ -75,10 +77,9 @@ pub struct BobConnectionData { } /// Optional auth credentials for a BOB cluster -#[derive( - ToSchema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, -)] -#[schema(example = json!({"login": "archeoss", "password": "12345"}))] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize)] +#[cfg_attr(all(feature = "swagger", debug_assertions), schema(example = json!({"login": "archeoss", "password": "12345"})))] +#[cfg_attr(all(feature = "swagger", debug_assertions), derive(ToSchema))] pub struct Credentials { /// Login used during auth pub login: String, diff --git a/backend/src/router.rs b/backend/src/router.rs index 93059a84..2d52cbf9 100644 --- a/backend/src/router.rs +++ b/backend/src/router.rs @@ -5,10 +5,11 @@ use axum::{handler::Handler, routing::MethodFilter, Router}; use std::convert::Infallible; use std::ops::Deref; use thiserror::Error; -use utoipa::OpenApi; #[cfg(all(feature = "swagger", debug_assertions))] use utoipa::openapi::PathItemType; +#[cfg(all(feature = "swagger", debug_assertions))] +use utoipa::OpenApi; #[derive(Clone, Debug, Error, PartialEq, Eq, PartialOrd, Ord)] pub enum RouteError { @@ -58,7 +59,6 @@ pub struct ContextRouter { impl<'a, Version, Doc, S, B> ContextRouter where Version: ApiVersion<'a>, - Doc: OpenApi, B: HttpBody + Send + 'static, S: Clone + Send + Sync + 'static, { @@ -100,6 +100,7 @@ where T: 'static, S: Clone + Send + Sync + 'static, B: HttpBody + Send + 'static, + Doc: OpenApi, { #[cfg(all(feature = "swagger", debug_assertions))] match try_convert_path_item_type_from_method(method) @@ -138,6 +139,7 @@ impl Deref for ContextRouter { } } +#[cfg(all(feature = "swagger", debug_assertions))] pub trait RouterApiExt { /// Wraps `Router` with `ApiVersion` and `OpenApi` instances into the new context to call /// `api_route` with said context @@ -146,6 +148,14 @@ pub trait RouterApiExt { ) -> ContextRouter; } +#[cfg(not(all(feature = "swagger", debug_assertions)))] +pub trait RouterApiExt { + /// Wraps `Router` with `ApiVersion` and `OpenApi` instances into the new context to call + /// `api_route` with said context + fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter; +} + +#[cfg(all(feature = "swagger", debug_assertions))] impl RouterApiExt for Router where B: HttpBody + Send + 'static, @@ -158,6 +168,17 @@ where } } +#[cfg(not(all(feature = "swagger", debug_assertions)))] +impl RouterApiExt for Router +where + B: HttpBody + Send + 'static, + S: Clone + Send + Sync + 'static, +{ + fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter { + ContextRouter::new(self) + } +} + /// Check if the following route corresponds with `OpenAPI` declaration /// Relies on `operation_id` field, must NOT be changed on handler's declaration #[cfg(all(feature = "swagger", debug_assertions))]