feature/share-libs
rs 67 lines 1.68 KB
Raw
1 //! Run with
2 //!
3 //! ```not_rust
4 //! cargo run -p example-form
5 //! ```
6
7 use axum::{extract::Form, response::Html, routing::get, Router};
8 use serde::Deserialize;
9 use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
10
11 #[tokio::main]
12 async fn main() {
13 tracing_subscriber::registry()
14 .with(
15 tracing_subscriber::EnvFilter::try_from_default_env()
16 .unwrap_or_else(|_| "example_form=debug".into()),
17 )
18 .with(tracing_subscriber::fmt::layer())
19 .init();
20
21 // build our application with some routes
22 let app = Router::new().route("/", get(show_form).post(accept_form));
23
24 // run it
25 let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
26 .await
27 .unwrap();
28 tracing::debug!("listening on {}", listener.local_addr().unwrap());
29 axum::serve(listener, app).await.unwrap();
30 }
31
32 async fn show_form() -> Html<&'static str> {
33 Html(
34 r#"
35 <!doctype html>
36 <html>
37 <head></head>
38 <body>
39 <form action="/" method="post">
40 <label for="name">
41 Enter your name:
42 <input type="text" name="name">
43 </label>
44
45 <label>
46 Enter your email:
47 <input type="text" name="email">
48 </label>
49
50 <input type="submit" value="Subscribe!">
51 </form>
52 </body>
53 </html>
54 "#,
55 )
56 }
57
58 #[derive(Deserialize, Debug)]
59 #[allow(dead_code)]
60 struct Input {
61 name: String,
62 email: String,
63 }
64
65 async fn accept_form(Form(input): Form<Input>) {
66 dbg!(&input);
67 }