diff --git a/Cargo.toml b/Cargo.toml index e8a47d8..1ff50a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ section = "web" [dependencies] actix-web = "4" +serde = { version = "1.0", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index e7a11a9..9c13967 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,49 @@ -fn main() { - println!("Hello, world!"); +use actix_web::{web::Json, App, HttpRequest, HttpServer, Responder}; +use serde::Serialize; + +#[derive(Serialize)] +struct EchoHeader { + name: String, + value: String, +} + +#[derive(Serialize)] +struct EchoResponse { + method: String, + path: String, + host: String, + headers: Vec, +} + +impl EchoResponse { + fn new(req: &HttpRequest) -> Self { + let req_uri = req.uri(); + let headers = req + .headers() + .iter() + .map(|(header_name, header_value)| EchoHeader { + name: header_name.to_string(), + value: header_value.to_str().unwrap_or("ERROR").to_string(), + }) + .collect::>(); + 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(request: HttpRequest) -> actix_web::Result { + let echo = EchoResponse::new(&request); + Ok(Json(echo)) +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + HttpServer::new(|| App::new().default_service(actix_web::web::route().to(echo_request))) + .bind(("0.0.0.0", 8080))? + .run() + .await }