Migrate to using axum over actix for web framework.

This commit is contained in:
Dorian Pula 2024-02-06 15:51:30 -05:00
parent 99a68b9f89
commit 2ed659e3ad
4 changed files with 22 additions and 12 deletions

1
.gitignore vendored
View File

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

View File

@ -16,6 +16,7 @@ section = "web"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-web = "4" axum = "0.7"
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.4", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }

View File

@ -25,6 +25,7 @@ sudo dpkg -i "mirror-server-${VERSION}_amd64.deb"
## TODO ## TODO
* [ ] Migrate actix to axum for easier maintainability.
* [ ] Add logging to server. * [ ] Add logging to server.
* [ ] Add convenience path to access logging for a certain date / records. * [ ] Add convenience path to access logging for a certain date / records.
* [ ] Dockerize mirror-server for easier distribution. * [ ] Dockerize mirror-server for easier distribution.

View File

@ -1,4 +1,7 @@
use actix_web::{web::Json, App, HttpRequest, HttpServer, Responder}; use axum::{
extract::{Json, Request},
Router,
};
use clap::Parser; use clap::Parser;
use serde::Serialize; use serde::Serialize;
use std::collections::BTreeMap; use std::collections::BTreeMap;
@ -30,7 +33,7 @@ struct EchoResponse {
} }
impl EchoResponse { impl EchoResponse {
fn new(req: &HttpRequest) -> Self { fn new(req: &Request) -> Self {
let req_uri = req.uri(); let req_uri = req.uri();
let mut headers = BTreeMap::new(); let mut headers = BTreeMap::new();
for (header_name, header_value) in req.headers().iter() { for (header_name, header_value) in req.headers().iter() {
@ -48,17 +51,21 @@ impl EchoResponse {
} }
} }
async fn echo_request(request: HttpRequest) -> actix_web::Result<impl Responder> { async fn echo_request(request: Request) -> Json<EchoResponse> {
let echo = EchoResponse::new(&request); Json(EchoResponse::new(&request))
Ok(Json(echo))
} }
#[actix_web::main] #[tokio::main]
async fn main() -> std::io::Result<()> { async fn main() {
let cli_args = CliArgs::parse(); let cli_args = CliArgs::parse();
let listen_on = (cli_args.ips, cli_args.port); let listen_on = (cli_args.ips, cli_args.port);
HttpServer::new(|| App::new().default_service(actix_web::web::route().to(echo_request))) let listen_on = format!("{ip}:{port}", ip = listen_on.0, port = listen_on.1);
.bind(listen_on)?
.run() let app = Router::new().fallback(echo_request);
let listener = tokio::net::TcpListener::bind(&listen_on)
.await .await
.expect(&format!("Attempted binding to {}", listen_on));
axum::serve(listener, app)
.await
.expect("Server should start");
} }