-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
102 lines (96 loc) · 3.08 KB
/
Copy pathhttp.go
File metadata and controls
102 lines (96 loc) · 3.08 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// HttpAPI is a very simple API inspired by HUMA with generc helpers
// for json serialization/deserialization
// See [Register] and [RegisterNoInputs]
type HttpAPI struct {
mux *http.ServeMux
}
func (api *HttpAPI) Mux() *http.ServeMux {
return api.mux
}
func NewHttpAPI() *HttpAPI {
return &HttpAPI{
mux: http.NewServeMux(),
}
}
// Register registers a handler for a pattern and handles the json deserialization
// from request body
func Register[Input any, Output any](
api *HttpAPI,
pattern string,
handler func(request *http.Request, input *Input) (*Output, error),
) {
api.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Accept", "application/json; charset=utf-8")
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
w.WriteHeader(http.StatusUnsupportedMediaType)
fmt.Fprintf(w, "use content-type: application/json")
return
}
if r.Body == nil || r.ContentLength == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "body is required")
return
}
var input Input
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "unable to parse json as %T: %s", input, err.Error())
return
}
output, err := handler(r, &input)
if err != nil {
// todo: check error type
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
buf := &bytes.Buffer{}
err = json.NewEncoder(buf).Encode(output)
if err != nil {
// todo: check error type
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "json encoding error: %s", err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
})
}
// RegisterNoInputs registers a handler for a pattern that does not expect
// any inputs and returns the Output serialized as json
func RegisterNoInputs[Output any](
api *HttpAPI,
pattern string,
handler func(*http.Request) (*Output, error),
) {
api.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
output, err := handler(r)
if err != nil {
// todo: check error type
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, err.Error())
return
}
buf := &bytes.Buffer{}
err = json.NewEncoder(buf).Encode(output)
if err != nil {
// todo: check error type
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "json encoding error: %s", err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(buf.Bytes())
})
}