axum-migration #1

Merged
dorian merged 3 commits from axum-migration into main 2024-02-07 15:03:14 -05:00
4 changed files with 75 additions and 39 deletions

1
.gitignore vendored
View File

@ -19,3 +19,4 @@ Cargo.lock
# Added by cargo
/target
.idea

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."
@ -16,6 +16,12 @@ section = "web"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
clap = { version = "4.0", features = ["derive"] }
axum = "0.7"
clap = { version = "4.4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
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

@ -11,13 +11,14 @@ 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
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"
@ -25,6 +26,8 @@ sudo dpkg -i "mirror-server-${VERSION}_amd64.deb"
## TODO
* [ ] Add logging to server.
* [x] Migrate actix to axum for easier maintainability.
* [x] Add mirroring of JSON request.
* [x] Add logging to server.
* [ ] Add convenience path to access logging for a certain date / records.
* [ ] Dockerize mirror-server for easier distribution.

View File

@ -1,7 +1,14 @@
use actix_web::{web::Json, App, HttpRequest, HttpServer, Responder};
use axum::{
extract::{Host, OriginalUri, Json},
http::{header::HeaderMap, Method},
Router,
};
use clap::Parser;
use serde::Serialize;
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)]
@ -15,50 +22,69 @@ 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>,
#[serde(skip_serializing_if = "Option::is_none")]
body: Option<Value>,
}
impl EchoResponse {
fn new(req: &HttpRequest) -> 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(),
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();
let json_body = match body {
None => {
warn!("Received a non-JSON body.");
None
},
Some(Json(value)) => Some(value),
};
let response = EchoResponse {
method,
host,
path,
headers,
}
}
body: json_body,
};
Json(response)
}
async fn echo_request(request: HttpRequest) -> actix_web::Result<impl Responder> {
let echo = EchoResponse::new(&request);
Ok(Json(echo))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
#[tokio::main]
async fn main() {
let cli_args = CliArgs::parse();
let listen_on = (cli_args.ips, cli_args.port);
HttpServer::new(|| App::new().default_service(actix_web::web::route().to(echo_request)))
.bind(listen_on)?
.run()
let listen_on = format!("{ip}:{port}", ip = listen_on.0, port = listen_on.1);
// 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!("Failed to binding to {}", listen_on));
axum::serve(listener, app)
.await
.expect("Server should start");
}