Finish off a mirror service.
This commit is contained in:
parent
1fa6c55d1d
commit
3c34cc05b9
|
@ -17,3 +17,4 @@ section = "web"
|
|||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
|
50
src/main.rs
50
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<EchoHeader>,
|
||||
}
|
||||
|
||||
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::<Vec<EchoHeader>>();
|
||||
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<impl Responder> {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue