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