-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.rs
81 lines (71 loc) · 2.05 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
76
77
78
79
80
81
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
}
}
}
}
}
async fn get() -> impl axum::response::IntoResponse {
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" }
}
},
};
axum::response::Html(template.to_string())
}
#[derive(serde::Deserialize)]
struct Contact {
name: String,
message: String,
}
async fn post(form: axum::extract::Form<Contact>) -> impl axum::response::IntoResponse {
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 }
"!"
}
},
};
axum::response::Html(template.to_string())
}
#[tokio::main]
async fn main() {
let app = axum::Router::new().route("/", axum::routing::get(get).post(post));
let address = "0.0.0.0:3000";
eprintln!("starting on http://{}", address);
let listener = tokio::net::TcpListener::bind(address).await.unwrap();
axum::serve(listener, app).await.unwrap();
}