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-06-13 01:23:51 +02:00
|
|
|
fn index() -> HttpResponse {
|
|
|
|
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-06-19 20:24:31 +02:00
|
|
|
fn register(form: web::Form<Register>) -> impl Responder {
|
|
|
|
format!("Hello {} from {}!", form.username, form.country)
|
2019-06-13 00:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.route("/", web::get().to(index))
|
|
|
|
.route("/register", web::post().to(register))
|
|
|
|
})
|
2019-06-13 06:32:48 +02:00
|
|
|
.bind("127.0.0.1:8088")
|
|
|
|
.unwrap()
|
2019-06-13 00:14:10 +02:00
|
|
|
.run()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
// </easy-form-handling>
|