-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
81 lines (66 loc) · 1.68 KB
/
handler.go
File metadata and controls
81 lines (66 loc) · 1.68 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
package handler
import (
"io"
"net/http"
"go.uber.org/fx"
"go.uber.org/zap"
"github.com/lambda-feedback/shimmy/config"
"github.com/lambda-feedback/shimmy/runtime"
)
type CommandHandlerParams struct {
fx.In
Handler runtime.Handler
Config config.Config
Log *zap.Logger
}
func NewCommandHandler(params CommandHandlerParams) *CommandHandler {
return &CommandHandler{
handler: params.Handler,
config: params.Config,
log: params.Log,
}
}
type CommandHandler struct {
handler runtime.Handler
config config.Config
log *zap.Logger
}
func (h *CommandHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log := h.log.With(
zap.String("path", r.URL.Path),
zap.String("method", r.Method),
)
// Check for authorization
if h.config.Auth.Key != "" && r.Header.Get("api-key") != h.config.Auth.Key {
log.Debug("unauthorized request")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Debug("failed to read body", zap.Error(err))
http.Error(w, "failed to read body", http.StatusBadRequest)
return
}
request := runtime.Request{
Path: r.URL.Path,
Method: r.Method,
Header: r.Header,
Body: body,
}
// Handle the request
response := h.handler.Handle(r.Context(), request)
// Map response headers
for k, v := range response.Header {
for _, vv := range v {
w.Header().Add(k, vv)
}
}
// Write response headers and status code
w.WriteHeader(response.StatusCode)
// Write response body
if _, err := w.Write(response.Body); err != nil {
log.Debug("failed to write response", zap.Error(err))
http.Error(w, "failed to write response", http.StatusInternalServerError)
}
}