-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
96 lines (82 loc) · 2.75 KB
/
request.go
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
package request
import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"github.com/go-playground/validator/v10"
"github.com/gorilla/schema"
"github.com/jonmol/http-skeleton/server/util/myctx"
"github.com/jonmol/http-skeleton/server/util/response"
"github.com/jonmol/http-skeleton/util/logging"
)
var (
decoder = schema.NewDecoder()
val = validator.New()
)
// HandlerType is the type of the business logic handler.
type HandlerFunc func(http.ResponseWriter, *http.Request) (any, any, *response.RespError)
func HandleCall(w http.ResponseWriter, r *http.Request, data any, handler HandlerFunc) {
ctx := r.Context()
l := myctx.LoggerFromCtx(ctx)
if data != nil {
switch m := r.Method; m {
case http.MethodGet:
if err := validateGet(ctx, data, w, r); err != nil {
return
}
case http.MethodPost, http.MethodPut, http.MethodDelete:
body, err := io.ReadAll(r.Body)
if err != nil {
response.JSONErrorResponse(ctx, w, response.MalformedRequest, "cannot read body")
return
}
if err = json.Unmarshal(body, data); err != nil {
l.Error("cannot unmarshal request body", logging.Err(err), slog.String("body", string(body)))
response.JSONErrorResponse(ctx, w, response.MalformedRequest, "cannot unmarshal body")
return
}
/*
having this code instead of the four lines after will allow maps to be used as input types,
a big problem with maps is to validate them and it'd have to happen in the handler and/or service
if d2, ok := data.(*map[string]interface{}); ok && d2 != nil {
// TODO: how we want to validate maps?
} else {
err = validator.New().Struct(data)
if err != nil {
response.JSONErrorResponse(ctx, w, response.MalformedRequest, err.Error())
return
}
}
*/
err = val.Struct(data)
if err != nil {
response.JSONErrorResponse(ctx, w, response.MalformedRequest, err.Error())
return
}
default:
response.JSONErrorResponse(ctx, w, response.Internal, "unsupported HTTP method")
return
}
}
resp, meta, httpErr := handler(w, r)
if httpErr != nil {
response.JSONErrorResponse(ctx, w, httpErr.Code, httpErr.Msg)
return
}
response.JSONResponse(ctx, w, &response.Resp{Data: resp, Meta: meta})
}
func validateGet(ctx context.Context, data interface{}, w http.ResponseWriter, r *http.Request) error {
l := myctx.LoggerFromCtx(ctx)
if err := decoder.Decode(data, r.URL.Query()); err != nil {
l.Error("cannot decode query parameters", err, "query", r.URL.Query())
response.JSONErrorResponse(ctx, w, response.MalformedRequest, "cannot unmarshal query parameters")
return err
}
if err := val.Struct(data); err != nil {
response.JSONErrorResponse(ctx, w, response.MalformedRequest, err.Error())
return err
}
return nil
}