Migrate to using axum over actix for web framework.
This commit is contained in:
parent
99a68b9f89
commit
2ed659e3ad
|
@ -19,3 +19,4 @@ Cargo.lock
|
|||
# Added by cargo
|
||||
|
||||
/target
|
||||
.idea
|
||||
|
|
|
@ -16,6 +16,7 @@ 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"] }
|
||||
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
|
||||
|
|
|
@ -25,6 +25,7 @@ sudo dpkg -i "mirror-server-${VERSION}_amd64.deb"
|
|||
|
||||
## TODO
|
||||
|
||||
* [ ] Migrate actix to axum for easier maintainability.
|
||||
* [ ] Add logging to server.
|
||||
* [ ] Add convenience path to access logging for a certain date / records.
|
||||
* [ ] Dockerize mirror-server for easier distribution.
|
||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -1,4 +1,7 @@
|
|||
use actix_web::{web::Json, App, HttpRequest, HttpServer, Responder};
|
||||
use axum::{
|
||||
extract::{Json, Request},
|
||||
Router,
|
||||
};
|
||||
use clap::Parser;
|
||||
use serde::Serialize;
|
||||
use std::collections::BTreeMap;
|
||||
|
@ -30,7 +33,7 @@ struct EchoResponse {
|
|||
}
|
||||
|
||||
impl EchoResponse {
|
||||
fn new(req: &HttpRequest) -> Self {
|
||||
fn new(req: &Request) -> Self {
|
||||
let req_uri = req.uri();
|
||||
let mut headers = BTreeMap::new();
|
||||
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> {
|
||||
let echo = EchoResponse::new(&request);
|
||||
Ok(Json(echo))
|
||||
async fn echo_request(request: Request) -> Json<EchoResponse> {
|
||||
Json(EchoResponse::new(&request))
|
||||
}
|
||||
|
||||
#[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);
|
||||
|
||||
let app = Router::new().fallback(echo_request);
|
||||
let listener = tokio::net::TcpListener::bind(&listen_on)
|
||||
.await
|
||||
.expect(&format!("Attempted binding to {}", listen_on));
|
||||
axum::serve(listener, app)
|
||||
.await
|
||||
.expect("Server should start");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue