src/server/rest.rs: log peer address, use hyper MakeService
This commit is contained in:
parent
6d1f61b208
commit
7fb4f5642a
@ -23,7 +23,7 @@ use tokio::fs::File;
|
|||||||
//use hyper::body::Payload;
|
//use hyper::body::Payload;
|
||||||
use hyper::http::request::Parts;
|
use hyper::http::request::Parts;
|
||||||
use hyper::{Body, Request, Response, StatusCode};
|
use hyper::{Body, Request, Response, StatusCode};
|
||||||
use hyper::service::{Service, NewService};
|
use hyper::service::{Service, MakeService};
|
||||||
use hyper::rt::{Future, Stream};
|
use hyper::rt::{Future, Stream};
|
||||||
use hyper::header;
|
use hyper::header;
|
||||||
|
|
||||||
@ -40,24 +40,46 @@ impl RestServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NewService for RestServer
|
impl MakeService<&tokio_openssl::SslStream<tokio::net::TcpStream>> for RestServer
|
||||||
{
|
{
|
||||||
type ReqBody = Body;
|
type ReqBody = Body;
|
||||||
type ResBody = Body;
|
type ResBody = Body;
|
||||||
type Error = hyper::Error;
|
type Error = Error;
|
||||||
type InitError = hyper::Error;
|
type MakeError = Error;
|
||||||
type Service = ApiService;
|
type Service = ApiService;
|
||||||
type Future = Box<dyn Future<Item = Self::Service, Error = Self::InitError> + Send>;
|
type Future = Box<dyn Future<Item = Self::Service, Error = Self::MakeError> + Send>;
|
||||||
fn new_service(&self) -> Self::Future {
|
fn make_service(&mut self, ctx: &tokio_openssl::SslStream<tokio::net::TcpStream>) -> Self::Future {
|
||||||
Box::new(future::ok(ApiService { api_config: self.api_config.clone() }))
|
let peer = ctx.get_ref().get_ref().peer_addr().unwrap();
|
||||||
|
Box::new(future::ok(ApiService { peer, api_config: self.api_config.clone() }))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MakeService<&tokio::net::TcpStream> for RestServer
|
||||||
|
{
|
||||||
|
type ReqBody = Body;
|
||||||
|
type ResBody = Body;
|
||||||
|
type Error = Error;
|
||||||
|
type MakeError = Error;
|
||||||
|
type Service = ApiService;
|
||||||
|
type Future = Box<dyn Future<Item = Self::Service, Error = Self::MakeError> + Send>;
|
||||||
|
fn make_service(&mut self, ctx: &tokio::net::TcpStream) -> Self::Future {
|
||||||
|
let peer = ctx.peer_addr().unwrap();
|
||||||
|
Box::new(future::ok(ApiService { peer, api_config: self.api_config.clone() }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct ApiService {
|
pub struct ApiService {
|
||||||
|
pub peer: std::net::SocketAddr,
|
||||||
pub api_config: Arc<ApiConfig>,
|
pub api_config: Arc<ApiConfig>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn log_response(method: hyper::Method, path: &str, resp: &Response<Body>) {
|
fn log_response(
|
||||||
|
peer: &std::net::SocketAddr,
|
||||||
|
method: hyper::Method,
|
||||||
|
path: &str,
|
||||||
|
resp: &Response<Body>,
|
||||||
|
) {
|
||||||
|
|
||||||
if resp.extensions().get::<NoLogExtension>().is_some() { return; };
|
if resp.extensions().get::<NoLogExtension>().is_some() { return; };
|
||||||
|
|
||||||
@ -65,43 +87,43 @@ fn log_response(method: hyper::Method, path: &str, resp: &Response<Body>) {
|
|||||||
|
|
||||||
if !(status.is_success() || status.is_informational()) {
|
if !(status.is_success() || status.is_informational()) {
|
||||||
let reason = status.canonical_reason().unwrap_or("unknown reason");
|
let reason = status.canonical_reason().unwrap_or("unknown reason");
|
||||||
let client = "unknown"; // fixme: howto get peer_addr ?
|
|
||||||
|
|
||||||
let mut message = "request failed";
|
let mut message = "request failed";
|
||||||
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
|
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
|
||||||
message = &data.0;
|
message = &data.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, client, message);
|
log::error!("{} {}: {} {}: [client {}] {}", method.as_str(), path, status.as_str(), reason, peer, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service for ApiService {
|
impl Service for ApiService {
|
||||||
type ReqBody = Body;
|
type ReqBody = Body;
|
||||||
type ResBody = Body;
|
type ResBody = Body;
|
||||||
type Error = hyper::Error;
|
type Error = Error;
|
||||||
type Future = Box<dyn Future<Item = Response<Body>, Error = Self::Error> + Send>;
|
type Future = Box<dyn Future<Item = Response<Body>, Error = Self::Error> + Send>;
|
||||||
|
|
||||||
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
|
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future {
|
||||||
let path = req.uri().path().to_owned();
|
let path = req.uri().path().to_owned();
|
||||||
let method = req.method().clone();
|
let method = req.method().clone();
|
||||||
|
|
||||||
|
let peer = self.peer.clone();
|
||||||
Box::new(handle_request(self.api_config.clone(), req).then(move |result| {
|
Box::new(handle_request(self.api_config.clone(), req).then(move |result| {
|
||||||
match result {
|
match result {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
log_response(method, &path, &res);
|
log_response(&peer, method, &path, &res);
|
||||||
Ok::<_, hyper::Error>(res)
|
Ok::<_, Self::Error>(res)
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
||||||
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
||||||
*resp.status_mut() = apierr.code;
|
*resp.status_mut() = apierr.code;
|
||||||
log_response(method, &path, &resp);
|
log_response(&peer, method, &path, &resp);
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
} else {
|
} else {
|
||||||
let mut resp = Response::new(Body::from(err.to_string()));
|
let mut resp = Response::new(Body::from(err.to_string()));
|
||||||
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
log_response(method, &path, &resp);
|
log_response(&peer, method, &path, &resp);
|
||||||
Ok(resp)
|
Ok(resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user