Skip to content

Instantly share code, notes, and snippets.

@harryhanYuhao
Created May 15, 2024 14:46
Show Gist options
  • Save harryhanYuhao/491949dc0ca5b16efced1376b9c30a6b to your computer and use it in GitHub Desktop.
Save harryhanYuhao/491949dc0ca5b16efced1376b9c30a6b to your computer and use it in GitHub Desktop.
// [dependencies]
// actix-web = "4"
// serde = "1.0.202"
// serde_derive = "1.0.202"
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use serde_derive::Deserialize;
#[derive(Deserialize)]
struct Info {
username: String,
}
// get query params
#[get("/")]
async fn index(req: HttpRequest) -> impl Responder {
let params =
web::Query::<Info>::from_query(req.query_string()).unwrap_or(actix_web::web::Query(Info {
username: "Anonymous".into(),
}));
HttpResponse::Ok().body(format!("Hello, {}!", params.username))
}
#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}
async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body("Hey there!")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(index)
.service(echo)
.route("/hey", web::get().to(manual_hello))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment