Skip to content

Commit

Permalink
Use web::block in web server
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola authored and pka committed Sep 23, 2020
1 parent cd69ca6 commit fbf1711
Showing 1 changed file with 42 additions and 19 deletions.
61 changes: 42 additions & 19 deletions t-rex-webserver/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ use actix_web::dev::BodyEncoding;
use actix_web::http::{header, ContentEncoding};
use actix_web::middleware::Compress;
use actix_web::{guard, middleware, web, App, HttpRequest, HttpResponse, HttpServer, Result};
use actix_web::web;
use clap::ArgMatches;
use log::Level;
use num_cpus;
use open;
use std::collections::HashMap;
use std::convert::Infallible;
use std::str;
use std::str::FromStr;

Expand Down Expand Up @@ -94,7 +96,11 @@ async fn tileset_tilejson(
tileset: web::Path<String>,
req: HttpRequest,
) -> Result<HttpResponse> {
let json = service.get_tilejson(&req_baseurl(&req), &tileset).unwrap();
let url = req_baseurl(&req);
let json =
web::block::<_, _, Infallible>(move || Ok(service.get_tilejson(&url, &tileset).unwrap()))
.await
.unwrap();
Ok(HttpResponse::Ok().json(json))
}

Expand All @@ -111,7 +117,10 @@ async fn tileset_metadata_json(
service: web::Data<MvtService>,
tileset: web::Path<String>,
) -> Result<HttpResponse> {
let json = service.get_mbtiles_metadata(&tileset).unwrap();
let json =
web::block::<_, _, Infallible>(move || Ok(service.get_mbtiles_metadata(&tileset).unwrap()))
.await
.unwrap();
Ok(HttpResponse::Ok().json(json))
}

Expand All @@ -121,7 +130,8 @@ async fn tile_pbf(
params: web::Path<(String, u8, u32, u32)>,
req: HttpRequest,
) -> Result<HttpResponse> {
let tileset = &params.as_ref().0;
let params = params.into_inner();
let tileset = params.0;
let z = params.1;
let x = params.2;
let y = params.3;
Expand All @@ -135,21 +145,29 @@ async fn tile_pbf(
.and_then(|headerstr| Some(headerstr.contains("gzip")))
})
.unwrap_or(false);
let tile = service.tile_cached(tileset, x, y, z, gzip, None);
let cache_max_age = config.webserver.cache_control_max_age.unwrap_or(300);
let tile = web::block::<_, _, Infallible>(move || {
Ok(service.tile_cached(&tileset, x, y, z, gzip, None))
})
.await;

let resp = if let Some(tile) = tile {
HttpResponse::Ok()
.content_type("application/x-protobuf")
.if_true(gzip, |r| {
// data is already gzip compressed
r.encoding(ContentEncoding::Identity)
.header(header::CONTENT_ENCODING, "gzip");
})
.header(header::CACHE_CONTROL, format!("max-age={}", cache_max_age))
.body(tile) // TODO: chunked response
} else {
HttpResponse::NoContent().finish()
let resp = match tile {
Ok(Some(tile)) => {
let cache_max_age = config.webserver.cache_control_max_age.unwrap_or(300);
HttpResponse::Ok()
.content_type("application/x-protobuf")
.if_true(gzip, |r| {
// data is already gzip compressed
r.encoding(ContentEncoding::Identity)
.header(header::CONTENT_ENCODING, "gzip");
})
.header(header::CACHE_CONTROL, format!("max-age={}", cache_max_age))
.body(tile) // TODO: chunked response
}
Ok(None) => HttpResponse::NoContent().finish(),
Err(e) => {
error!("{}", e);
HttpResponse::InternalServerError().finish()
}
};
Ok(resp)
}
Expand Down Expand Up @@ -215,8 +233,13 @@ pub async fn webserver(args: ArgMatches<'static>) -> std::io::Result<()> {
let static_dirs = config.webserver.static_.clone();

let mut service = service_from_args(&config, &args);
service.prepare_feature_queries();
service.init_cache();
let service = web::block::<_, _, Infallible>(|| {
service.prepare_feature_queries();
service.init_cache();
Ok(service)
})
.await
.unwrap();

let server = HttpServer::new(move || {
let mut app = App::new()
Expand Down

0 comments on commit fbf1711

Please sign in to comment.