Julio Biason
2 years ago
6 changed files with 56 additions and 148 deletions
@ -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, |
||||
}) |
||||
} |
@ -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) |
||||
} |
||||
} |
@ -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) |
||||
} |
||||
} |
@ -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) |
||||
} |
||||
} |
@ -1,3 +0,0 @@
|
||||
pub mod cipwd; |
||||
pub mod cirole; |
||||
pub mod ciusr; |
Loading…
Reference in new issue