-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.rs
75 lines (69 loc) · 1.91 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
markup::define! {
Layout<Head: markup::Render, Main: markup::Render>(
head: Head,
main: Main,
) {
@markup::doctype()
html {
head {
@head
style { @markup::raw(include_str!("main.css")) }
}
body {
nav {
a[href = "/"] { "Home" }
}
main {
@main
}
}
}
}
}
#[rocket::get("/")]
fn get() -> rocket::response::content::RawHtml<String> {
let template = Layout {
head: markup::new! {
title { "Home" }
},
main: markup::new! {
h1 { "My contact form" }
form[method = "post"] {
label[for = "name"] { "Name" }
input[id = "name", name = "name", type = "text", required];
label[for = "message"] { "Message" }
textarea[id = "message", name = "message", required, rows = 7] {}
button[type = "submit"] { "Submit" }
}
},
};
rocket::response::content::RawHtml(template.to_string())
}
#[derive(rocket::FromForm)]
struct Contact {
name: String,
message: String,
}
#[rocket::post("/", data = "<form>")]
fn post(form: rocket::form::Form<Contact>) -> rocket::response::content::RawHtml<String> {
let template = Layout {
head: markup::new! {
title { "Message sent! | Home" }
},
main: markup::new! {
h1 { "Message sent!" }
p {
"Thanks for the "
@form.message.chars().count()
" character long message, "
strong { @form.name }
"!"
}
},
};
rocket::response::content::RawHtml(template.to_string())
}
#[rocket::launch]
fn rocket() -> _ {
rocket::build().mount("/", rocket::routes![get, post])
}