1
0
mirror of https://github.com/actix/actix-website synced 2024-11-30 19:14:36 +01:00
actix-website/examples/easy-form-handling/src/main.rs

33 lines
785 B
Rust
Raw Normal View History

// <easy-form-handling>
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;
#[derive(Deserialize)]
struct Register {
username: String,
country: String,
}
2019-12-28 17:24:52 +01:00
async fn index() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(include_str!("../static/form.html"))
}
2019-12-28 17:24:52 +01:00
async fn register(form: web::Form<Register>) -> impl Responder {
2019-06-19 20:24:31 +02:00
format!("Hello {} from {}!", form.username, form.country)
}
2020-09-12 17:21:54 +02:00
#[actix_web::main]
2019-12-28 17:24:52 +01:00
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/register", web::post().to(register))
})
2020-09-12 17:21:54 +02:00
.bind("127.0.0.1:8080")?
.run()
2019-12-28 17:24:52 +01:00
.await
}
// </easy-form-handling>