-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathnotification_v1.go
More file actions
179 lines (158 loc) · 4.98 KB
/
Copy pathnotification_v1.go
File metadata and controls
179 lines (158 loc) · 4.98 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package httptransport
import (
"context"
"errors"
"log/slog"
"net/http"
"path"
"path/filepath"
"strconv"
"time"
"github.com/google/uuid"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"github.com/quay/clair/v4/internal/codec"
"github.com/quay/clair/v4/internal/httputil"
"github.com/quay/clair/v4/middleware/compress"
"github.com/quay/clair/v4/notifier"
)
const defaultPageSize = 500
type notificationResponse struct {
Page notifier.Page `json:"page"`
Notifications []notifier.Notification `json:"notifications"`
}
// NotificationV1 is a Notification endpoint.
type NotificationV1 struct {
inner http.Handler
serv notifier.Service
}
var _ http.Handler = (*NotificationV1)(nil)
// NewNotificationV1 returns an http.Handler serving the Notification V1 API rooted at
// "prefix".
func NewNotificationV1(_ context.Context, prefix string, srv notifier.Service, topt otelhttp.Option) (*NotificationV1, error) {
prefix = path.Join("/", prefix) // Ensure the prefix is rooted and cleaned.
m := http.NewServeMux()
h := NotificationV1{
inner: otelhttp.NewHandler(
compress.Handler(m),
"notificationv1",
otelhttp.WithMessageEvents(otelhttp.ReadEvents, otelhttp.WriteEvents),
topt,
),
serv: srv,
}
p := path.Join(prefix, "notification") + "/"
m.Handle(p, notificationv1wrapper.wrapFunc(path.Join(p, ":id"), h.serveHTTP))
return &h, nil
}
// ServeHTTP implements http.Handler.
func (h *NotificationV1) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
r = withRequestID(r)
ctx := r.Context()
var status int
var length int64
w = httputil.ResponseRecorder(&status, &length, w)
defer func() {
switch err := http.NewResponseController(w).Flush(); {
case errors.Is(err, nil):
case errors.Is(err, http.ErrNotSupported): // Skip
default:
slog.WarnContext(ctx, "unable to flush http response", "reason", err)
}
slog.InfoContext(ctx, "handled HTTP request",
"remote_addr", r.RemoteAddr,
"method", r.Method,
"request_uri", r.RequestURI,
"status", status,
"written", length,
"duration", time.Since(start))
}()
h.inner.ServeHTTP(w, r)
}
func (h *NotificationV1) serveHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
h.get(w, r)
case http.MethodDelete:
h.delete(w, r)
default:
apiError(r.Context(), w, http.StatusMethodNotAllowed, "endpoint only allows GET or DELETE")
}
}
func (h *NotificationV1) delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
path := r.URL.Path
id := filepath.Base(path)
notificationID, err := uuid.Parse(id)
if err != nil {
slog.WarnContext(ctx, "could not parse notification id", "reason", err)
apiError(ctx, w, http.StatusBadRequest, "could not parse notification id: %v", err)
}
err = h.serv.DeleteNotifications(ctx, notificationID)
if err != nil {
slog.WarnContext(ctx, "could not delete notification", "reason", err)
apiError(ctx, w, http.StatusInternalServerError, "could not delete notification: %v", err)
}
// TODO(hank) This should return HTTP 204.
}
// Get will return paginated notifications to the caller.
func (h *NotificationV1) get(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
path := r.URL.Path
id := filepath.Base(path)
notificationID, err := uuid.Parse(id)
if err != nil {
slog.WarnContext(ctx, "could not parse notification id", "reason", err)
apiError(ctx, w, http.StatusBadRequest, "could not parse notification id: %v", err)
}
// optional page_size parameter
var pageSize int
if param := r.URL.Query().Get("page_size"); param != "" {
p, err := strconv.ParseInt(param, 10, 64)
if err != nil {
apiError(ctx, w, http.StatusBadRequest, "could not parse %q query param into integer", "page_size")
}
pageSize = int(p)
}
if pageSize == 0 {
pageSize = defaultPageSize
}
// optional page parameter
var next *uuid.UUID
if param := r.URL.Query().Get("next"); param != "" {
n, err := uuid.Parse(param)
if err != nil {
apiError(ctx, w, http.StatusBadRequest, "could not parse %q query param into integer", "next")
}
if n != uuid.Nil {
next = &n
}
}
allow := []string{"application/vnd.clair.notification_page.v1+json", "application/json"}
switch err := pickContentType(w, r, allow); {
case errors.Is(err, nil): // OK
case errors.Is(err, ErrMediaType):
apiError(ctx, w, http.StatusUnsupportedMediaType, "unable to negotiate common media type for %v", allow)
default:
apiError(ctx, w, http.StatusBadRequest, "malformed request: %v", err)
}
inP := ¬ifier.Page{
Size: pageSize,
Next: next,
}
notifications, outP, err := h.serv.Notifications(ctx, notificationID, inP)
if err != nil {
apiError(ctx, w, http.StatusInternalServerError, "failed to retrieve notifications: %v", err)
}
response := notificationResponse{
Page: outP,
Notifications: notifications,
}
defer writerError(w, &err)()
enc := codec.GetEncoder(w)
err = enc.Encode(&response)
}
func init() {
notificationv1wrapper.init("notificationv1")
}
var notificationv1wrapper wrapper