-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathform_data.rs
48 lines (35 loc) · 1.42 KB
/
form_data.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#[macro_use] extern crate nickel;
use async_trait::async_trait;
use nickel::{Nickel, HttpRouter, Request, Response, Middleware, MiddlewareResult};
use std::collections::HashMap;
struct Root;
#[async_trait]
impl Middleware<()> for Root {
async fn invoke(&self, _req: &mut Request, res: Response) -> MiddlewareResult {
let mut data = HashMap::new();
data.insert("title","Contact");
return res.render("examples/form_data/views/contact.html", &data).await
}
}
struct Confirmation;
#[async_trait]
impl Middleware<()> for Confirmation {
async fn invoke(&self, req: &mut Request, res: Response) -> MiddlewareResult {
let form_data = try_with!(res, req.form_body().await);
println!("{:?}", form_data);
let mut data = HashMap::new();
data.insert("title", "Confirmation");
data.insert("firstname", form_data.get("firstname").unwrap_or("First name?"));
data.insert("lastname", form_data.get("lastname").unwrap_or("Last name?"));
data.insert("phone", form_data.get("phone").unwrap_or("Phone?"));
data.insert("email", form_data.get("email").unwrap_or("Email?"));
return res.render("examples/form_data/views/confirmation.html", &data).await
}
}
#[tokio::main]
async fn main() {
let mut server = Nickel::new();
server.get("/", Root);
server.post("/confirmation", Confirmation);
server.listen("0.0.0.0:8080").await.unwrap();
}