From 39c6170ccab2977bbbaecdde2fdff772f39e2415 Mon Sep 17 00:00:00 2001 From: Mathias Magnusson Date: Sat, 24 Jan 2026 01:57:33 +0100 Subject: allow returning values from handlers --- examples/basic.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'examples/basic.go') 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)) -- cgit v1.2.3