Skip to content

Commit

Permalink
tweak flags
Browse files Browse the repository at this point in the history
  • Loading branch information
archeoss committed Jan 14, 2024
1 parent 221ac7b commit 13004b4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
3 changes: 2 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]

22 changes: 11 additions & 11 deletions backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -92,36 +91,37 @@ 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};
pub use hyper::{client::HttpConnector, Body, Method, Request, StatusCode};
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;
}

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};
Expand Down
13 changes: 7 additions & 6 deletions backend/src/models/shared.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
25 changes: 23 additions & 2 deletions backend/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,7 +59,6 @@ pub struct ContextRouter<Version, Doc, S = (), B = Body> {
impl<'a, Version, Doc, S, B> ContextRouter<Version, Doc, S, B>
where
Version: ApiVersion<'a>,
Doc: OpenApi,
B: HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -138,6 +139,7 @@ impl<V, D, S, B> Deref for ContextRouter<V, D, S, B> {
}
}

#[cfg(all(feature = "swagger", debug_assertions))]
pub trait RouterApiExt<S = (), B = Body, E = Infallible> {
/// Wraps `Router` with `ApiVersion` and `OpenApi` instances into the new context to call
/// `api_route` with said context
Expand All @@ -146,6 +148,14 @@ pub trait RouterApiExt<S = (), B = Body, E = Infallible> {
) -> ContextRouter<Version, Doc, S, B>;
}

#[cfg(not(all(feature = "swagger", debug_assertions)))]
pub trait RouterApiExt<S = (), B = Body, E = Infallible> {
/// 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<Version, Doc, S, B>;
}

#[cfg(all(feature = "swagger", debug_assertions))]
impl<S, B> RouterApiExt<S, B, Infallible> for Router<S, B>
where
B: HttpBody + Send + 'static,
Expand All @@ -158,6 +168,17 @@ where
}
}

#[cfg(not(all(feature = "swagger", debug_assertions)))]
impl<S, B> RouterApiExt<S, B, Infallible> for Router<S, B>
where
B: HttpBody + Send + 'static,
S: Clone + Send + Sync + 'static,
{
fn with_context<'a, Version: ApiVersion<'a>, Doc>(self) -> ContextRouter<Version, Doc, S, B> {
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))]
Expand Down

0 comments on commit 13004b4

Please sign in to comment.