From 313e9edb19ac6ae87d44e0c747b5d51d722c649d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Wed, 6 Mar 2024 09:48:05 +0000 Subject: [PATCH] rust: move the API to /api --- rust/agama-server/src/web/service.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/rust/agama-server/src/web/service.rs b/rust/agama-server/src/web/service.rs index 6d918da10e..cf58665cc0 100644 --- a/rust/agama-server/src/web/service.rs +++ b/rust/agama-server/src/web/service.rs @@ -25,7 +25,7 @@ use tower_http::{compression::CompressionLayer, services::ServeDir, trace::Trace pub struct MainServiceBuilder { config: ServiceConfig, events: EventsSender, - router: Router, + api_router: Router, public_dir: PathBuf, } @@ -38,12 +38,12 @@ impl MainServiceBuilder { where P: AsRef, { - let router = Router::new().route("/ws", get(super::ws::ws_handler)); + let api_router = Router::new().route("/ws", get(super::ws::ws_handler)); let config = ServiceConfig::default(); Self { events, - router, + api_router, config, public_dir: PathBuf::from(public_dir.as_ref()), } @@ -55,7 +55,7 @@ impl MainServiceBuilder { /// Add an authenticated service. /// - /// * `path`: Path to mount the service on. + /// * `path`: Path to mount the service under `/api`. /// * `service`: Service to mount on the given `path`. pub fn add_service(self, path: &str, service: T) -> Self where @@ -64,7 +64,7 @@ impl MainServiceBuilder { T::Future: Send + 'static, { Self { - router: self.router.nest_service(path, service), + api_router: self.api_router.nest_service(path, service), ..self } } @@ -75,14 +75,18 @@ impl MainServiceBuilder { events: self.events, }; - let serve = ServeDir::new(self.public_dir); - self.router + let api_router = self + .api_router .route_layer(middleware::from_extractor_with_state::( state.clone(), )) - .nest_service("/", serve) .route("/ping", get(super::http::ping)) - .route("/authenticate", post(super::http::authenticate)) + .route("/authenticate", post(super::http::authenticate)); + + let serve = ServeDir::new(self.public_dir); + Router::new() + .nest_service("/", serve) + .nest("/api", api_router) .layer(TraceLayer::new_for_http()) .layer(CompressionLayer::new().br(true)) .with_state(state)