Browse Source

Prettify the collections response

master
Julio Biason 2 years ago
parent
commit
537b29ae4e
  1. 44
      axumtest/src/collections/mod.rs
  2. 39
      axumtest/src/headers/cipwd.rs
  3. 39
      axumtest/src/headers/cirole.rs
  4. 39
      axumtest/src/headers/ciusr.rs
  5. 3
      axumtest/src/headers/mod.rs
  6. 40
      axumtest/src/main.rs

44
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<State>, 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<String>,
}
/// Return the list of collections.
async fn get_collections(state: Arc<State>) -> Json<Collections> {
let collections = state.db.list_collection_names(None).await.unwrap();
Json(Collections {
status: "OK".into(),
collections,
})
}

39
axumtest/src/headers/cipwd.rs

@ -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<Self, axum::headers::Error>
where
Self: Sized,
I: Iterator<Item = &'i axum::http::HeaderValue>,
{
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<E: Extend<axum::http::HeaderValue>>(&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)
}
}

39
axumtest/src/headers/cirole.rs

@ -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<Self, axum::headers::Error>
where
Self: Sized,
I: Iterator<Item = &'i axum::http::HeaderValue>,
{
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<E: Extend<axum::http::HeaderValue>>(&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)
}
}

39
axumtest/src/headers/ciusr.rs

@ -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<Self, axum::headers::Error>
where
Self: Sized,
I: Iterator<Item = &'i axum::http::HeaderValue>,
{
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<E: Extend<axum::http::HeaderValue>>(&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)
}
}

3
axumtest/src/headers/mod.rs

@ -1,3 +0,0 @@
pub mod cipwd;
pub mod cirole;
pub mod ciusr;

40
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<State>) -> Json<Vec<String>> {
let collections = state.db.list_collection_names(None).await.unwrap();
Json(collections)
}

Loading…
Cancel
Save