settings: Settings,
}
+#[derive(Deserialize)]
+struct FormData {
+ name: String,
+ notes: String,
+}
+
+async fn index_post(
+ data: web::Data<AppState>,
+ form: web::Form<FormData>,
+) -> Result<impl Responder, Error> {
+ let mut s = String::new();
+
+ s.push_str("<html>\n");
+ s.push_str("<body>\n");
+
+ s.push_str(r#"<form method="post" action="/">"#);
+
+ let mut f = zakform::Form::new();
+
+ let mut fi = zakform::Field::new(zakform::FType::Text, "name");
+ fi.set_label("Name");
+ fi.set_value(form.name.as_str());
+ f.add_field(fi);
+
+ let mut fi = zakform::Field::new(zakform::FType::TextArea, "notes");
+ fi.set_label("Notes");
+ fi.set_rows(15);
+ fi.set_value(form.notes.as_str());
+ f.add_field(fi);
+
+ let fs = f.fields();
+ for fi in fs {
+ s.push_str(fi.render().as_str());
+ }
+
+ s.push_str(r#"<button type="submit">Send</button>"#);
+
+ s.push_str("</form>\n");
+
+ s.push_str("</body>\n");
+ s.push_str("</html>");
+
+ Ok(HttpResponse::Ok().body(s))
+}
+
async fn index(
data: web::Data<AppState>,
) -> Result<impl Responder, Error> {
let mut s = String::new();
+ s.push_str("<html>\n");
+ s.push_str("<body>\n");
+
+ s.push_str(r#"<form method="post" action="/">"#);
+
let mut f = zakform::Form::new();
let mut fi = zakform::Field::new(zakform::FType::Text, "name");
fi.set_label("Name");
- fi.set_help("Must not be empty.");
- s.push_str(fi.render().as_str());
f.add_field(fi);
let mut fi = zakform::Field::new(zakform::FType::TextArea, "notes");
fi.set_label("Notes");
fi.set_rows(15);
- s.push_str(fi.render().as_str());
f.add_field(fi);
+ let fs = f.fields();
+ for fi in fs {
+ s.push_str(fi.render().as_str());
+ }
+
+ s.push_str(r#"<button type="submit">Send</button>"#);
+
+ s.push_str("</form>\n");
+
+ s.push_str("</body>\n");
+ s.push_str("</html>");
+
Ok(HttpResponse::Ok().body(s))
}
settings: Settings::new().unwrap(),
}))
.wrap(middleware::Logger::default()) // enable logger
- .service(web::resource("/").route(web::get().to(index)))
+ .service(web::resource("/")
+ .route(web::get().to(index))
+ .route(web::post().to(index_post)))
.service(web::scope("").wrap(error_handlers()))
})
.bind((appstate.settings.http.bind_ip, appstate.settings.http.bind_port))?