Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions commons/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
10 changes: 5 additions & 5 deletions graph-builder/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,13 +53,13 @@ pub struct Options {
#[structopt(long = "credentials-file", parse(from_os_str))]
pub credentials_path: Option<PathBuf>,

/// 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<Duration, ParseIntError> {
Expand Down
9 changes: 6 additions & 3 deletions graph-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ 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();
thread::spawn(move || graph::run(&opts, &state));
}

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();
Expand Down
10 changes: 5 additions & 5 deletions policy-engine/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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,
}
10 changes: 3 additions & 7 deletions policy-engine/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -38,7 +39,7 @@ lazy_static! {
}

/// Serve Cincinnati graph requests.
pub(crate) fn index(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
pub(crate) fn index(req: HttpRequest<AppState>) -> Box<Future<Item = HttpResponse, Error = Error>> {
HTTP_GRAPH_REQS.inc();
match req.headers().get(header::ACCEPT) {
Some(entry) if entry == HeaderValue::from_static(CONTENT_TYPE) => {
Expand Down Expand Up @@ -84,8 +85,3 @@ pub(crate) fn index(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse,
}
}
}

#[derive(Clone)]
pub struct State {
pub upstream: Uri,
}
18 changes: 13 additions & 5 deletions policy-engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn main() -> Result<(), Error> {
.init();

// Metrics service.
server::new(move || {
server::new(|| {
App::new()
.middleware(Logger::default())
.route("/metrics", Method::GET, metrics::serve)
Expand All @@ -54,19 +54,27 @@ 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();

sys.run();
Ok(())
}

#[derive(Debug, Clone)]
pub struct AppState {
pub upstream: hyper::Uri,
pub path_prefix: String,
}