forked from b3scale/b3scale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbb_request_middleware.go
188 lines (161 loc) · 4.73 KB
/
bbb_request_middleware.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
package http
import (
"bytes"
"context"
"fmt"
"io/ioutil"
netHTTP "net/http"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
// "github.com/rs/zerolog/log"
"github.com/b3scale/b3scale/pkg/bbb"
"github.com/b3scale/b3scale/pkg/cluster"
"github.com/b3scale/b3scale/pkg/store"
)
// BBBRequestMiddleware decodes the incoming HTTP request
// into a BBB request and passes it to the API gateway.
// All requests starting with the mountpoint prefix are
// treated as BBB requests.
//
// An error will be returned when A request can not be
// decoded.
func BBBRequestMiddleware(
mountPoint string,
ctrl *cluster.Controller,
gateway *cluster.Gateway,
) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// We use the request context. However, for some things
// we need to make sure they are persisted even though
// the context is canceled. This might happen when the
// client disconnects after we made our request to the
// backend.
// ctx := c.Request().Context()
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
path := c.Request().URL.Path
if !strings.HasPrefix(path, mountPoint) {
return next(c) // nothing to do here.
}
// We acquire a connection to the database here,
// if this fails it does not really make sense to move on.
// TODO: See if we actually can use this context.
conn, err := store.Acquire(ctx)
if err != nil {
return err
}
defer conn.Release()
ctx = store.ContextWithConnection(ctx, conn)
// Decode HTTP request into a BBB request
// and verify it.
path = path[len(mountPoint):]
frontendKey, resource := decodePath(path)
frontend, err := cluster.GetFrontend(ctx, store.Q().
Where("key = ?", frontendKey))
if err != nil {
return handleAPIError(c, err)
}
// Check if the frontend could be identified
if frontend == nil {
return handleAPIError(c, fmt.Errorf(
"no such frontend for key: %s", frontendKey))
}
ctx = cluster.ContextWithFrontend(ctx, frontend)
// We have an action, we have a frontend, now
// we need the query parameters and request body.
params := decodeParams(c)
checksum, _ := params.Checksum()
body := readRequestBody(c)
bbbReq := &bbb.Request{
Request: c.Request(),
Frontend: frontend.Frontend(),
Resource: resource,
Params: params,
Body: body,
Checksum: checksum,
}
log.Debug().Stringer("req", bbbReq).Msg("inbound request")
// Authenticate request
if err := bbbReq.Verify(); err != nil {
return handleAPIError(c, err)
}
// Before we dispatch, let's check if the original
// request context is still valid
if err := c.Request().Context().Err(); err != nil {
return err
}
res := gateway.Dispatch(ctx, conn, bbbReq)
return writeBBBResponse(c, res)
}
}
}
// writeBBBResponse takes a response from the cluster
// and writes it as a response to the request.
func writeBBBResponse(c echo.Context, res bbb.Response) error {
// Check if the context is still valid
if err := c.Request().Context().Err(); err != nil {
return err
}
// When the status is not set assume something went wrong
status := res.Status()
if status == 0 {
status = netHTTP.StatusInternalServerError
}
// Update and write headers
for key, values := range res.Header() {
for _, v := range values {
c.Response().Header().Add(key, v)
}
}
c.Response().WriteHeader(status)
// Serialize BBB response and send
data, err := res.Marshal()
if err != nil {
return err
}
_, err = c.Response().Write(data)
return err
}
// decodePath extracts the frontend key and BBB
// action from the request path
func decodePath(path string) (string, string) {
tokens := strings.Split(path, "/")
if len(tokens) < 3 {
return "", ""
}
return tokens[1], tokens[len(tokens)-1]
}
// handleAPIError is the error handler function
// for all API errors. The error will be wrapped into
// a BBB error response.
func handleAPIError(c echo.Context, err error) error {
// Encode as BBB error
res := &bbb.XMLResponse{
Returncode: "ERROR",
Message: fmt.Sprintf("%s", err),
MessageKey: "b3scale_server_error",
}
// Write error response
return c.XML(netHTTP.StatusInternalServerError, res)
}
// readRequestBody will load the entire request body.
func readRequestBody(c echo.Context) []byte {
body := []byte{}
if c.Request().Body != nil { // Read
body, _ = ioutil.ReadAll(c.Request().Body)
}
// Reset after reading
c.Request().Body = ioutil.NopCloser(bytes.NewBuffer(body))
return body
}
func decodeParams(c echo.Context) bbb.Params {
values := c.QueryParams()
params := bbb.Params{}
for k := range values {
params[k] = values.Get(k)
}
return params
}