-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
341 lines (273 loc) · 9.45 KB
/
server.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright The Alfred.go Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package server
import (
"alfred/internal/conf"
"alfred/internal/function"
"alfred/internal/helper"
"alfred/internal/log"
"alfred/internal/mock"
"alfred/internal/tracing"
"alfred/pkg/metrics"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"go.uber.org/zap"
)
type Key string
func pathHelperMiddleware(mockCollection mock.MockCollection) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
for _, m := range mockCollection.Mocks {
if m.HasRegexUrl() {
if m.Request.RegexUrl.Match([]byte(r.URL.Path)) {
//var values Values
values := make(map[string]string)
//add the helper name:value in the context
hs := m.GetPathRegexHelpers()
//populate
for _, h := range hs {
index, err := strconv.Atoi(h.Target)
if err != nil {
log.Error(r.Context(), "Failed to transform helper target into integer type", err)
}
if err != nil {
fmt.Println("Error during conversion")
return
}
v := m.Request.RegexUrl.FindSubmatch([]byte(r.URL.Path))
//add to values
values[h.String] = string(v[index])
}
//add to original path
values["originalPath"] = r.URL.Path
//update url to match the mock handler
r.URL.Path = m.Request.UrlTransformed
ctx := context.WithValue(r.Context(), helper.PathHelperKey("pathHelperValues"), values)
next.ServeHTTP(w, r.WithContext(ctx))
return
}
}
}
// call next handler
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}
func routerMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/" + r.Method + r.URL.Path
// call next handler
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func removeFirstFolder(path string) string {
components := strings.SplitN(path, "/", 3)
if len(components) >= 3 {
return "/" + components[2]
}
return path
}
// Middleware for logging each request
func logRequestMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// Serve the request
next.ServeHTTP(w, r)
var path string
values := r.Context().Value(helper.PathHelperKey("pathHelperValues"))
if values != nil {
path = values.(map[string]string)["originalPath"]
} else {
path = r.URL.Path
}
// Log the request
msg := " " + r.Method + " " + removeFirstFolder(path) + " " + r.RemoteAddr + " " + fmt.Sprint(time.Since(start))
log.Info(r.Context(), msg)
if log.GetLevel() == "debug" {
reqBodyBytes, _ := io.ReadAll(r.Body)
log.Debug(r.Context(), msg,
zap.String("request-body", string(reqBodyBytes)))
}
})
}
// Build the service
func BuildServer(conf *conf.Config, asyncRunningJobsCount *sync.WaitGroup, mockCollection mock.MockCollection) (*http.Server, error) {
//Build all endpoints handler
handler, err := BuildHandler(conf, asyncRunningJobsCount, mockCollection)
if err != nil {
return nil, err
}
//Router
handler = routerMiddleware(handler)
//logger
handler = logRequestMiddleware(handler)
//tracing
handler = tracing.AddTracingMiddlware(handler)
//
if mockCollection.HasRegexUrlMock() {
log.Debug(context.Background(), "Regex URL detected")
middlewarePathHelper := pathHelperMiddleware(mockCollection)
handler = middlewarePathHelper(handler)
}
//Associate the handler to a server (-> contains listening interface(s))
//Here, the server is listening on ALL interfaces and binding on 'conf.Port' port
return &http.Server{
Handler: handler,
Addr: fmt.Sprintf("%s:%s", conf.Alfred.Core.Listen.Ip, conf.Alfred.Core.Listen.Port),
}, nil
}
// Create Handler with endpoints to serve: used from service or from tests
func BuildHandler(conf *conf.Config, asyncRunningJobsCount *sync.WaitGroup, mocks mock.MockCollection) (http.Handler, error) {
//log
log.Info(context.Background(), "Starting")
defer log.LogPanic()
// Create controller
mux := http.NewServeMux()
if conf.Alfred.Prometheus.Enable {
prometheusConfig := metrics.MetricsConfig{
MetricPath: conf.Alfred.Prometheus.Path,
MetricPort: conf.Alfred.Prometheus.Listen.Port,
MetricIp: conf.Alfred.Prometheus.Listen.Ip,
HttpServerIp: conf.Alfred.Core.Listen.Ip,
HttpServerPort: conf.Alfred.Core.Listen.Port,
SlowTime: conf.Alfred.Prometheus.SlowTimeSeconds,
}
prometheusConfig.SanitizeConfiguration()
//metrics.CreateMetricEngine(controller, prometheusConfig)
//metrics
metrics.AddMetrics(mux, prometheusConfig)
log.Info(context.Background(), "Prometheus exporter started to serve on host "+prometheusConfig.MetricIp+" and is listening at port "+fmt.Sprint(prometheusConfig.MetricPort)+" with '"+prometheusConfig.MetricPath+"' path")
}
// Global delay applied to mocks
var alfredGlobalDelay time.Duration
{
//Add Routes
{
mux.HandleFunc("/POST"+"/logger", ChangingLoggingLevelRuntime)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mockList, _ := json.MarshalIndent(mocks.GetMockInfoList(), "", " ")
_, err := w.Write([]byte("Hello Sir ! I take care ot the following mocks:\n" + string(mockList) + "\n\n(Alfred)"))
if err != nil {
log.Error(r.Context(), "failed to write", err)
}
})
mux.HandleFunc("/PATCH"+"/alfred", func(w http.ResponseWriter, r *http.Request) {
PatchMock(w, r, mocks)
})
mux.HandleFunc("/POST"+"/alfred/delay", func(w http.ResponseWriter, r *http.Request) {
DelayMocks(&alfredGlobalDelay, w, r)
})
//Load JS functions
functionCollection, err := function.CreateFunctionCollectionFromFolder(conf.Alfred.Core.FunctionsDir)
if err != nil {
log.Debug(context.Background(), "function files loader error: "+err.Error())
}
// Create mocks routes
AddMocksRoutes(mux, mocks, functionCollection, &alfredGlobalDelay)
}
}
return mux, nil
}
// Serve will bind the port(s) and launch serve in a separated goroutine
func Serve(main_ctx context.Context, conf *conf.Config, server *http.Server) {
//Bind
listener, err := net.Listen("tcp", server.Addr)
if err != nil {
log.Error(main_ctx, "error Server Binding", err)
panic("error Server Binding on " + server.Addr)
}
//Log that's the bind is ok
fmt.Println("{ \"alfred-speaking\" : \"Started to serve on host " + conf.Alfred.Core.Listen.Ip + " and listening at port " + conf.Alfred.Core.Listen.Port + ", with " + main_ctx.Value(Key("mocksNb")).(string) + " mocks, Sir.\"}")
log.Info(main_ctx, "alfred started to serve on host "+conf.Alfred.Core.Listen.Ip+" and is listening at port "+conf.Alfred.Core.Listen.Port)
//Start to serve
go func() {
//tracing
cleanup, err := tracing.Init(main_ctx, tracing.OtelConfig{
ServiceName: conf.Alfred.Name,
ServiceNamespace: conf.Alfred.Namespace,
DeploymentEnvironment: conf.Alfred.Environment,
ExporterInsecure: conf.Alfred.Tracing.Insecure,
TracesSampler: conf.Alfred.Tracing.Sampler,
TracesSamplerArg: conf.Alfred.Tracing.SamplerArgs,
ExporterOtlpEndpoint: conf.Alfred.Tracing.OtlpEndpoint,
})
if err != nil {
log.Error(main_ctx, "server panic", errors.New("error during preparing tracer..."+err.Error()))
panic("error during preparing tracer..." + err.Error())
}
defer func() {
err := cleanup(main_ctx)
if err != nil {
log.Error(main_ctx, "error during tracing cleanup", err)
}
}()
err = server.Serve(listener)
if err != nil {
log.Error(main_ctx, "error at server start", err)
}
}()
}
// Return necessaries to stop serve
func Stop(ctx context.Context, server *http.Server, asyncRunningJobsCount *sync.WaitGroup) {
log.Info(ctx, "Server is stopping")
//Let's some few seconds to shutdown gracefully
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Second))
defer cancel()
//Shutdown the http server
if err := server.Shutdown(ctx); err != nil {
log.Error(ctx, "Error While stopping Server: ", err)
}
// Wait that all async jobs are done (timeboxed)
waitAsyncJobsTimeout(ctx, asyncRunningJobsCount)
//Log again...
log.Info(ctx, "Stopped Server")
}
// waitAsyncJobsTimeout is synchronized on 2 things :
// - a channel to be aware that's an async job is finished
// - a timeout context to timebox this 'waiting'
func waitAsyncJobsTimeout(ctx context.Context, wg *sync.WaitGroup) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // wait() was done
case <-ctx.Done():
return true // deadline was reached
}
}
// requestRecover retreives a panic if it happened during request treatment
func requestRecover(w http.ResponseWriter, req *http.Request) {
defer func() {
if r := recover(); r != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Error(req.Context(), "Catched Panic", errors.New(fmt.Sprint(r)))
}
}()
}