This commit is contained in:
Stepan Kuzmin 2018-03-26 16:43:18 +03:00
parent d5e6eaabcb
commit 0f222dbe88

View File

@ -1,6 +1,6 @@
use actix::*;
use actix_web::*;
use futures::future::{result, Future};
use futures::future::Future;
use super::db::{DbExecutor, GetSources, GetTile};
use super::source::Sources;
@ -22,54 +22,51 @@ fn index(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Err
.responder()
}
fn source(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
fn source(req: HttpRequest<State>) -> Result<HttpResponse> {
let sources = &req.state().sources;
let source_id = req.match_info().get("source").unwrap();
let source = sources
.get(source_id)
.ok_or(error::ErrorNotFound(format!(
"source {} not found",
source_id
)))
.and_then(|source| httpcodes::HTTPOk.build().json(source));
let source_id = req.match_info()
.get("source")
.ok_or(error::ErrorBadRequest("invalid source"))?;
result(source).responder()
let source = sources.get(source_id).ok_or(error::ErrorNotFound(format!(
"source {} not found",
source_id
)))?;
Ok(httpcodes::HTTPOk.build().json(source)?)
}
fn tile(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Error>> {
fn tile(req: HttpRequest<State>) -> Result<Box<Future<Item = HttpResponse, Error = Error>>> {
let sources = &req.state().sources;
let source_id = req.match_info().get("source").unwrap();
let source = sources
.get(source_id)
.ok_or(error::ErrorNotFound(format!(
"source {} not found",
source_id
)))
.unwrap();
let source_id = req.match_info()
.get("source")
.ok_or(error::ErrorBadRequest("invalid source"))?;
let source = sources.get(source_id).ok_or(error::ErrorNotFound(format!(
"source {} not found",
source_id
)))?;
let z = req.match_info()
.get("z")
.and_then(|i| i.parse::<u32>().ok())
.ok_or(error::ErrorNotFound("invalid z"))
.unwrap();
.ok_or(error::ErrorBadRequest("invalid z"))?;
let x = req.match_info()
.get("x")
.and_then(|i| i.parse::<u32>().ok())
.ok_or(error::ErrorNotFound("invalid x"))
.unwrap();
.ok_or(error::ErrorBadRequest("invalid x"))?;
let y = req.match_info()
.get("y")
.and_then(|i| i.parse::<u32>().ok())
.ok_or(error::ErrorNotFound("invalid y"))
.unwrap();
.ok_or(error::ErrorBadRequest("invalid y"))?;
let condition = None;
req.state()
Ok(req.state()
.db
.send(GetTile {
z: z,
@ -82,11 +79,10 @@ fn tile(req: HttpRequest<State>) -> Box<Future<Item = HttpResponse, Error = Erro
.and_then(|res| match res {
Ok(tile) => Ok(HttpResponse::Ok()
.content_type("application/x-protobuf")
.body(tile)
.unwrap()),
.body(tile)?),
Err(_) => Ok(httpcodes::HTTPInternalServerError.into()),
})
.responder()
.responder())
}
pub fn new(db_sync_arbiter: Addr<Syn, DbExecutor>, sources: Sources) -> Application<State> {
@ -103,8 +99,8 @@ pub fn new(db_sync_arbiter: Addr<Syn, DbExecutor>, sources: Sources) -> Applicat
.middleware(middleware::Logger::default())
.middleware(cors)
.resource("/index.json", |r| r.method(Method::GET).a(index))
.resource("/{source}.json", |r| r.method(Method::GET).a(source))
.resource("/{source}.json", |r| r.method(Method::GET).f(source))
.resource("/{source}/{z}/{x}/{y}.pbf", |r| {
r.method(Method::GET).a(tile)
r.method(Method::GET).f(tile)
})
}