From 537b29ae4ed525baeeaa28d11542b2acfc2cca9d Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 18 Aug 2022 09:22:01 -0300 Subject: [PATCH] Prettify the collections response --- axumtest/src/collections/mod.rs | 44 +++++++++++++++++++++++++++++++++ axumtest/src/headers/cipwd.rs | 39 ----------------------------- axumtest/src/headers/cirole.rs | 39 ----------------------------- axumtest/src/headers/ciusr.rs | 39 ----------------------------- axumtest/src/headers/mod.rs | 3 --- axumtest/src/main.rs | 40 +++++++++--------------------- 6 files changed, 56 insertions(+), 148 deletions(-) create mode 100644 axumtest/src/collections/mod.rs delete mode 100644 axumtest/src/headers/cipwd.rs delete mode 100644 axumtest/src/headers/cirole.rs delete mode 100644 axumtest/src/headers/ciusr.rs delete mode 100644 axumtest/src/headers/mod.rs diff --git a/axumtest/src/collections/mod.rs b/axumtest/src/collections/mod.rs new file mode 100644 index 0000000..305ba52 --- /dev/null +++ b/axumtest/src/collections/mod.rs @@ -0,0 +1,44 @@ +//! Deals specifically with collections. + +use std::sync::Arc; + +use axum::middleware; +use axum::routing::get; +use axum::Json; +use axum::Router; + +use crate::{auth, State}; + +/// Build the routes for the collections resource. +pub fn router(state: Arc, ci_usr: String, ci_pwd: String, ci_role: String) -> Router { + Router::new().route( + "/collections", + get({ + let shared_state = Arc::clone(&state); + move || get_collections(Arc::clone(&shared_state)) + }) + .route_layer(middleware::from_fn(move |req, next| { + let ci_usr = ci_usr.clone(); + let ci_pwd = ci_pwd.clone(); + let ci_role = ci_role.clone(); + + auth::ci_auth(req, next, ci_usr, ci_pwd, ci_role) + })), + ) +} + +/// The response for retrieving the list of collections. +#[derive(serde::Serialize)] +struct Collections { + status: String, + collections: Vec, +} + +/// Return the list of collections. +async fn get_collections(state: Arc) -> Json { + let collections = state.db.list_collection_names(None).await.unwrap(); + Json(Collections { + status: "OK".into(), + collections, + }) +} diff --git a/axumtest/src/headers/cipwd.rs b/axumtest/src/headers/cipwd.rs deleted file mode 100644 index d03cf70..0000000 --- a/axumtest/src/headers/cipwd.rs +++ /dev/null @@ -1,39 +0,0 @@ -//! Header parsing for X-CIPWD - -use axum::headers::Header; -use axum::headers::HeaderName; -use axum::headers::HeaderValue; - -static NAME: HeaderName = HeaderName::from_static("x-cipwd"); - -pub struct CiPwd(String); - -impl Header for CiPwd { - fn name() -> &'static HeaderName { - &NAME - } - - fn decode<'i, I>(values: &mut I) -> Result - where - Self: Sized, - I: Iterator, - { - let value = values.next().ok_or_else(axum::headers::Error::invalid)?; - Ok(CiPwd( - value - .to_str() - .or(Err(axum::headers::Error::invalid()))? - .into(), - )) - } - - fn encode>(&self, values: &mut E) { - values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap())) - } -} - -impl std::fmt::Display for CiPwd { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/axumtest/src/headers/cirole.rs b/axumtest/src/headers/cirole.rs deleted file mode 100644 index 3ac39c7..0000000 --- a/axumtest/src/headers/cirole.rs +++ /dev/null @@ -1,39 +0,0 @@ -//! Header parsing for X-CIROLE - -use axum::headers::Header; -use axum::headers::HeaderName; -use axum::headers::HeaderValue; - -static NAME: HeaderName = HeaderName::from_static("x-cirole"); - -pub struct CiRole(String); - -impl Header for CiRole { - fn name() -> &'static HeaderName { - &NAME - } - - fn decode<'i, I>(values: &mut I) -> Result - where - Self: Sized, - I: Iterator, - { - let value = values.next().ok_or_else(axum::headers::Error::invalid)?; - Ok(CiRole( - value - .to_str() - .or(Err(axum::headers::Error::invalid()))? - .into(), - )) - } - - fn encode>(&self, values: &mut E) { - values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap())) - } -} - -impl std::fmt::Display for CiRole { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/axumtest/src/headers/ciusr.rs b/axumtest/src/headers/ciusr.rs deleted file mode 100644 index 59841ba..0000000 --- a/axumtest/src/headers/ciusr.rs +++ /dev/null @@ -1,39 +0,0 @@ -//! Header parsing for X-CIUSR - -use axum::headers::Header; -use axum::headers::HeaderName; -use axum::headers::HeaderValue; - -static NAME: HeaderName = HeaderName::from_static("x-ciusr"); - -pub struct CiUsr(String); - -impl Header for CiUsr { - fn name() -> &'static HeaderName { - &NAME - } - - fn decode<'i, I>(values: &mut I) -> Result - where - Self: Sized, - I: Iterator, - { - let value = values.next().ok_or_else(axum::headers::Error::invalid)?; - Ok(CiUsr( - value - .to_str() - .or(Err(axum::headers::Error::invalid()))? - .into(), - )) - } - - fn encode>(&self, values: &mut E) { - values.extend(std::iter::once(HeaderValue::from_str(&self.0).unwrap())) - } -} - -impl std::fmt::Display for CiUsr { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/axumtest/src/headers/mod.rs b/axumtest/src/headers/mod.rs deleted file mode 100644 index 9469bda..0000000 --- a/axumtest/src/headers/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod cipwd; -pub mod cirole; -pub mod ciusr; diff --git a/axumtest/src/main.rs b/axumtest/src/main.rs index 4d112c7..feda865 100644 --- a/axumtest/src/main.rs +++ b/axumtest/src/main.rs @@ -1,11 +1,9 @@ mod auth; +mod collections; use std::sync::Arc; -use axum::middleware; use axum::routing::get; -use axum::Json; - use axum::routing::Router; use clap::Parser; use dotenv::dotenv; @@ -30,7 +28,8 @@ struct Params { #[clap(long, env = "CIROLE")] ci_role: String, } -struct State { + +pub struct State { db: Database, } @@ -45,27 +44,17 @@ async fn main() { let mongo_options = ClientOptions::parse(&args.mongo_addr).await.unwrap(); let client = Client::with_options(mongo_options).unwrap(); let db = client.database("helyxTestDatabase"); - let collections = db.list_collection_names(None).await.unwrap(); - tracing::debug!("Collections = {:?}", &collections); - - let state = Arc::new(State { - db, - }); - let app = Router::new().route("/", get(index)).route( - "/collections", - get({ - let shared_state = Arc::clone(&state); - move || get_collections(Arc::clone(&shared_state)) - }) - .route_layer(middleware::from_fn(move |req, next| { - let ci_usr = args.ci_usr.clone(); - let ci_pwd = args.ci_pwd.clone(); - let ci_role = args.ci_role.clone(); + let state = Arc::new(State { db }); - auth::ci_auth(req, next, ci_usr, ci_pwd, ci_role) - })), - ); + let app = Router::new() + .route("/", get(index)) + .merge(collections::router( + state.clone(), + args.ci_usr.clone(), + args.ci_pwd.clone(), + args.ci_role.clone(), + )); tracing::info!(args.addr, "Server listening in"); axum::Server::bind(&args.addr.parse().unwrap()) @@ -77,8 +66,3 @@ async fn main() { async fn index() -> String { format!("Hellow") } - -async fn get_collections(state: Arc) -> Json> { - let collections = state.db.list_collection_names(None).await.unwrap(); - Json(collections) -}