engine routing experiment
This commit is contained in:
parent
be4e698d61
commit
2c355ef75d
1 changed files with 31 additions and 62 deletions
|
|
@ -1,77 +1,46 @@
|
||||||
pub mod kv;
|
pub mod kv;
|
||||||
|
|
||||||
use crate::engines::kv::logic::body_to_json;
|
|
||||||
use crate::engines::kv::structs::KvSecret;
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, Request},
|
body::Body,
|
||||||
http::StatusCode,
|
extract::{Request, State},
|
||||||
middleware::{self, Next},
|
|
||||||
response::Response,
|
response::Response,
|
||||||
routing::*,
|
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use log::*;
|
use log::*;
|
||||||
use sqlx::{Any, Pool};
|
use sqlx::{Any, Pool};
|
||||||
|
|
||||||
pub fn secrets_router(pool: Pool<Any>) -> Router<Pool<Any>> {
|
pub fn secrets_router(pool: Pool<Any>) -> Router<Pool<Any>> {
|
||||||
// Router::new().layer(map_request(handler))
|
Router::new().fallback(map_mount_points).with_state(pool)
|
||||||
|
|
||||||
Router::new()
|
|
||||||
.route("/:mount_path/data/:kv_path", post(baz))
|
|
||||||
.with_state(pool)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Routing handler for path "/v1/kv-v2/data/foo"
|
async fn map_mount_points(State(_pool): State<Pool<Any>>, req: Request) -> Response<Body> {
|
||||||
/// expects payload as JSON, prints payload into struct
|
// let mount_path = mount_path.trim_start_matches('/');
|
||||||
|
// debug!("Mount path: {}", mount_path);
|
||||||
|
debug!("Mount path: {:?}", req);
|
||||||
|
|
||||||
async fn baz(Path(mount_path): Path<String>, Path(kv_path): Path<String>, body: String) -> String {
|
let mut mount_path: Vec<&str> = req.uri().path().split("/").collect();
|
||||||
let mut body_json = body_to_json(body);
|
|
||||||
|
|
||||||
// TODO: If version field provided during a read, the value at the version number will be returned
|
// let a = sqlx::query!("SELECT * FROM secret_engines WHERE path = $1", mount_path.join("/"))
|
||||||
let secret: KvSecret = KvSecret {
|
// .fetch_one(&pool)
|
||||||
data: body_json.to_string(),
|
// .await
|
||||||
// content: body_json["data"]["password1"].take().to_string(),
|
// .expect("Failed to fetch secret engine");
|
||||||
version: body_json["data"]["version"].take().as_i64(),
|
|
||||||
};
|
let tada = vec!["a/b/c/d".to_string(), "/a/b/c".to_string(), "/kv-v2".into()];
|
||||||
log::debug!(
|
|
||||||
"Secret: {}, Content: {}, Version: {:?}, path: {}",
|
// Find longest matching existing mount path for the request
|
||||||
kv_path,
|
for _ in 1..mount_path.len() {
|
||||||
secret.data,
|
if tada.contains(&mount_path.join("/")) {
|
||||||
secret.version,
|
trace!(
|
||||||
mount_path,
|
"Mount path {} found for route request: {}",
|
||||||
|
mount_path.join("/"),
|
||||||
|
req.uri().path()
|
||||||
);
|
);
|
||||||
String::from("RoutingTest baz successful")
|
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
mount_path.pop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// async fn handler(Host(hostname): Host, request: Request<Body>) -> &'static str {
|
return Response::new(Body::from(format!("Mount path:")));
|
||||||
// TODO: Find a solution for this mess
|
}
|
||||||
// async fn handler(request: Request<Body>) -> Result<Request<Body>, StatusCode> {
|
|
||||||
// // let path: Vec<&str> = request.uri().path().split('/').clone().collect();
|
|
||||||
// // log::info!("path, {:?}", path[1]);
|
|
||||||
|
|
||||||
// let root = service_fn(|req: Request<String>| async move {
|
|
||||||
// let res = Response::new("Hello, World!".to_string());
|
|
||||||
// Ok::<_, Infallible>(res)
|
|
||||||
// });
|
|
||||||
// let root = BoxService::new(root);
|
|
||||||
|
|
||||||
// let mut routes = vec!["/abc", "/def"];
|
|
||||||
// routes.sort_unstable_by(|a, b| a.len().cmp(&b.len()));
|
|
||||||
|
|
||||||
// let mut app = Router::new();
|
|
||||||
// app.as_service().call(request).await.unwrap();
|
|
||||||
|
|
||||||
// // match path[1] {
|
|
||||||
// // "test" => {
|
|
||||||
// // log::info!("test route");
|
|
||||||
// // // TODO: Nest another Router here
|
|
||||||
// // return Ok(Request::new(Body::empty()));
|
|
||||||
// // }
|
|
||||||
// // _ => {
|
|
||||||
// // log::info!("default");
|
|
||||||
// // return Err(StatusCode::NOT_FOUND);
|
|
||||||
// // }
|
|
||||||
// // }
|
|
||||||
|
|
||||||
// Err(StatusCode::IM_A_TEAPOT)
|
|
||||||
// }
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue