summaryrefslogtreecommitdiff
path: root/examples/basic.go
blob: 5986e7e51b3b09608035e3f4c6becba924109a84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
	"errors"
	"log/slog"
	"net/http"

	"git.0m.nu/hh"
	"github.com/google/uuid"
)

//go:generate go run git.0m.nu/hh/cmd/generate

// Big bungus function here!
//
//hh:route GET /org/{orgID}/users
func adminUsersForm(r struct {
	w       http.ResponseWriter
	raw     *http.Request
	search  string    `hh:"form"`
	year    int       `hh:"optional,form"`
	offset  int       `hh:"form"`
	orgID   uuid.UUID `hh:"path"`
	banana  uuid.UUID `hh:"optional,form"`
	nextURL string    `hh:"optional,cookie,logout_next_url"`
}) {
	_, _ = r.w.Write([]byte("ahahaha"))
	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))
}