summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2024-11-17 14:55:05 +0100
committerMathias Magnusson <mathias@magnusson.space>2024-11-17 14:55:05 +0100
commit72440d0e3b2a8224b65491202798f5c0b18a2dde (patch)
treee75675956ab77bf635ade8f2599dfa060a66f83c /examples
parent14db9705ae1c2a10bb5e62a66a72f5cbc7aa7b13 (diff)
downloadhh-72440d0e3b2a8224b65491202798f5c0b18a2dde.tar.gz
mkChangeItUp
Diffstat (limited to 'examples')
-rw-r--r--examples/basic.go35
1 files changed, 29 insertions, 6 deletions
diff --git a/examples/basic.go b/examples/basic.go
index 467460f..2515c52 100644
--- a/examples/basic.go
+++ b/examples/basic.go
@@ -3,15 +3,38 @@ package examples
import (
"log/slog"
"net/http"
+ "strconv"
)
// Big bungus function here!
//
-//hh:route GET /admin/users, query: query, nextURL: hh.Cookie(r, "logout_next_url")
-func adminUsersForm(query struct {
- search, year string
- offset int
-}, w http.ResponseWriter) {
+//hh:route GET /admin/users
+func adminUsersForm(w http.ResponseWriter, r struct {
+ r *http.Request
+ search, year string `hh:"form"`
+ offset int `hh:"form,optional"`
+ nextURL string `hh:"cookie,logout_next_url"`
+}) {
_, _ = w.Write([]byte("ahahaha"))
- slog.Info("get admin users form", "search", query.search, "offset", query.offset)
+ slog.Info("get admin users form", "search", r.search, "offset", r.offset, "next-url", r.nextURL)
+}
+
+func hh_adminUsersForm[S any](s S, w http.ResponseWriter, r *http.Request) {
+ search := r.FormValue("search")
+
+ year := r.FormValue("year")
+
+ offset0 := r.FormValue("offset")
+ offset, err := strconv.Atoi(offset0)
+ if err != nil {
+ w.WriteHeader(http.StatusBadRequest)
+ w.Write([]byte(`Bad request. Invalid integer 'offset' in form/query`))
+ }
+
+ adminUsersForm(w, struct {
+ r *http.Request
+ search, year string `hh:"form"`
+ offset int `hh:"form,optional"`
+ nextURL string `hh:"cookie,logout_next_url"`
+ }{r: r, search: search, year: year, offset: offset})
}