-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
79 lines (69 loc) · 2.2 KB
/
handler.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
package authorizer
import (
"log/slog"
"net/http"
"time"
)
// Authorizer is a handler that checks requests for valid keys.
type Authorizer struct {
// NotAuthorizedStatusCode contains the status code the hander
// returns when the request does not contain a valid key.
NotAuthorizedStatusCode int
// AuthorizedStatusCode contains the status code the hander
// returns when the request does not contain a valid key.
AuthorizedStatusCode int
// Logger is the structured logger for handler.
Logger *slog.Logger
}
// durationLV is a slog valuer for duration as string.
type durationLV time.Duration
// String returns the value of d as fmt formatted string.
func (d durationLV) String() string {
return time.Duration(d).String()
}
// LogValue returns the value of d.
func (d durationLV) LogValue() slog.Value {
return slog.StringValue(d.String())
}
type RequestAuthorizer interface {
Authorize(*http.Request) error
}
// Handle returns a handler func that checks if the request
// contains a valid api key.
// It returns AuthorizedStatusCode if this is the case and
// NotAuthorizedStatusCode if not.
func (a *Authorizer) Handle(ra RequestAuthorizer) http.HandlerFunc {
logger := a.Logger.With(slog.String("function", "handler"))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger = logger.With(
slog.String("path", r.URL.RawPath),
slog.String("host", r.Host))
defer func(start time.Time) {
since := time.Since(start)
logger.Info("handler called",
slog.Time("start", start),
slog.Duration("duration", since),
slog.Attr{Key: "duration_string", Value: durationLV(since).LogValue()},
)
}(time.Now())
if err := ra.Authorize(r); err != nil {
logger.Info("forbidden")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(a.NotAuthorizedStatusCode)
w.Write([]byte(err.Error()))
return
}
logger.Info("authorized")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(a.AuthorizedStatusCode)
w.Write([]byte("Authorized"))
})
}
// New returns an authorizer with the give keys.
func New() (*Authorizer, error) {
a := &Authorizer{
NotAuthorizedStatusCode: http.StatusForbidden,
AuthorizedStatusCode: http.StatusOK,
}
return a, nil
}