-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathapi.go
More file actions
134 lines (110 loc) · 3.8 KB
/
api.go
File metadata and controls
134 lines (110 loc) · 3.8 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package api
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/golang/gddo/httputil/header"
"github.com/dstackai/dstack/runner/internal/common/log"
)
type Error struct {
Status int
Err error
Msg string
}
func (e *Error) Error() string {
if e.Msg != "" {
return e.Msg
}
if e.Err != nil {
return e.Err.Error()
}
return http.StatusText(e.Status)
}
type Router struct {
*http.ServeMux
}
func (r *Router) AddHandler(method string, pattern string, handler func(http.ResponseWriter, *http.Request) (interface{}, error)) {
r.HandleFunc(fmt.Sprintf("%s %s", method, pattern), JSONResponseHandler(handler))
}
func NewRouter() Router {
return Router{http.NewServeMux()}
}
func DecodeJSONBody(w http.ResponseWriter, r *http.Request, dst interface{}, allowUnknown bool) error {
// From https://www.alexedwards.net/blog/how-to-properly-parse-a-json-request-body
if r.Header.Get("Content-Type") != "" {
value, _ := header.ParseValueAndParams(r.Header, "Content-Type")
msg := "Content-Type header is not application/json"
if value != "application/json" {
return &Error{Status: http.StatusUnsupportedMediaType, Msg: msg}
}
}
r.Body = http.MaxBytesReader(w, r.Body, 1*1024*1024)
dec := json.NewDecoder(r.Body)
if !allowUnknown {
dec.DisallowUnknownFields()
}
err := dec.Decode(&dst)
if err != nil {
var syntaxError *json.SyntaxError
var unmarshalTypeError *json.UnmarshalTypeError
switch {
case errors.As(err, &syntaxError):
msg := fmt.Sprintf("Request body contains badly-formed JSON (at position %d)", syntaxError.Offset)
return &Error{Status: http.StatusBadRequest, Msg: msg}
case errors.Is(err, io.ErrUnexpectedEOF):
msg := "Request body contains badly-formed JSON"
return &Error{Status: http.StatusBadRequest, Msg: msg}
case errors.As(err, &unmarshalTypeError):
msg := fmt.Sprintf("Request body contains an invalid value for the %q field (at position %d)", unmarshalTypeError.Field, unmarshalTypeError.Offset)
return &Error{Status: http.StatusBadRequest, Msg: msg}
case strings.HasPrefix(err.Error(), "json: unknown field "):
fieldName := strings.TrimPrefix(err.Error(), "json: unknown field ")
msg := fmt.Sprintf("Request body contains unknown field %s", fieldName)
return &Error{Status: http.StatusBadRequest, Msg: msg}
case errors.Is(err, io.EOF):
msg := "Request body must not be empty"
return &Error{Status: http.StatusBadRequest, Msg: msg}
case err.Error() == "http: request body too large":
msg := "Request body must not be larger than 1MB"
return &Error{Status: http.StatusRequestEntityTooLarge, Msg: msg}
default:
return err
}
}
err = dec.Decode(&struct{}{})
if !errors.Is(err, io.EOF) {
msg := "Request body must only contain a single JSON object"
return &Error{Status: http.StatusBadRequest, Msg: msg}
}
return nil
}
func JSONResponseHandler(handler func(http.ResponseWriter, *http.Request) (interface{}, error)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
status := 200
errMsg := ""
var apiErr *Error
body, err := handler(w, r)
if err != nil {
if errors.As(err, &apiErr) {
status = apiErr.Status
errMsg = apiErr.Error()
log.Warning(r.Context(), "API error", "err", errMsg, "method", r.Method, "endpoint", r.URL.Path, "status", status)
} else {
status = http.StatusInternalServerError
log.Error(r.Context(), "Unexpected API error", "err", err, "method", r.Method, "endpoint", r.URL.Path, "status", status)
}
} else {
log.Trace(r.Context(), "", "method", r.Method, "endpoint", r.URL.Path, "status", status)
}
if status != 500 && body != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
} else {
http.Error(w, errMsg, status)
}
}
}