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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
// WARNING: this file has been automatically generated by
// git.magnusson.space/hh. DO NOT EDIT MANUALLY!
package {{ .PackageName }}
import (
{{ range $_, $i := .Imports -}}
"{{ $i }}"
{{ end }}
)
func hhMountRoutes(mux *http.ServeMux) {
if mux == nil {
mux = http.DefaultServeMux
}
wrapper := func(handler func(w http.ResponseWriter, r *http.Request)) http.Handler {
return http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
handler(w, r)
})
}
{{ range $_, $fn := .Functions -}}
mux.Handle({{ $fn.Pattern | quote }}, wrapper(hh_{{ $fn.Name }}))
{{ end }}
}
{{ range $_, $fn := .Functions }}
func hh_{{ $fn.Name }}(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err == nil {
return
}
w.WriteHeader(http.StatusInternalServerError)
id := uuid.New()
w.Write([]byte("Internal server error. id = " + id.String()))
slog.Error("Panic in handler", "handler", {{ quote $fn.Name }}, "id", id, "error", err)
}()
{{- if $fn.DoParseForm }}
if err := r.ParseForm(); err != nil {
panic("todo: Bad request")
}
{{ end }}
var parsed {{ $fn.RequestTypeDef }}
{{- range $_, $f := $fn.RequestTypeFields }}
{{ if eq $f.TypeDef "*http.Request" }}
{{ continue }}
{{ end }}
{{ $f.Name }}, {{ $f.Name }}Skipped := {{ extractorName $f.Extractor }}(r, {{ $f.NameInReq | quote }})
{{ if not $f.Optional }}
if {{ $f.Name }}Skipped {
panic("todo: Bad request: " + {{ $f.Extractor | quote }} + " value " + {{ $f.NameInReq | quote }} + " missing")
}
{{ end }}
{{ if $f.Optional }} if !{{ $f.Name }}Skipped {{ end -}} {
var err error
parsed.{{ $f.Name }}, err = {{ converterName $f.TypeDef }}({{ $f.Name }})
if err != nil {
panic("todo: Bad request: " + {{ $f.NameInReq | quote }} + " must be a valid " + {{ $f.TypeDef | quote }})
}
}
{{ end }}
{{ $fn.Name }}(w, parsed)
{{ if false }}
{{ range $_, $f := $fn.RequestTypeFields -}}
{{ $f.Name }}: {{ $f.Name }},
{{ end }}
{{ end }}
}
{{ end }}
|