2019-06-13 00:14:10 +02:00
|
|
|
// <easy-form-handling>
|
2019-06-17 05:37:14 +02:00
|
|
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
2019-06-13 00:14:10 +02:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Register {
|
|
|
|
username: String,
|
|
|
|
country: String,
|
|
|
|
}
|
|
|
|
|
2019-12-28 17:24:52 +01:00
|
|
|
async fn index() -> HttpResponse {
|
2019-06-13 01:23:51 +02:00
|
|
|
HttpResponse::Ok()
|
2019-06-13 00:14:10 +02:00
|
|
|
.content_type("text/html; charset=utf-8")
|
2019-06-13 01:23:51 +02:00
|
|
|
.body(include_str!("../static/form.html"))
|
2019-06-13 00:14:10 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2019-06-13 00:14:10 +02:00
|
|
|
}
|
|
|
|
|
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<()> {
|
2019-06-13 00:14:10 +02:00
|
|
|
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")?
|
2019-06-13 00:14:10 +02:00
|
|
|
.run()
|
2019-12-28 17:24:52 +01:00
|
|
|
.await
|
2019-06-13 00:14:10 +02:00
|
|
|
}
|
|
|
|
// </easy-form-handling>
|