From 0c5e250800f1b8f50ea832bf414a5a7cdfe944a4 Mon Sep 17 00:00:00 2001 From: morguldir Date: Tue, 26 Jul 2022 09:05:34 +0200 Subject: [PATCH] Add support for prepending a path to all routes for the server (#484) * Add support for prepending a path to all routes * Don't nest if there is no path provided Co-authored-by: Conrad Ludgate * Change the default for the path variable * run cargo-fmt Co-authored-by: Conrad Ludgate --- atuin-server/src/router.rs | 25 ++++++++++++++++--------- atuin-server/src/settings.rs | 2 ++ docs/server.md | 6 ++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/atuin-server/src/router.rs b/atuin-server/src/router.rs index 9b525e0..08eea99 100644 --- a/atuin-server/src/router.rs +++ b/atuin-server/src/router.rs @@ -57,7 +57,7 @@ async fn teapot() -> impl IntoResponse { (http::StatusCode::IM_A_TEAPOT, "☕") } pub fn router(postgres: Postgres, settings: Settings) -> Router { - Router::new() + let routes = Router::new() .route("/", get(handlers::index)) .route("/sync/count", get(handlers::history::count)) .route("/sync/history", get(handlers::history::list)) @@ -65,12 +65,19 @@ pub fn router(postgres: Postgres, settings: Settings) -> Router { .route("/history", post(handlers::history::add)) .route("/user/:username", get(handlers::user::get)) .route("/register", post(handlers::user::register)) - .route("/login", post(handlers::user::login)) - .fallback(teapot.into_service()) - .layer( - ServiceBuilder::new() - .layer(TraceLayer::new_for_http()) - .layer(Extension(postgres)) - .layer(Extension(settings)), - ) + .route("/login", post(handlers::user::login)); + + let path = settings.path.as_str(); + if path.is_empty() { + routes + } else { + Router::new().nest(path, routes) + } + .fallback(teapot.into_service()) + .layer( + ServiceBuilder::new() + .layer(TraceLayer::new_for_http()) + .layer(Extension(postgres)) + .layer(Extension(settings)), + ) } diff --git a/atuin-server/src/settings.rs b/atuin-server/src/settings.rs index b0e07b7..8da0c0a 100644 --- a/atuin-server/src/settings.rs +++ b/atuin-server/src/settings.rs @@ -11,6 +11,7 @@ pub const HISTORY_PAGE_SIZE: i64 = 100; pub struct Settings { pub host: String, pub port: u16, + pub path: String, pub db_uri: String, pub open_registration: bool, pub max_history_length: usize, @@ -35,6 +36,7 @@ impl Settings { .set_default("port", 8888)? .set_default("open_registration", false)? .set_default("max_history_length", 8192)? + .set_default("path", "")? .add_source( Environment::with_prefix("atuin") .prefix_separator("_") diff --git a/docs/server.md b/docs/server.md index 79e6223..b4f41dd 100644 --- a/docs/server.md +++ b/docs/server.md @@ -67,6 +67,12 @@ Defaults to `false`. A valid postgres URI, where the user and history data will be saved to. +### path + +A path to prepend to all the routes of the server. Any empty string means that nothing will be prepended. + +Defaults to `""` + ## Container deployment instructions You can deploy you own atuin server in a container: