Skip to content

Commit

Permalink
rust: move the API to /api
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 6, 2024
1 parent a59adab commit 393d265
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions rust/agama-server/src/web/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tower_http::{compression::CompressionLayer, services::ServeDir, trace::Trace
pub struct MainServiceBuilder {
config: ServiceConfig,
events: EventsSender,
router: Router<ServiceState>,
api_router: Router<ServiceState>,
public_dir: PathBuf,
}

Expand All @@ -38,12 +38,12 @@ impl MainServiceBuilder {
where
P: AsRef<Path>,
{
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()),
}
Expand All @@ -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<T>(self, path: &str, service: T) -> Self
where
Expand All @@ -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
}
}
Expand All @@ -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::<TokenClaims, _>(
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)
Expand Down

0 comments on commit 393d265

Please sign in to comment.