add tilejson endpoint

This commit is contained in:
Stepan Kuzmin 2018-03-28 19:15:00 +03:00
parent 30e4d76808
commit e3b2d5042b
4 changed files with 36 additions and 13 deletions

12
Cargo.lock generated
View File

@ -757,6 +757,7 @@ dependencies = [
"serde 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"tilejson 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -1393,6 +1394,16 @@ dependencies = [
"unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "tilejson"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "time"
version = "0.1.39"
@ -1895,6 +1906,7 @@ dependencies = [
"checksum termcolor 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "56c456352e44f9f91f774ddeeed27c1ec60a2455ed66d692059acfb1d731bda1"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963"
"checksum tilejson 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2117abf55755175b050f820472ddcd7762d7c60764af8bdfa828734276841847"
"checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098"
"checksum tokio 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "65bd27f59c223e7c9e406bcdadb453e143bcf1242e162ae6c0f0eb6d14487306"
"checksum tokio-core 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "799492ccba3d8ed5e41f2520a7cfd504cb65bbfe5fbbbd0012e335ae5f188051"

View File

@ -15,4 +15,5 @@ r2d2 = "0.8"
r2d2_postgres = "0.14"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_json = "1.0"
tilejson = "0.1"

View File

@ -11,6 +11,7 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate tilejson;
use actix_web::HttpServer;
use actix::{Actor, Addr, Syn, SyncArbiter};

View File

@ -4,6 +4,7 @@ use futures::future::Future;
use mapbox_expressions_to_sql;
use std::cell::RefCell;
use std::rc::Rc;
use tilejson::TileJSONBuilder;
use super::messages;
use super::db::DbExecutor;
@ -36,25 +37,30 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Err
}
fn source(req: HttpRequest<State>) -> Result<HttpResponse> {
let sources = req.state().sources.borrow();
let source_id = req.match_info()
.get("source")
let source_ids = req.match_info()
.get("sources")
.ok_or(error::ErrorBadRequest("invalid source"))?;
let source = sources.get(source_id).ok_or(error::ErrorNotFound(format!(
"source {} not found",
source_id
)))?;
let mut tilejson_builder = TileJSONBuilder::new();
tilejson_builder.scheme("tms");
tilejson_builder.name(&source_ids);
Ok(httpcodes::HTTPOk.build().json(source)?)
let tiles_url = format!(
"{}/{{z}}/{{x}}/{{y}}.pbf",
req.url_for("tilejson", &[source_ids]).unwrap()
);
tilejson_builder.tiles(vec![&tiles_url]);
let tilejson = tilejson_builder.finalize();
Ok(httpcodes::HTTPOk.build().json(tilejson)?)
}
fn tile(req: HttpRequest<State>) -> Result<Box<Future<Item = HttpResponse, Error = Error>>> {
let sources = &req.state().sources.borrow();
let source_id = req.match_info()
.get("source")
.get("sources")
.ok_or(error::ErrorBadRequest("invalid source"))?;
let source = sources.get(source_id).ok_or(error::ErrorNotFound(format!(
@ -133,8 +139,11 @@ pub fn new(
.middleware(middleware::Logger::default())
.middleware(cors)
.resource("/index.json", |r| r.method(Method::GET).a(index))
.resource("/{source}.json", |r| r.method(Method::GET).f(source))
.resource("/{source}/{z}/{x}/{y}.pbf", |r| {
.resource("/{sources}.json", |r| {
r.name("tilejson");
r.method(Method::GET).f(source)
})
.resource("/{sources}/{z}/{x}/{y}.pbf", |r| {
r.method(Method::GET).f(tile)
})
}