|
@@ -7,7 +7,7 @@ use ntex::http::header::{CONTENT_TYPE, SERVER};
|
|
|
use ntex::http::{HttpService, KeepAlive, Request, Response, StatusCode};
|
|
|
use ntex::service::{Service, ServiceCtx, ServiceFactory};
|
|
|
use ntex::web::{Error, HttpResponse};
|
|
|
-use ntex::{time::Seconds, util::BoxFuture, util::PoolId};
|
|
|
+use ntex::{time::Seconds, util::PoolId};
|
|
|
|
|
|
mod db;
|
|
|
mod utils;
|
|
@@ -17,52 +17,49 @@ struct App(db::PgConnection);
|
|
|
impl Service<Request> for App {
|
|
|
type Response = Response;
|
|
|
type Error = Error;
|
|
|
- type Future<'f> = BoxFuture<'f, Result<Response, Error>> where Self: 'f;
|
|
|
|
|
|
- fn call<'a>(&'a self, req: Request, _: ServiceCtx<'a, Self>) -> Self::Future<'a> {
|
|
|
- Box::pin(async move {
|
|
|
- match req.path() {
|
|
|
- "/db" => {
|
|
|
- let body = self.0.get_world().await;
|
|
|
- let mut res = HttpResponse::with_body(StatusCode::OK, body.into());
|
|
|
- res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
- res.headers_mut()
|
|
|
- .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
|
|
|
- Ok(res)
|
|
|
- }
|
|
|
- "/fortunes" => {
|
|
|
- let body = self.0.tell_fortune().await;
|
|
|
- let mut res = HttpResponse::with_body(StatusCode::OK, body.into());
|
|
|
- res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
- res.headers_mut()
|
|
|
- .insert(CONTENT_TYPE, utils::HDR_HTML_CONTENT_TYPE);
|
|
|
- Ok(res)
|
|
|
- }
|
|
|
- "/query" => {
|
|
|
- let worlds = self
|
|
|
- .0
|
|
|
- .get_worlds(utils::get_query_param(req.uri().query()))
|
|
|
- .await;
|
|
|
- let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
|
|
|
- res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
- res.headers_mut()
|
|
|
- .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
|
|
|
- Ok(res)
|
|
|
- }
|
|
|
- "/update" => {
|
|
|
- let worlds = self
|
|
|
- .0
|
|
|
- .update(utils::get_query_param(req.uri().query()))
|
|
|
- .await;
|
|
|
- let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
|
|
|
- res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
- res.headers_mut()
|
|
|
- .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
|
|
|
- Ok(res)
|
|
|
- }
|
|
|
- _ => Ok(Response::new(StatusCode::NOT_FOUND)),
|
|
|
+ async fn call(&self, req: Request, _: ServiceCtx<'_, Self>) -> Result<Response, Error> {
|
|
|
+ match req.path() {
|
|
|
+ "/db" => {
|
|
|
+ let body = self.0.get_world().await;
|
|
|
+ let mut res = HttpResponse::with_body(StatusCode::OK, body.into());
|
|
|
+ res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
+ res.headers_mut()
|
|
|
+ .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
|
|
|
+ Ok(res)
|
|
|
}
|
|
|
- })
|
|
|
+ "/fortunes" => {
|
|
|
+ let body = self.0.tell_fortune().await;
|
|
|
+ let mut res = HttpResponse::with_body(StatusCode::OK, body.into());
|
|
|
+ res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
+ res.headers_mut()
|
|
|
+ .insert(CONTENT_TYPE, utils::HDR_HTML_CONTENT_TYPE);
|
|
|
+ Ok(res)
|
|
|
+ }
|
|
|
+ "/query" => {
|
|
|
+ let worlds = self
|
|
|
+ .0
|
|
|
+ .get_worlds(utils::get_query_param(req.uri().query()))
|
|
|
+ .await;
|
|
|
+ let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
|
|
|
+ res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
+ res.headers_mut()
|
|
|
+ .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
|
|
|
+ Ok(res)
|
|
|
+ }
|
|
|
+ "/update" => {
|
|
|
+ let worlds = self
|
|
|
+ .0
|
|
|
+ .update(utils::get_query_param(req.uri().query()))
|
|
|
+ .await;
|
|
|
+ let mut res = HttpResponse::with_body(StatusCode::OK, worlds.into());
|
|
|
+ res.headers_mut().insert(SERVER, utils::HDR_SERVER);
|
|
|
+ res.headers_mut()
|
|
|
+ .insert(CONTENT_TYPE, utils::HDR_JSON_CONTENT_TYPE);
|
|
|
+ Ok(res)
|
|
|
+ }
|
|
|
+ _ => Ok(Response::new(StatusCode::NOT_FOUND)),
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -73,13 +70,12 @@ impl ServiceFactory<Request> for AppFactory {
|
|
|
type Error = Error;
|
|
|
type Service = App;
|
|
|
type InitError = ();
|
|
|
- type Future<'f> = BoxFuture<'f, Result<Self::Service, Self::InitError>>;
|
|
|
|
|
|
- fn create(&self, _: ()) -> Self::Future<'_> {
|
|
|
+ async fn create(&self, _: ()) -> Result<Self::Service, Self::InitError> {
|
|
|
const DB_URL: &str =
|
|
|
"postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world";
|
|
|
|
|
|
- Box::pin(async move { Ok(App(db::PgConnection::connect(DB_URL).await)) })
|
|
|
+ Ok(App(db::PgConnection::connect(DB_URL).await))
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -97,6 +93,8 @@ async fn main() -> std::io::Result<()> {
|
|
|
HttpService::build()
|
|
|
.keep_alive(KeepAlive::Os)
|
|
|
.client_timeout(Seconds(0))
|
|
|
+ .headers_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
|
|
|
+ .payload_read_rate(Seconds::ZERO, Seconds::ZERO, 0)
|
|
|
.h1(AppFactory)
|
|
|
})?
|
|
|
.workers(num_cpus::get())
|