feat engines: Routing to engine accepts mount_path via Extension

also changes how kv obtains the `mount_path`
This commit is contained in:
Laurenz 2024-05-05 18:04:25 +02:00
parent 6755e61163
commit 4b88966e81
2 changed files with 25 additions and 19 deletions

View file

@ -5,7 +5,7 @@ use axum::{
extract::{Request, State}, extract::{Request, State},
http::{StatusCode, Uri}, http::{StatusCode, Uri},
response::{IntoResponse, Response}, response::{IntoResponse, Response},
Router, Extension, Router,
}; };
use log::*; use log::*;
use tower::Service; use tower::Service;
@ -17,7 +17,7 @@ use crate::storage::DatabaseDriver;
/// and the routers for each engine /// and the routers for each engine
struct EngineMapperState { struct EngineMapperState {
pool: DatabaseDriver, pool: DatabaseDriver,
kv_v2: Router<String>, kv_v2: Router,
} }
/// Secret engine router /// Secret engine router
@ -52,9 +52,12 @@ async fn engine_handler(
} }
/// Helper function to call the appropriate router with the request /// Helper function to call the appropriate router with the request
async fn call_router(engine: Router<String>, mount_path: String, req: Request) -> Response { async fn call_router(engine: Router, mount_path: String, mut req: Request) -> Response {
let rui = req.uri().path().replace(&mount_path, "").parse().unwrap();
*req.uri_mut() = rui;
engine engine
.with_state(mount_path) .layer(Extension(mount_path))
.call(req) .call(req)
.await .await
.into_response() .into_response()

View file

@ -9,24 +9,26 @@ mod tests;
use crate::engines::kv::structs::KvSecret; use crate::engines::kv::structs::KvSecret;
use crate::{engines::kv::logic::body_to_json, storage::DatabaseDriver}; use crate::{engines::kv::logic::body_to_json, storage::DatabaseDriver};
use axum::extract::State;
use axum::Extension;
use axum::{extract::Path, routing::*, Router}; use axum::{extract::Path, routing::*, Router};
pub fn kv_router(pool: DatabaseDriver) -> Router<String> { pub fn kv_router(pool: DatabaseDriver) -> Router {
Router::new() Router::new()
.route("/:mount_path/config", get(get_config)) .route("/config", get(get_config))
.route("/:mount_path/config", post(post_config)) .route("/config", post(post_config))
.route("/:mount_path/data/*path", get(get_data)) .route("/data/*path", get(get_data))
// .route("/:mount_path/data/*path/", get(get_data)) // .route("/:mount_path/data/*path/", get(get_data))
.route("/:mount_path/data/*path", post(post_data)) .route("/data/*path", post(post_data))
.route("/:mount_path/data/*path", delete(delete_data)) .route("/data/*path", delete(delete_data))
.route("/:mount_path/delete/*path", post(delete_path)) .route("/delete/*path", post(delete_path))
.route("/:mount_path/destroy/*path", post(destroy_path)) .route("/destroy/*path", post(destroy_path))
.route("/:mount_path/metadata/*path", get(get_meta)) .route("/metadata/*path", get(get_meta))
// .route("/:mount_path/metadata/*path/", get(get_meta)) // .route("/:mount_path/metadata/*path/", get(get_meta))
.route("/:mount_path/metadata/*path", post(post_meta)) .route("/metadata/*path", post(post_meta))
.route("/:mount_path/metadata/*path", delete(delete_meta)) .route("/metadata/*path", delete(delete_meta))
.route("/:mount_path/subkeys/*path", get(get_subkeys)) .route("/subkeys/*path", get(get_subkeys))
.route("/:mount_path/undelete/*path", post(post_undelete)) .route("/undelete/*path", post(post_undelete))
.with_state(pool) .with_state(pool)
} }
@ -43,7 +45,8 @@ async fn get_data() -> &'static str {
} }
async fn post_data( async fn post_data(
Path((mount_path, kv_path)): Path<(String, String)>, Path(kv_path): Path<String>,
Extension(mount_path): Extension<String>,
body: String, body: String,
) -> &'static str { ) -> &'static str {
let mut body_json = body_to_json(body); let mut body_json = body_to_json(body);
@ -56,7 +59,7 @@ async fn post_data(
"Secret: {}, Content: {}, Version: {}, path: {}", "Secret: {}, Content: {}, Version: {}, path: {}",
kv_path, kv_path,
secret.data, secret.data,
secret.version.unwrap(), secret.version.unwrap_or(0),
mount_path, mount_path,
); );
"RoutingTest foo successful" "RoutingTest foo successful"