Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --base-path CLI option to override the URL path in the tilejson #1205

Merged
merged 21 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ jobs:
"@

# Replace the default Nginx configuration file
mkdir "C:\tools\nginx-1.25.3\conf\"
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
echo "$nginxConf"
Set-Content -Path "C:\tools\nginx-1.25.3\conf\nginx.conf" -Value $nginxConf
Get-Content -Path "C:\tools\nginx-1.25.3\conf\nginx.conf"
Expand Down
4 changes: 4 additions & 0 deletions docs/src/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ keep_alive: 75
# The socket address to bind [default: 0.0.0.0:3000]
listen_addresses: '0.0.0.0:3000'


# To generate correct tilejson, use this to tell martin the real path if it's running behind a proxy server
base_path: /tiles

# Number of web server workers
worker_processes: 8

Expand Down
4 changes: 3 additions & 1 deletion docs/src/run-with-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Options:

-l, --listen-addresses <LISTEN_ADDRESSES>
The socket address to bind. [DEFAULT: 0.0.0.0:3000]

--base-path <BASE_PATH>
Set TileJSON URL path prefix. Must begin with a `/`. Examples: `/`, `/tiles`
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved

-W, --workers <WORKERS>
Number of web server workers

Expand Down
6 changes: 6 additions & 0 deletions martin/src/args/srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub struct SrvArgs {
pub keep_alive: Option<u64>,
#[arg(help = format!("The socket address to bind. [DEFAULT: {}]", LISTEN_ADDRESSES_DEFAULT), short, long)]
pub listen_addresses: Option<String>,
/// Set TileJSON URL path prefix. Must begin with a `/`. Examples: `/`, `/tiles`
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long)]
pub base_path: Option<String>,
/// Number of web server workers
#[arg(short = 'W', long)]
pub workers: Option<usize>,
Expand Down Expand Up @@ -42,5 +45,8 @@ impl SrvArgs {
if self.preferred_encoding.is_some() {
srv_config.preferred_encoding = self.preferred_encoding;
}
if self.base_path.is_some() {
srv_config.base_path = self.base_path;
}
}
}
6 changes: 5 additions & 1 deletion martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::source::{TileInfoSources, TileSources};
#[cfg(feature = "sprites")]
use crate::sprites::{SpriteConfig, SpriteSources};
use crate::srv::{SrvConfig, RESERVED_KEYWORDS};
use crate::utils::{CacheValue, MainCache, OptMainCache};
use crate::utils::{parse_base_path, CacheValue, MainCache, OptMainCache};
use crate::MartinError::{ConfigLoadError, ConfigParseError, ConfigWriteError, NoSources};
use crate::{IdResolver, MartinResult, OptOneMany};

Expand Down Expand Up @@ -71,6 +71,10 @@ impl Config {
let mut res = UnrecognizedValues::new();
copy_unrecognized_config(&mut res, "", &self.unrecognized);

if let Some(path) = &self.srv.base_path {
self.srv.base_path = Some(parse_base_path(path)?);
}

#[cfg(feature = "postgres")]
for pg in self.postgres.iter_mut() {
res.extend(pg.finalize()?);
Expand Down
4 changes: 4 additions & 0 deletions martin/src/srv/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub const LISTEN_ADDRESSES_DEFAULT: &str = "0.0.0.0:3000";
pub struct SrvConfig {
pub keep_alive: Option<u64>,
pub listen_addresses: Option<String>,
pub base_path: Option<String>,
pub worker_processes: Option<usize>,
pub preferred_encoding: Option<PreferredEncoding>,
}
Expand All @@ -35,6 +36,7 @@ mod tests {
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
preferred_encoding: None,
base_path: None,
}
);
assert_eq!(
Expand All @@ -50,6 +52,7 @@ mod tests {
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
preferred_encoding: Some(PreferredEncoding::Brotli),
base_path: None
}
);
assert_eq!(
Expand All @@ -65,6 +68,7 @@ mod tests {
listen_addresses: some("0.0.0.0:3000"),
worker_processes: Some(8),
preferred_encoding: Some(PreferredEncoding::Brotli),
base_path: None,
}
);
}
Expand Down
96 changes: 85 additions & 11 deletions martin/src/srv/tiles_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use serde::Deserialize;
use tilejson::{tilejson, TileJSON};

use crate::source::{Source, TileSources};

