diff --git a/commons/src/lib.rs b/commons/src/lib.rs index 5422ebd6a..87e57cbac 100644 --- a/commons/src/lib.rs +++ b/commons/src/lib.rs @@ -1,12 +1,15 @@ -/// Strip any leading and trailing slashes -pub fn parse_path_namespace(path_namespace: &str) -> String { - path_namespace.to_string().trim_matches('/').to_string() +/// Strip all but one leading slash and all trailing slashes +pub fn parse_path_prefix(path_prefix: &str) -> String { + format!("/{}", path_prefix.to_string().trim_matches('/')) } #[cfg(test)] mod tests { #[test] - fn test_parse_path_namespace() { - assert_eq!(super::parse_path_namespace("//a/b/c/"), "a/b/c"); + fn test_parse_path_prefix() { + assert_eq!(super::parse_path_prefix("//a/b/c//"), "/a/b/c"); + assert_eq!(super::parse_path_prefix("/a/b/c/"), "/a/b/c"); + assert_eq!(super::parse_path_prefix("/a/b/c"), "/a/b/c"); + assert_eq!(super::parse_path_prefix("a/b/c"), "/a/b/c"); } } diff --git a/graph-builder/src/config.rs b/graph-builder/src/config.rs index 3f6138978..b2bd02c59 100644 --- a/graph-builder/src/config.rs +++ b/graph-builder/src/config.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use commons::parse_path_namespace; +use commons::parse_path_prefix; use std::net::IpAddr; use std::num::ParseIntError; use std::path::PathBuf; @@ -53,13 +53,13 @@ pub struct Options { #[structopt(long = "credentials-file", parse(from_os_str))] pub credentials_path: Option, - /// Path namespace prefix for all paths. Shall be given without leading and trailing slashes. + /// Path prefix for all paths. #[structopt( - long = "path-namespace", + long = "path-prefix", default_value = "", - parse(from_str = "parse_path_namespace") + parse(from_str = "parse_path_prefix") )] - pub path_namespace: String, + pub path_prefix: String, } fn parse_duration(src: &str) -> Result { diff --git a/graph-builder/src/main.rs b/graph-builder/src/main.rs index 891019ab9..ca6960b78 100644 --- a/graph-builder/src/main.rs +++ b/graph-builder/src/main.rs @@ -43,7 +43,7 @@ fn main() -> Result<(), Error> { let state = graph::State::new(); let addr = (opts.address, opts.port); - let graph_path = format!("/{}/v1/graph", opts.path_namespace); + let app_prefix = opts.path_prefix.clone(); { let state = state.clone(); @@ -51,9 +51,12 @@ fn main() -> Result<(), Error> { } server::new(move || { - App::with_state(state.clone()) + let app_prefix = app_prefix.clone(); + let state = state.clone(); + App::with_state(state) .middleware(Logger::default()) - .route(&graph_path, Method::GET, graph::index) + .prefix(app_prefix) + .route("/v1/graph", Method::GET, graph::index) }) .bind(addr)? .run(); diff --git a/policy-engine/src/config.rs b/policy-engine/src/config.rs index 216e7ac8b..7bc04c0a1 100644 --- a/policy-engine/src/config.rs +++ b/policy-engine/src/config.rs @@ -1,6 +1,6 @@ //! Command-line options for policy-engine. -use commons::parse_path_namespace; +use commons::parse_path_prefix; use hyper::Uri; use std::net::IpAddr; @@ -30,11 +30,11 @@ pub struct Options { #[structopt(long = "metrics_port", default_value = "9081")] pub metrics_port: u16, - /// Path namespace prefix for all paths. Must be given without leading and ending '/' + /// Path prefix for all paths. #[structopt( - long = "path-namespace", + long = "path-prefix", default_value = "", - parse(from_str = "parse_path_namespace") + parse(from_str = "parse_path_prefix") )] - pub path_namespace: String, + pub path_prefix: String, } diff --git a/policy-engine/src/graph.rs b/policy-engine/src/graph.rs index acceeb2a0..5806adb0e 100644 --- a/policy-engine/src/graph.rs +++ b/policy-engine/src/graph.rs @@ -5,9 +5,10 @@ use actix_web::{HttpMessage, HttpRequest, HttpResponse}; use cincinnati::{Graph, CONTENT_TYPE}; use failure::Error; use futures::{future, Future, Stream}; -use hyper::{Body, Client, Request, Uri}; +use hyper::{Body, Client, Request}; use prometheus::{Counter, Histogram}; use serde_json; +use AppState; lazy_static! { static ref HTTP_GRAPH_REQS: Counter = register_counter!( @@ -38,7 +39,7 @@ lazy_static! { } /// Serve Cincinnati graph requests. -pub(crate) fn index(req: HttpRequest) -> Box> { +pub(crate) fn index(req: HttpRequest) -> Box> { HTTP_GRAPH_REQS.inc(); match req.headers().get(header::ACCEPT) { Some(entry) if entry == HeaderValue::from_static(CONTENT_TYPE) => { @@ -84,8 +85,3 @@ pub(crate) fn index(req: HttpRequest) -> Box Result<(), Error> { .init(); // Metrics service. - server::new(move || { + server::new(|| { App::new() .middleware(Logger::default()) .route("/metrics", Method::GET, metrics::serve) @@ -54,15 +54,17 @@ fn main() -> Result<(), Error> { .start(); // Main service. - let state = graph::State { - upstream: opts.upstream, + let state = AppState { + upstream: opts.upstream.clone(), + path_prefix: opts.path_prefix.clone(), }; - let graph_path = format!("/{}/v1/graph", opts.path_namespace); server::new(move || { + let app_prefix = state.path_prefix.clone(); App::with_state(state.clone()) .middleware(Logger::default()) - .route(&graph_path, Method::GET, graph::index) + .prefix(app_prefix) + .route("/v1/graph", Method::GET, graph::index) }) .bind((opts.address, opts.port))? .start(); @@ -70,3 +72,9 @@ fn main() -> Result<(), Error> { sys.run(); Ok(()) } + +#[derive(Debug, Clone)] +pub struct AppState { + pub upstream: hyper::Uri, + pub path_prefix: String, +}