Skip to content

Commit

Permalink
feat: add function_sources tilejson endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
stepankuzmin committed Aug 9, 2018
1 parent cc75ab4 commit 95d92c5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
60 changes: 56 additions & 4 deletions src/martin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct State {
}

// TODO: Swagger endpoint
fn index(req: &HttpRequest<State>) -> Result<HttpResponse> {
fn get_table_sources(req: &HttpRequest<State>) -> Result<HttpResponse> {
let state = &req.state();
let table_sources = state
.table_sources
Expand Down Expand Up @@ -54,8 +54,9 @@ fn get_table_source(req: &HttpRequest<State>) -> Result<HttpResponse> {
let tilejson = build_tilejson(
source.clone(),
req.connection_info().clone(),
req.headers().clone(),
);
req.path(),
req.headers(),
).map_err(|e| error::ErrorBadRequest(format!("Can't build TileJSON: {}", e)))?;

Ok(HttpResponse::Ok()
.header("Access-Control-Allow-Origin", "*")
Expand Down Expand Up @@ -113,6 +114,49 @@ fn get_table_source_tile(
.responder())
}

fn get_function_sources(req: &HttpRequest<State>) -> Result<HttpResponse> {
let state = &req.state();
let function_sources = state
.function_sources
.clone()
.ok_or(error::ErrorNotFound("There is no function sources"))?;

Ok(HttpResponse::Ok()
.header("Access-Control-Allow-Origin", "*")
.json(function_sources))
}

fn get_function_source(req: &HttpRequest<State>) -> Result<HttpResponse> {
let state = &req.state();
let function_sources = state
.function_sources
.clone()
.ok_or(error::ErrorNotFound("There is no function sources"))?;

let params = req.match_info();
let source_id = params
.get("source_id")
.ok_or(error::ErrorBadRequest("Invalid function source id"))?;

let source = function_sources
.get(source_id)
.ok_or(error::ErrorNotFound(format!(
"Function source '{}' not found",
source_id
)))?;

let tilejson = build_tilejson(
source.clone(),
req.connection_info().clone(),
req.path(),
req.headers(),
).map_err(|e| error::ErrorBadRequest(format!("Can't build TileJSON: {}", e)))?;

Ok(HttpResponse::Ok()
.header("Access-Control-Allow-Origin", "*")
.json(tilejson))
}

fn get_function_source_tile(
req: &HttpRequest<State>,
) -> Result<Box<Future<Item = HttpResponse, Error = Error>>> {
Expand Down Expand Up @@ -173,13 +217,21 @@ pub fn new(db_sync_arbiter: Addr<DbExecutor>, config: Config) -> App<State> {

App::with_state(state)
.middleware(middleware::Logger::default())
.resource("/index.json", |r| r.method(http::Method::GET).f(index))
.resource("/index.json", |r| {
r.method(http::Method::GET).f(get_table_sources)
})
.resource("/{source_id}.json", |r| {
r.method(http::Method::GET).f(get_table_source)
})
.resource("/{source_id}/{z}/{x}/{y}.pbf", |r| {
r.method(http::Method::GET).f(get_table_source_tile)
})
.resource("/rpc/index.json", |r| {
r.method(http::Method::GET).f(get_function_sources)
})
.resource("/rpc/{source_id}.json", |r| {
r.method(http::Method::GET).f(get_function_source)
})
.resource("/rpc/{source_id}/{z}/{x}/{y}.pbf", |r| {
r.method(http::Method::GET).f(get_function_source_tile)
})
Expand Down
23 changes: 10 additions & 13 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::dev::{ConnectionInfo, Params};
use actix_web::http::header::HeaderMap;
use actix_web::http::header::{HeaderMap, ToStrError};
use martin::Query;
use serde_json;
use std::collections::HashMap;
Expand All @@ -10,23 +10,20 @@ use super::source::{Source, XYZ};
pub fn build_tilejson(
source: Box<dyn Source>,
connection_info: ConnectionInfo,
headers: HeaderMap,
) -> TileJSON {
path: &str,
headers: &HeaderMap,
) -> Result<TileJSON, ToStrError> {
let source_id = source.get_id();

let path = headers
.get("x-rewrite-url")
.map_or(String::from(source_id), |header| {
let parts: Vec<&str> = header.to_str().unwrap().split('.').collect();
let (_, parts_without_extension) = parts.split_last().unwrap();
let path_without_extension = parts_without_extension.join(".");
let (_, path_without_leading_slash) = path_without_extension.split_at(1);

String::from(path_without_leading_slash)
});
.map_or(Ok(path.trim_right_matches(".json")), |header| {
let header_str = header.to_str()?;
Ok(header_str.trim_right_matches(".json"))
})?;

let tiles_url = format!(
"{}://{}/{}/{{z}}/{{x}}/{{y}}.pbf",
"{}://{}{}/{{z}}/{{x}}/{{y}}.pbf",
connection_info.scheme(),
connection_info.host(),
path
Expand All @@ -37,7 +34,7 @@ pub fn build_tilejson(
tilejson_builder.name(source_id);
tilejson_builder.tiles(vec![&tiles_url]);

tilejson_builder.finalize()
Ok(tilejson_builder.finalize())
}

pub fn parse_xyz(params: &Params) -> Result<XYZ, &str> {
Expand Down

0 comments on commit 95d92c5

Please sign in to comment.