use crate::srv::SrvConfig;
use crate::utils::parse_base_path;
nyurik marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Deserialize)]
pub struct SourceIDsRequest {
pub source_ids: String,
Expand All @@ -26,17 +27,11 @@ async fn get_source_info(
req: HttpRequest,
path: Path<SourceIDsRequest>,
sources: Data<TileSources>,
srv_config: Data<SrvConfig>,
) -> ActixResult<HttpResponse> {
let sources = sources.get_sources(&path.source_ids, None)?.0;

// Get `X-REWRITE-URL` header value, and extract its `path` component.
// If the header is not present or cannot be parsed as a URL, return the request path.
let tiles_path = req
.headers()
.get("x-rewrite-url")
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<Uri>().ok())
.map_or_else(|| req.path().to_owned(), |v| v.path().to_owned());
let tiles_path = generate_tiles_base(&srv_config, &req, &path);

let query_string = req.query_string();
let path_and_query = if query_string.is_empty() {
Expand All @@ -58,6 +53,29 @@ async fn get_source_info(
Ok(HttpResponse::Ok().json(merge_tilejson(&sources, tiles_url)))
}

fn generate_tiles_base(
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
srv_config: &SrvConfig,
req: &HttpRequest,
path: &Path<SourceIDsRequest>,
) -> String {
let mut result = None;
if let Some(path_from_config) = &srv_config.base_path {
let base = format!("{path_from_config}/{0}", &path.source_ids);
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
result = parse_base_path(&base).ok();
} else if let Some(rewrite_url) = req.headers().get("x-rewrite-url") {
if let Ok(url_str) = rewrite_url.to_str() {
result = parse_base_path(&url_str.to_string()).ok();
} else {
result = None;
}
}
if let Some(base_path) = result {
base_path
} else {
req.path().to_owned()
}
}

#[must_use]
pub fn merge_tilejson(sources: &[&dyn Source], tiles_url: String) -> TileJSON {
if sources.len() == 1 {
Expand Down Expand Up @@ -155,10 +173,66 @@ pub fn merge_tilejson(sources: &[&dyn Source], tiles_url: String) -> TileJSON {
pub mod tests {
use std::collections::BTreeMap;

use tilejson::{tilejson, Bounds, VectorLayer};

use super::*;
use crate::srv::server::tests::TestSource;
use actix_web::test::TestRequest;
use tilejson::{Bounds, VectorLayer};

#[actix_web::test]
#[test]
async fn test_generate_tiles_base_path() {
// With config but rewrite url
let srv_config = SrvConfig {
base_path: Some("/tiles".to_string()),
..Default::default()
};
let req = TestRequest::default().uri("/MixedPoints").to_http_request();
let path = Path::from(SourceIDsRequest {
source_ids: "MixedPoints".to_string(),
});

let base = generate_tiles_base(&srv_config, &req, &path);
assert_eq!(base, "/tiles/MixedPoints");

// With rewrite url but config
let srv_config = SrvConfig::default();
let req = TestRequest::default()
.insert_header(("x-rewrite-url", "/tiles/MixedPoints"))
.to_http_request();
let path = Path::from(SourceIDsRequest {
source_ids: "MixedPoints".to_string(),
});

let base = generate_tiles_base(&srv_config, &req, &path);
assert_eq!(base, "/tiles/MixedPoints");

// no config, no rewrite url
let srv_config = SrvConfig::default();
let req = TestRequest::default().uri("/MixedPoints").to_http_request();
let path = Path::from(SourceIDsRequest {
source_ids: "MixedPoints".to_string(),
});

let base = generate_tiles_base(&srv_config, &req, &path);
assert_eq!(base, "/MixedPoints");

// both
let srv_config = SrvConfig {
base_path: Some("/foo".to_string()),
..Default::default()
};
let req = TestRequest::default()
.insert_header(("x-rewrite-url", "/bar"))
.to_http_request();
let path = Path::from(SourceIDsRequest {
source_ids: "MixedPoints".to_string(),
});

let base = generate_tiles_base(&srv_config, &req, &path);
assert_eq!(base, "/foo/MixedPoints");

// todo other edge cases
}

#[test]
fn test_merge_tilejson() {
Expand Down
3 changes: 3 additions & 0 deletions martin/src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub enum MartinError {
#[error("Unable to bind to {1}: {0}")]
BindingError(io::Error, String),

#[error("Base path must be a valid URL path, and must begin with a '/' symbol, but is '{0}'")]
BasePathError(String),

#[error("Unable to load config file {}: {0}", .1.display())]
ConfigLoadError(io::Error, PathBuf),

Expand Down
32 changes: 32 additions & 0 deletions martin/src/utils/utilities.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::io::{Read as _, Write as _};

use crate::MartinError::BasePathError;
use crate::MartinResult;
use actix_web::http::Uri;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;

Expand Down Expand Up @@ -28,3 +31,32 @@ pub fn encode_brotli(data: &[u8]) -> Result<Vec<u8>, std::io::Error> {
encoder.write_all(data)?;
Ok(encoder.into_inner())
}

pub fn parse_base_path(path: &String) -> MartinResult<String> {
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
if !path.starts_with('/') {
return Err(BasePathError(path.to_string()));
}
if let Ok(uri) = path.parse::<Uri>() {
return Ok(uri.path().to_string());
}
Err(BasePathError(path.to_string()))
}

#[cfg(test)]
pub mod tests {
use crate::utils::parse_base_path;
#[test]
fn test_parse_base_path() {
let case1 = "/".to_string();
assert_eq!("/", parse_base_path(&case1).unwrap());

let case2 = String::new();
assert!(parse_base_path(&case2).is_err());

let case3 = "/foo/bar".to_string();
assert_eq!("/foo/bar", parse_base_path(&case3).unwrap());

let case4 = "foo/bar".to_string();
assert!(parse_base_path(&case4).is_err());
sharkAndshark marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading