summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorMathias Magnusson <mathias@magnusson.space>2026-01-24 01:57:33 +0100
committerMathias Magnusson <mathias@magnusson.space>2026-01-24 01:57:33 +0100
commit39c6170ccab2977bbbaecdde2fdff772f39e2415 (patch)
tree97d1d360e8ce62517a573380c0c03362915e55e8 /examples
parent1fe804770391a644e23eea7c22e0868e3dfb4d2e (diff)
downloadhh-39c6170ccab2977bbbaecdde2fdff772f39e2415.tar.gz
allow returning values from handlersHEADmain
Diffstat (limited to 'examples')
-rw-r--r--examples/basic.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/examples/basic.go b/examples/basic.go
index d8ad6d7..5986e7e 100644
--- a/examples/basic.go
+++ b/examples/basic.go
@@ -1,9 +1,11 @@
package main
import (
+ "errors"
"log/slog"
"net/http"
+ "git.0m.nu/hh"
"github.com/google/uuid"
)
@@ -26,6 +28,29 @@ func adminUsersForm(r struct {
slog.Info("get admin users form", "search", r.search, "offset", r.offset, "next-url", r.nextURL)
}
+//hh:route GET /return-err
+func returnErr(r struct{ raw *http.Request }) error {
+ _ = r
+ return errors.New("whoops")
+}
+
+//hh:route GET /return-value
+func returnValue(r struct{ raw *http.Request }) hh.JSONValue {
+ _ = r
+ return hh.JSON(map[string]any{"key": []string{"value", "s"}})
+}
+
+//hh:route GET /return-both/{which}
+func returnBoth(r struct {
+ which string `hh:"path"`
+}) (hh.JSONValue, error) {
+ if r.which == "error" {
+ return hh.JSON(nil), errors.New("whoops")
+ } else {
+ return hh.JSON(map[string]any{"key": []string{"value", "s"}}), nil
+ }
+}
+
func main() {
hhMountRoutes(nil)
slog.Error("Error listening", "error", http.ListenAndServe(":http", nil))