mirror of
https://github.com/actix/actix-website
synced 2025-06-27 07:29:02 +02:00
migrate to docusaurus (v2) (#266)
Co-authored-by: ibraheemdev <ibrah1440@gmail.com>
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
// <easy-form-handling>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||
use serde::Deserialize;
|
||||
// <easy-form-handling>
|
||||
use actix_web::web::{Either, Json, Form};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Register {
|
||||
@ -8,25 +9,33 @@ struct Register {
|
||||
country: String,
|
||||
}
|
||||
|
||||
// register form is JSON
|
||||
async fn json_register(form: web::Json<Register>) -> impl Responder {
|
||||
format!("Hello {} from {}!", form.username, form.country)
|
||||
}
|
||||
|
||||
// register form can be either JSON or URL-encoded
|
||||
async fn register(form: Either<Json<Register>, Form<Register>>) -> impl Responder {
|
||||
let Register { username, country } = form.into_inner();
|
||||
format!("Hello {username} from {country}!")
|
||||
}
|
||||
// </easy-form-handling>
|
||||
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok()
|
||||
.content_type("text/html; charset=utf-8")
|
||||
.body(include_str!("../static/form.html"))
|
||||
}
|
||||
|
||||
async fn register(form: web::Form<Register>) -> impl Responder {
|
||||
format!("Hello {} from {}!", form.username, form.country)
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/register", web::post().to(register))
|
||||
.route("/json_register", web::post().to(json_register))
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
// </easy-form-handling>
|
||||
|
Reference in New Issue
Block a user