Skip to content

Commit

Permalink
Merge pull request #1 from kjagiello/request-logging
Browse files Browse the repository at this point in the history
Log requests
  • Loading branch information
kjagiello authored May 23, 2021
2 parents d89eb45 + 4d084aa commit 4bf0617
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ path = "src/cli.rs"
doc = false

[dependencies]
chrono = "~0.4.19"
clap = "~2.33.3"
dynfmt = { version = "~0.1.5", features = ["curly"] }
serde = { version = "~1.0.117", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ mod concrete {
}
}

pub fn parse(content: &str) -> ConfigResult<concrete::Config> {
let config = raw::Config::parse(&content)?;
pub fn parse(config: &str) -> ConfigResult<concrete::Config> {
let config = raw::Config::parse(&config)?;
let config = concrete::Config::from(config)?;
Ok(config)
}
40 changes: 31 additions & 9 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use chrono;
use log::{error, info};
use std::net::SocketAddr;
use std::sync::Arc;

use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Error, Response, Server};
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};

pub struct Config {
pub addr: SocketAddr,
Expand All @@ -14,24 +16,44 @@ struct Context {
pub pac: String,
}

fn log_request<T, U>(remote_addr: &SocketAddr, req: &Request<T>, response: &Response<U>) {
info!(
"{} [{}] \"{} {} {:?}\" {}",
remote_addr.ip(),
chrono::offset::Local::now(),
req.method(),
req.uri(),
req.version(),
response.status().as_u16(),
);
}

#[tokio::main]
pub async fn serve(config: Config) {
let ctx = Arc::new(Context { pac: config.pac });

let make_service = make_service_fn(move |_| {
let make_service = make_service_fn(move |conn: &AddrStream| {
let ctx = ctx.clone();
let remote_addr = conn.remote_addr();

async move {
Ok::<_, Error>(service_fn(move |_req| {
Ok::<_, Error>(service_fn(move |req| {
let ctx = ctx.clone();

async move {
let response = Response::builder()
.header("Content-Type", "application/x-ns-proxy-autoconfig")
.body(Body::from(ctx.pac.clone()))
.unwrap();
let res = match *req.method() {
Method::GET => Response::builder()
.header("Content-Type", "application/x-ns-proxy-autoconfig")
.body(Body::from(ctx.pac.clone()))
.unwrap(),
_ => Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.body(Body::empty())
.unwrap(),
};

Ok::<_, Error>(response)
log_request(&remote_addr, &req, &res);
Ok::<_, Error>(res)
}
}))
}
Expand All @@ -42,6 +64,6 @@ pub async fn serve(config: Config) {
info!("Quit the server with CTRL-C");

if let Err(e) = server.await {
error!(target: "server", "server error: {}", e);
error!("Server error: {}", e);
}
}

0 comments on commit 4bf0617

Please sign in to comment.