From c34dec5c94f694c96a93cd1e5913c514923d1abb Mon Sep 17 00:00:00 2001 From: wildonion Date: Sun, 14 Jul 2024 16:03:33 +0330 Subject: [PATCH] showing structures in swagger ui --- src/apis/v1/http/auth.rs | 5 ++-- src/apis/v1/http/health.rs | 3 ++- src/apis/v1/http/hoop.rs | 7 ++++- src/apis/v1/http/notif.rs | 23 ++++++++++------ src/apis/v1/ws/notif.rs | 3 ++- src/middlewares/passport.rs | 4 +-- src/models/event.rs | 10 +++---- src/server/mod.rs | 52 ++++++++++++++++++++++++++++++++----- 8 files changed, 81 insertions(+), 26 deletions(-) diff --git a/src/apis/v1/http/auth.rs b/src/apis/v1/http/auth.rs index f919f77..8289afe 100644 --- a/src/apis/v1/http/auth.rs +++ b/src/apis/v1/http/auth.rs @@ -13,7 +13,7 @@ use interfaces::passport::Passport; -#[derive(Deserialize)] +#[derive(Deserialize, ToSchema)] struct GenTokenQueries { exp: Option, scope: Option @@ -34,7 +34,8 @@ pub async fn generate_access_token( }); */ - ctrl: &mut FlowCtrl + ctrl: &mut FlowCtrl, + params: QueryParam // query param is required, showcasing in swagger ui ){ diff --git a/src/apis/v1/http/health.rs b/src/apis/v1/http/health.rs index 9ed1ebd..8edf942 100644 --- a/src/apis/v1/http/health.rs +++ b/src/apis/v1/http/health.rs @@ -142,7 +142,8 @@ pub async fn mint( req: &mut Request, res: &mut Response, depot: &mut Depot, - ctrl: &mut FlowCtrl + ctrl: &mut FlowCtrl, + prod: JsonBody, // used to extract the request body as well as showcasing in swagger ui ){ let app_ctx = depot.obtain::>().unwrap(); // extracting shared app context diff --git a/src/apis/v1/http/hoop.rs b/src/apis/v1/http/hoop.rs index 5093610..03c11f2 100644 --- a/src/apis/v1/http/hoop.rs +++ b/src/apis/v1/http/hoop.rs @@ -1,5 +1,6 @@ +use models::event::EventQuery; use salvo::Error; use serde::{Deserialize, Serialize}; use crate::*; @@ -59,8 +60,12 @@ pub async fn get_hoop( req: &mut Request, res: &mut Response, depot: &mut Depot, - ctrl: &mut FlowCtrl + ctrl: &mut FlowCtrl, + query_params: QueryParam ){ + + let query_params = req.parse_queries::().unwrap(); + res.render("developing...") } diff --git a/src/apis/v1/http/notif.rs b/src/apis/v1/http/notif.rs index 0f1ef1d..998d7db 100644 --- a/src/apis/v1/http/notif.rs +++ b/src/apis/v1/http/notif.rs @@ -46,18 +46,24 @@ pub async fn register_notif( }); */ - ctrl: &mut FlowCtrl + ctrl: &mut FlowCtrl, + register_notif_req: JsonBody // used to extract the request body as well as showcasing in swagger ui ){ + // if we're here means the passport verification was true and we + // can access the passed in token_string to the request header + let passport_verification_status = depot.get::("passport_token_time").unwrap(); let app_ctx = depot.obtain::>().unwrap(); // extracting shared app context let zerlog_producer_actor = app_ctx.as_ref().unwrap().actors.clone().unwrap().broker_actors.zerlog_actor; let app_ctx = app_ctx.clone(); + // extracting the request body let register_notif_req = req.extract::().await.unwrap(); let get_producer_info = register_notif_req.clone().producer_info; let get_consumer_info = register_notif_req.clone().consumer_info; + // there is a producer info in body if get_producer_info.is_some(){ let producer_info = get_producer_info.unwrap(); let mut notif = producer_info.info; @@ -71,7 +77,7 @@ pub async fn register_notif( as the data coming at a same time, kindly put the sending message logic to actor inside a loop{}. */ - tokio::spawn( // running the producing notif job in the background in a free thread + tokio::spawn( // running the producing notif job in the background in a separate thread { let cloned_app_ctx = app_ctx.clone(); let cloned_notif = notif.clone(); @@ -100,7 +106,7 @@ pub async fn register_notif( // in here the background task might have halted, executed or even // crashed but the response is sent already to the caller regardless - // of what ever has happened. + // of what ever has happened inside the tokio::spawn // fill the response object, salvo returns it by itself to the caller res.status_code = Some(StatusCode::OK); @@ -116,7 +122,7 @@ pub async fn register_notif( } )); - } else if get_consumer_info.is_some(){ + } else if get_consumer_info.is_some(){ // there is a consumer info in body let consumer_info = get_consumer_info.unwrap(); let mut notif = consumer_info.info; notif.exchange_name = format!("{}.notif:{}", APP_NAME, notif.exchange_name); @@ -129,7 +135,7 @@ pub async fn register_notif( you would have to send message to its actor to start it as an async task in the background. */ - tokio::spawn( // running the consuming notif job in the background in a free thread + tokio::spawn( // running the consuming notif job in the background in a separate thread { let cloned_app_ctx = app_ctx.clone(); let cloned_notif = notif.clone(); @@ -178,7 +184,7 @@ pub async fn register_notif( } )); - } else{ + } else{ // neither producer nor consumer are passed // fill the response object, salvo returns it by itself to the caller let server_time = format!("{}", chrono::Local::now().to_string()); @@ -210,7 +216,8 @@ pub async fn get_notif( req: &mut Request, res: &mut Response, depot: &mut Depot, - ctrl: &mut FlowCtrl + ctrl: &mut FlowCtrl, + notif_query: QueryParam // query param is required, showcasing in swagger ui ){ let app_ctx = depot.obtain::>().unwrap(); // extracting shared app context @@ -544,7 +551,7 @@ pub async fn get_notif( pub fn register_controller() -> Router{ Router::with_path("/v1/events/notif/") - .hoop(check_passport) + .hoop(check_passport) // before falling into each router this gets executed first .oapi_tag("Events") // the passport verifier middleware .post(register_notif) .get(get_notif) diff --git a/src/apis/v1/ws/notif.rs b/src/apis/v1/ws/notif.rs index 88950e0..c4a3edf 100644 --- a/src/apis/v1/ws/notif.rs +++ b/src/apis/v1/ws/notif.rs @@ -31,7 +31,8 @@ async fn consume( req: &mut Request, res: &mut Response, depot: &mut Depot, - ctrl: &mut FlowCtrl + ctrl: &mut FlowCtrl, + notif_query: QueryParam // query param is required, showcasing in swagger ui ) -> Result<(), StatusError>{ let app_ctx = depot.obtain::>().unwrap(); // extracting shared app context diff --git a/src/middlewares/passport.rs b/src/middlewares/passport.rs index 163fce5..9319a3c 100644 --- a/src/middlewares/passport.rs +++ b/src/middlewares/passport.rs @@ -41,11 +41,11 @@ pub async fn check_passport( Ok(token_time) => { depot.insert("passport_verified", true); + depot.insert("passport_token_time", token_time); }, Err(resp_err) => { - ctrl.skip_rest(); // skip executing all rest handlers - + ctrl.skip_rest(); // skip other handlers executing cause there would be no need to execute newer handlers depot.insert("passport_verified", false); /* ------------------- diff --git a/src/models/event.rs b/src/models/event.rs index 853cf87..f0bd83f 100644 --- a/src/models/event.rs +++ b/src/models/event.rs @@ -9,7 +9,7 @@ use crate::*; -#[derive(Serialize, Deserialize, Clone, Default, Debug)] +#[derive(Serialize, Deserialize, Clone, Default, Debug, ToSchema)] pub struct EventQuery{ pub owner: Option, // the entity owner pub id: Option, // any entity id @@ -19,23 +19,23 @@ pub struct EventQuery{ pub page_size: Option, } -#[derive(Serialize, Deserialize, Clone, Default, Debug)] +#[derive(Serialize, Deserialize, Clone, Default, Debug, ToSchema)] pub struct UpdateHoopRequest{ pub hoop_id: i32, } -#[derive(Serialize, Deserialize, Clone, Default, Debug)] +#[derive(Serialize, Deserialize, Clone, Default, Debug, ToSchema)] pub struct NewHoopRequest{ } -#[derive(Serialize, Deserialize, Clone, Default, Debug)] +#[derive(Serialize, Deserialize, Clone, Default, Debug, ToSchema)] pub struct GenerateTokenTimeQuery{ pub exp_time: Option, // in seconds pub scope: Option } -#[derive(Serialize, Deserialize, Clone, Default, Debug)] +#[derive(Serialize, Deserialize, Clone, Default, Debug, ToSchema)] pub enum TokenTimeScope{ #[default] Write, diff --git a/src/server/mod.rs b/src/server/mod.rs index 3a55993..f2e3cbe 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -3,6 +3,7 @@ use crate::*; use actix::{Actor, Addr, AsyncContext, Context}; +use actix_web::middleware::Compress; use constants::{NEXT_USER_ID, ONLINE_USERS, WS_ROOMS}; use futures::{lock, FutureExt}; use models::event::EventQuery; @@ -24,7 +25,7 @@ pub trait HoopoeService{ // supports polymorphism pub struct HoopoeServer{ pub service: Option, pub addr: String, - pub app_ctx: Option, + pub app_ctx: Option, // the whole app context: db, actors and global types pub ssl_domain: Option } @@ -38,15 +39,35 @@ impl HoopoeServer{ let app_ctx = self.app_ctx.clone(); let router = Router::new() .push(routers::register_app_controllers()) + .hoop(Compression::new().enable_brotli(CompressionLevel::Fastest)) .hoop(Logger::new()) // register middlewares using hoop() method .hoop(affix::inject(app_ctx)) .hoop(cors); - // adding swagger ui route + // adding api ui routes let doc = OpenApi::new("Hoopoe Api", "0.1.0").merge_router(&router); let router = router .push(doc.into_router("/api-doc/openapi.json")) - .push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger")); + .unshift( + Scalar::new("/api-doc/openapi.json") + .title("Hoopoe - Scalar") + .into_router("scalar"), + ) + .unshift( + SwaggerUi::new("/api-doc/openapi.json") + .title("Hoopoe - Swagger UI") + .into_router("swagger") + ) + .unshift( + RapiDoc::new("/api-doc/openapi.json") + .title("Hoopoe - RapiDoc") + .into_router("rapidoc"), + ) + .unshift( + ReDoc::new("/api-doc/openapi.json") + .title("Hoopoe - ReDoc") + .into_router("redoc"), + ); router } @@ -57,16 +78,35 @@ impl HoopoeServer{ .into_handler(); let router = Router::new() .push(routers::register_app_controllers()) + .hoop(Compression::new().enable_brotli(CompressionLevel::Fastest)) .hoop(Logger::new()) // register middlewares using hoop() method .hoop(affix::inject(app_ctx)) .hoop(cors); - // adding swagger ui route let doc = OpenApi::new("Hoopoe Api", "0.1.0").merge_router(&router); let router = router .push(doc.into_router("/api-doc/openapi.json")) - .push(SwaggerUi::new("/api-doc/openapi.json").into_router("swagger")); + .unshift( + Scalar::new("/api-doc/openapi.json") + .title("Hoopoe - Scalar") + .into_router("scalar"), + ) + .unshift( + SwaggerUi::new("/api-doc/openapi.json") + .title("Hoopoe - Swagger UI") + .into_router("swagger") + ) + .unshift( + RapiDoc::new("/api-doc/openapi.json") + .title("Hoopoe - RapiDoc") + .into_router("rapidoc"), + ) + .unshift( + ReDoc::new("/api-doc/openapi.json") + .title("Hoopoe - ReDoc") + .into_router("redoc"), + ); router } @@ -440,7 +480,7 @@ impl HoopoeWsServerActor{ }) .or_insert(users); // if the key is not there insert a new user ids }); - + while let Some(result) = current_user_ws_rx.next().await { let msg = match result {