Browse Source

We don't need to keep the whole collection (connection is slow, not retrieve)

master
Julio Biason 2 years ago
parent
commit
2084ee6dc8
  1. 2
      axumtest/Cargo.lock
  2. 2
      axumtest/Cargo.toml
  3. 6
      axumtest/src/auth/mod.rs
  4. 40
      axumtest/src/main.rs

2
axumtest/Cargo.lock generated

@ -113,6 +113,8 @@ dependencies = [
"clap",
"dotenv",
"mongodb",
"serde",
"serde_json",
"tokio",
"tracing",
"tracing-subscriber",

2
axumtest/Cargo.toml

@ -8,6 +8,8 @@ axum = { version = "0.5.15", features = ["headers"] }
clap = { version = "3.2.17", features = ["derive", "env"] }
dotenv = "0.15.0"
mongodb = "2.3.0"
serde = { version = "1.0.143", features = ["derive"] }
serde_json = "1.0.83"
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.36"
tracing-subscriber = "0.3.15"

6
axumtest/src/auth/mod.rs

@ -13,9 +13,9 @@ static CIROLE: HeaderName = HeaderName::from_static("x-cirole");
pub async fn ci_auth<B>(
req: Request<B>,
next: Next<B>,
expected_usr: &str,
expected_pwd: &str,
expected_role: &str,
expected_usr: String,
expected_pwd: String,
expected_role: String,
) -> Result<Response, StatusCode> {
let usr = req
.headers()

40
axumtest/src/main.rs

@ -1,16 +1,12 @@
mod auth;
mod headers;
use std::sync::Arc;
use axum::middleware;
use axum::routing::get;
use headers::cipwd::CiPwd;
use headers::cirole::CiRole;
use headers::ciusr::CiUsr;
use axum::Json;
use axum::routing::Router;
use axum::TypedHeader;
use clap::Parser;
use dotenv::dotenv;
use mongodb::options::ClientOptions;
@ -24,11 +20,18 @@ struct Params {
#[clap(short, long, env = "MONGO_URI")]
mongo_addr: String,
}
#[clap(long, env = "CIUSR")]
ci_usr: String,
#[clap(long, env = "CIPWD")]
ci_pwd: String,
#[clap(long, env = "CIROLE")]
ci_role: String,
}
struct State {
db: Database,
collections: Vec<String>,
}
#[tokio::main]
@ -45,7 +48,9 @@ async fn main() {
let collections = db.list_collection_names(None).await.unwrap();
tracing::debug!("Collections = {:?}", &collections);
let state = Arc::new(State { db, collections });
let state = Arc::new(State {
db,
});
let app = Router::new().route("/", get(index)).route(
"/collections",
@ -54,7 +59,11 @@ async fn main() {
move || get_collections(Arc::clone(&shared_state))
})
.route_layer(middleware::from_fn(move |req, next| {
auth::ci_auth(req, next, "A", "A", "A")
let ci_usr = args.ci_usr.clone();
let ci_pwd = args.ci_pwd.clone();
let ci_role = args.ci_role.clone();
auth::ci_auth(req, next, ci_usr, ci_pwd, ci_role)
})),
);
@ -65,14 +74,11 @@ async fn main() {
.unwrap();
}
async fn index(
TypedHeader(usr): TypedHeader<CiUsr>,
TypedHeader(_pwd): TypedHeader<CiPwd>,
TypedHeader(_role): TypedHeader<CiRole>,
) -> String {
format!("Hellow {}", usr)
async fn index() -> String {
format!("Hellow")
}
async fn get_collections(state: Arc<State>) -> String {
format!("Collections")
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