2019-06-13 00:14:10 +02:00
|
|
|
// <easy-form-handling>
|
|
|
|
use actix_web::{web, App, HttpResponse, HttpServer};
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn register(params: web::Form<Register>) -> actix_web::Result<HttpResponse> {
|
|
|
|
Ok(HttpResponse::Ok().body(format!(
|
|
|
|
"Hello {} from {}!",
|
|
|
|
params.username, params.country
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|