Add logging via tracing and improve managing JSON request bodies.

This commit is contained in:
Dorian Pula 2024-02-07 15:01:15 -05:00
parent 6a7869a80c
commit 9ecfb109b4
3 changed files with 29 additions and 19 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "mirror-server"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
authors = ["Dorian Pula <dorian.pula@amber-penguin-software.ca>"]
description = "A simple server for mirror HTTP requests for testing."
@ -19,5 +19,9 @@ section = "web"
axum = "0.7"
clap = { version = "4.4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.113"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
serde_json = "1.0"
tokio = { version = "1.36", features = ["macros", "rt-multi-thread"] }
tower = "0.4"
tower-http = { version = "0.5", features = ["trace"] }
tracing = "0.1"
tracing-subscriber = "0.3"

View File

@ -18,7 +18,7 @@ A simple server for mirroring HTTP requests for testing.
Download the DEB file, and install it:
```bash
VERSION=0.2.0
VERSION=0.3.0
REPO_URL=https://code.birch-tree.net/api/packages/dorian/generic/mirror-server/
curl "${REPO_URL}/${VERSION}/mirror-server_${VERSION}_amd64.deb"
sudo dpkg -i "mirror-server-${VERSION}_amd64.deb"
@ -28,6 +28,6 @@ sudo dpkg -i "mirror-server-${VERSION}_amd64.deb"
* [x] Migrate actix to axum for easier maintainability.
* [x] Add mirroring of JSON request.
* [ ] Add logging to server.
* [x] Add logging to server.
* [ ] Add convenience path to access logging for a certain date / records.
* [ ] Dockerize mirror-server for easier distribution.

View File

@ -5,8 +5,10 @@ use axum::{
};
use clap::Parser;
use serde::Serialize;
use serde_json::{json, Value};
use serde_json::Value;
use std::collections::BTreeMap;
use tower_http::trace;
use tracing::{Level, info, warn};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
@ -20,19 +22,14 @@ struct CliArgs {
ips: String,
}
#[derive(Serialize)]
struct EchoHeader {
name: String,
value: String,
}
#[derive(Serialize)]
struct EchoResponse {
method: String,
path: String,
host: String,
headers: BTreeMap<String, String>,
body: Value,
#[serde(skip_serializing_if = "Option::is_none")]
body: Option<Value>,
}
async fn echo_request(
@ -49,14 +46,13 @@ async fn echo_request(
.iter()
.map(|(name, value)| (name.to_string(), value.to_str().unwrap_or("").to_string()))
.collect();
// TODO: Logging the method and path.
let json_body = match body {
None => {
// TODO: Adding logging.
json!({"error": "Non-JSON request sent."})
warn!("Received a non-JSON body.");
None
},
Some(Json(value)) => value,
Some(Json(value)) => Some(value),
};
let response = EchoResponse {
method,
@ -74,10 +70,20 @@ async fn main() {
let listen_on = (cli_args.ips, cli_args.port);
let listen_on = format!("{ip}:{port}", ip = listen_on.0, port = listen_on.1);
let app = Router::new().fallback(echo_request);
// From https://stackoverflow.com/questions/75009289/how-to-enable-logging-tracing-with-axum
tracing_subscriber::fmt()
.with_max_level(Level::INFO)
.init();
let app = Router::new().fallback(echo_request).layer(
trace::TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
);
info!("Starting the mirror-server to listen to {}", listen_on);
let listener = tokio::net::TcpListener::bind(&listen_on)
.await
.unwrap_or_else(|_| panic!("Attempted binding to {}", listen_on));
.unwrap_or_else(|_| panic!("Failed to binding to {}", listen_on));
axum::serve(listener, app)
.await
.expect("Server should start");