Break out into using axum extractor.

Add support for JSON extraction.
This commit is contained in:
Dorian Pula 2024-02-07 10:12:40 -05:00
parent 2ed659e3ad
commit 6a7869a80c
3 changed files with 39 additions and 23 deletions

View File

@ -19,4 +19,5 @@ 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"] }

View File

@ -11,6 +11,7 @@ A simple server for mirroring HTTP requests for testing.
* Create a DEB package:
* Install cargo-deb: `cargo install cargo-deb`
* Create the DEB package: `cargo deb`
* Faster builds using [cargo-watch](https://watchexec.github.io/#cargo-watch): `cargo watch -x run`
## Install
@ -25,7 +26,8 @@ sudo dpkg -i "mirror-server-${VERSION}_amd64.deb"
## TODO
* [ ] Migrate actix to axum for easier maintainability.
* [x] Migrate actix to axum for easier maintainability.
* [x] Add mirroring of JSON request.
* [ ] Add logging to server.
* [ ] Add convenience path to access logging for a certain date / records.
* [ ] Dockerize mirror-server for easier distribution.

View File

@ -1,9 +1,11 @@
use axum::{
extract::{Json, Request},
extract::{Host, OriginalUri, Json},
http::{header::HeaderMap, Method},
Router,
};
use clap::Parser;
use serde::Serialize;
use serde_json::{json, Value};
use std::collections::BTreeMap;
#[derive(Parser)]
@ -30,29 +32,40 @@ struct EchoResponse {
path: String,
host: String,
headers: BTreeMap<String, String>,
body: Value,
}
impl EchoResponse {
fn new(req: &Request) -> Self {
let req_uri = req.uri();
let mut headers = BTreeMap::new();
for (header_name, header_value) in req.headers().iter() {
headers.insert(
header_name.to_string(),
header_value.to_str().unwrap_or("ERROR").to_string(),
);
}
EchoResponse {
method: req.method().to_string(),
path: req_uri.path().to_string(),
host: req_uri.host().unwrap_or("").to_string(),
headers,
}
}
}
async fn echo_request(
method: Method,
original_uri: OriginalUri,
host: Host,
header_map: HeaderMap,
body: Option<Json<Value>>
) -> Json<EchoResponse> {
let method = method.to_string();
let host = host.0;
let path = original_uri.path().to_string();
let headers = header_map
.iter()
.map(|(name, value)| (name.to_string(), value.to_str().unwrap_or("").to_string()))
.collect();
// TODO: Logging the method and path.
async fn echo_request(request: Request) -> Json<EchoResponse> {
Json(EchoResponse::new(&request))
let json_body = match body {
None => {
// TODO: Adding logging.
json!({"error": "Non-JSON request sent."})
},
Some(Json(value)) => value,
};
let response = EchoResponse {
method,
host,
path,
headers,
body: json_body,
};
Json(response)
}
#[tokio::main]
@ -64,7 +77,7 @@ async fn main() {
let app = Router::new().fallback(echo_request);
let listener = tokio::net::TcpListener::bind(&listen_on)
.await
.expect(&format!("Attempted binding to {}", listen_on));
.unwrap_or_else(|_| panic!("Attempted binding to {}", listen_on));
axum::serve(listener, app)
.await
.expect("Server should start");