-
Notifications
You must be signed in to change notification settings - Fork 666
/
transport.go
192 lines (160 loc) · 5.12 KB
/
transport.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package api
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"strings"
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/consumers/notifiers"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/go-chi/chi/v5"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
const (
contentType = "application/json"
offsetKey = "offset"
limitKey = "limit"
topicKey = "topic"
contactKey = "contact"
defOffset = 0
defLimit = 20
)
// MakeHandler returns a HTTP handler for API endpoints.
func MakeHandler(svc notifiers.Service, logger *slog.Logger, instanceID string) http.Handler {
opts := []kithttp.ServerOption{
kithttp.ServerErrorEncoder(apiutil.LoggingErrorEncoder(logger, encodeError)),
}
mux := chi.NewRouter()
mux.Route("/subscriptions", func(r chi.Router) {
r.Post("/", otelhttp.NewHandler(kithttp.NewServer(
createSubscriptionEndpoint(svc),
decodeCreate,
encodeResponse,
opts...,
), "create").ServeHTTP)
r.Get("/", otelhttp.NewHandler(kithttp.NewServer(
listSubscriptionsEndpoint(svc),
decodeList,
encodeResponse,
opts...,
), "list").ServeHTTP)
r.Delete("/", otelhttp.NewHandler(kithttp.NewServer(
deleteSubscriptionEndpint(svc),
decodeSubscription,
encodeResponse,
opts...,
), "delete").ServeHTTP)
r.Get("/{subID}", otelhttp.NewHandler(kithttp.NewServer(
viewSubscriptionEndpint(svc),
decodeSubscription,
encodeResponse,
opts...,
), "view").ServeHTTP)
r.Delete("/{subID}", otelhttp.NewHandler(kithttp.NewServer(
deleteSubscriptionEndpint(svc),
decodeSubscription,
encodeResponse,
opts...,
), "delete").ServeHTTP)
})
mux.Get("/health", magistrala.Health("notifier", instanceID))
mux.Handle("/metrics", promhttp.Handler())
return mux
}
func decodeCreate(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), contentType) {
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
}
req := createSubReq{token: apiutil.ExtractBearerToken(r)}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(err, errors.ErrMalformedEntity))
}
return req, nil
}
func decodeSubscription(_ context.Context, r *http.Request) (interface{}, error) {
req := subReq{
id: chi.URLParam(r, "subID"),
token: apiutil.ExtractBearerToken(r),
}
return req, nil
}
func decodeList(_ context.Context, r *http.Request) (interface{}, error) {
req := listSubsReq{token: apiutil.ExtractBearerToken(r)}
vals := r.URL.Query()[topicKey]
if len(vals) > 0 {
req.topic = vals[0]
}
vals = r.URL.Query()[contactKey]
if len(vals) > 0 {
req.contact = vals[0]
}
offset, err := apiutil.ReadNumQuery[uint64](r, offsetKey, defOffset)
if err != nil {
return listSubsReq{}, errors.Wrap(apiutil.ErrValidation, err)
}
req.offset = uint(offset)
limit, err := apiutil.ReadNumQuery[uint64](r, limitKey, defLimit)
if err != nil {
return listSubsReq{}, errors.Wrap(apiutil.ErrValidation, err)
}
req.limit = uint(limit)
return req, nil
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
if ar, ok := response.(magistrala.Response); ok {
for k, v := range ar.Headers() {
w.Header().Set(k, v)
}
w.Header().Set("Content-Type", contentType)
w.WriteHeader(ar.Code())
if ar.Empty() {
return nil
}
}
return json.NewEncoder(w).Encode(response)
}
func encodeError(_ context.Context, err error, w http.ResponseWriter) {
var wrapper error
if errors.Contains(err, apiutil.ErrValidation) {
wrapper, err = errors.Unwrap(err)
}
switch {
case errors.Contains(err, svcerr.ErrMalformedEntity),
errors.Contains(err, apiutil.ErrInvalidContact),
errors.Contains(err, apiutil.ErrInvalidTopic),
errors.Contains(err, apiutil.ErrMissingID),
errors.Contains(err, apiutil.ErrInvalidQueryParams):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(err, svcerr.ErrNotFound):
w.WriteHeader(http.StatusNotFound)
case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, apiutil.ErrBearerToken):
w.WriteHeader(http.StatusUnauthorized)
case errors.Contains(err, svcerr.ErrConflict):
w.WriteHeader(http.StatusConflict)
case errors.Contains(err, apiutil.ErrUnsupportedContentType):
w.WriteHeader(http.StatusUnsupportedMediaType)
case errors.Contains(err, svcerr.ErrCreateEntity),
errors.Contains(err, svcerr.ErrViewEntity),
errors.Contains(err, svcerr.ErrRemoveEntity):
w.WriteHeader(http.StatusInternalServerError)
default:
w.WriteHeader(http.StatusInternalServerError)
}
if wrapper != nil {
err = errors.Wrap(wrapper, err)
}
if errorVal, ok := err.(errors.Error); ok {
w.Header().Set("Content-Type", contentType)
if err := json.NewEncoder(w).Encode(errorVal); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}