Add ext/web forms (design §5.5): fields, validate, submit, GET|POST /_form.
Also fix static links (URLs with dots) being misread as db fields. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
9599303c4c
commit
ee019fb1c2
13 files changed
+890
-46
No files matched your search
+64
-8
@@ -1,9 +1,10 @@
|
||||
//! Blocking HTTP listen: page shell, `/_part/{id}`, optional `/admin`.
|
||||
//! Blocking HTTP listen: page shell, `/_part/{id}`, `/_form/{id}`, optional `/admin`.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::{Path, State};
|
||||
use axum::extract::{Form, Path, State};
|
||||
use axum::response::{Html, IntoResponse, Redirect, Response};
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
@@ -11,6 +12,7 @@ use serde_json::{json, Map, Value};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
use crate::db;
|
||||
use crate::form;
|
||||
use crate::render;
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -18,6 +20,7 @@ struct AppState {
|
||||
page: Value,
|
||||
db_url: Option<String>,
|
||||
admin: bool,
|
||||
forms: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
pub fn listen(
|
||||
@@ -26,16 +29,19 @@ pub fn listen(
|
||||
host: &str,
|
||||
port: u16,
|
||||
admin: bool,
|
||||
forms: HashMap<String, Value>,
|
||||
) -> Result<Value, String> {
|
||||
let state = Arc::new(AppState {
|
||||
page: page.clone(),
|
||||
db_url: db_url.map(|s| s.to_string()),
|
||||
admin,
|
||||
forms,
|
||||
});
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(home))
|
||||
.route("/_part/{id}", get(part))
|
||||
.route("/_form/{id}", get(form_get).post(form_post))
|
||||
.route("/admin", get(admin_home))
|
||||
.route("/admin/{table}", get(admin_table))
|
||||
.with_state(state);
|
||||
@@ -73,6 +79,45 @@ async fn part(State(st): State<Arc<AppState>>, Path(id): Path<String>) -> Respon
|
||||
Html(html).into_response()
|
||||
}
|
||||
|
||||
async fn form_get(State(st): State<Arc<AppState>>, Path(id): Path<String>) -> Response {
|
||||
let Some(frm) = st.forms.get(&id) else {
|
||||
return Html(format!("<p>unknown form {id}</p>")).into_response();
|
||||
};
|
||||
Html(form::render(frm, &id, None, None)).into_response()
|
||||
}
|
||||
|
||||
async fn form_post(
|
||||
State(st): State<Arc<AppState>>,
|
||||
Path(id): Path<String>,
|
||||
Form(posted): Form<HashMap<String, String>>,
|
||||
) -> Response {
|
||||
let Some(frm) = st.forms.get(&id) else {
|
||||
return Html(format!("<p>unknown form {id}</p>")).into_response();
|
||||
};
|
||||
let Some(url) = st.db_url.as_deref() else {
|
||||
return Html(String::from("<p>no database</p>")).into_response();
|
||||
};
|
||||
let mut data = Map::new();
|
||||
for (k, v) in posted {
|
||||
data.insert(k, json!(v));
|
||||
}
|
||||
let data_v = Value::Object(data);
|
||||
match form::submit(frm, &data_v, url) {
|
||||
Ok(res) if res.get("ok").and_then(|v| v.as_bool()).unwrap_or(false) => {
|
||||
let redirect = res
|
||||
.get("redirect")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("/");
|
||||
Redirect::to(redirect).into_response()
|
||||
}
|
||||
Ok(res) => {
|
||||
let errors = res.get("errors");
|
||||
Html(form::render(frm, &id, Some(&data_v), errors)).into_response()
|
||||
}
|
||||
Err(e) => Html(format!("<p>submit error: {}</p>", esc(&e))).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn admin_home(State(st): State<Arc<AppState>>) -> Response {
|
||||
if !st.admin {
|
||||
return Html(String::from("<p>admin disabled</p>")).into_response();
|
||||
@@ -88,6 +133,12 @@ async fn admin_home(State(st): State<Arc<AppState>>) -> Response {
|
||||
for t in &tables {
|
||||
body.push_str(&format!("<li><a href=\"/admin/{t}\">{t}</a></li>"));
|
||||
}
|
||||
if !st.forms.is_empty() {
|
||||
body.push_str("</ul><h2>forms</h2><ul>");
|
||||
for id in st.forms.keys() {
|
||||
body.push_str(&format!("<li><a href=\"/_form/{id}\">{id}</a></li>"));
|
||||
}
|
||||
}
|
||||
body.push_str("</ul><p><a href=\"/\">home</a></p></body></html>");
|
||||
Html(body).into_response()
|
||||
}
|
||||
@@ -110,7 +161,9 @@ async fn admin_table(State(st): State<Arc<AppState>>, Path(table): Path<String>)
|
||||
let mut body = format!(
|
||||
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"/><title>{table}</title></head><body>"
|
||||
);
|
||||
body.push_str(&format!("<h1>{table}</h1><p><a href=\"/admin\">back</a></p><table border=\"1\" cellpadding=\"6\"><thead><tr>"));
|
||||
body.push_str(&format!(
|
||||
"<h1>{table}</h1><p><a href=\"/admin\">back</a></p><table border=\"1\" cellpadding=\"6\"><thead><tr>"
|
||||
));
|
||||
let cols: Vec<String> = rows
|
||||
.first()
|
||||
.and_then(|r| r.as_object())
|
||||
@@ -124,11 +177,14 @@ async fn admin_table(State(st): State<Arc<AppState>>, Path(table): Path<String>)
|
||||
body.push_str("<tr>");
|
||||
let m = row.as_object().cloned().unwrap_or_else(Map::new);
|
||||
for c in &cols {
|
||||
let cell = m.get(c).map(|v| match v {
|
||||
Value::String(s) => s.clone(),
|
||||
Value::Null => String::new(),
|
||||
other => other.to_string(),
|
||||
}).unwrap_or_default();
|
||||
let cell = m
|
||||
.get(c)
|
||||
.map(|v| match v {
|
||||
Value::String(s) => s.clone(),
|
||||
Value::Null => String::new(),
|
||||
other => other.to_string(),
|
||||
})
|
||||
.unwrap_or_default();
|
||||
body.push_str(&format!("<td>{}</td>", esc(&cell)));
|
||||
}
|
||||
body.push_str("</tr>");
|
||||
|
||||
Reference in new issue
Block a user