-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
131 lines (112 loc) · 3.52 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
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
package gatego
import (
"context"
"errors"
"net/http"
"os"
"slices"
"github.com/hvuhsg/gatego/internal/config"
"github.com/hvuhsg/gatego/internal/handlers"
"github.com/hvuhsg/gatego/internal/middlewares"
"github.com/hvuhsg/gatego/internal/middlewares/security"
)
var ErrUnsupportedBaseHandler = errors.New("base handler unsupported")
func GetBaseHandler(service config.Service, path config.Path) (http.Handler, error) {
if path.Destination != nil && *path.Destination != "" {
return handlers.NewProxy(service, path)
} else if path.Directory != nil && *path.Directory != "" {
handler := handlers.NewFiles(*path.Directory, path.Path)
return handler, nil
} else if path.Backend != nil {
return handlers.NewBalancer(service, path)
} else {
// Should not be reached (early validation should prevent it)
return nil, ErrUnsupportedBaseHandler
}
}
func NewHandler(ctx context.Context, useOtel bool, service config.Service, path config.Path) (http.Handler, error) {
handler, err := GetBaseHandler(service, path)
if err != nil {
return nil, err
}
handlerWithMiddlewares := middlewares.NewHandlerWithMiddleware(handler)
handlerWithMiddlewares.Add(middlewares.NewLoggingMiddleware(os.Stdout))
// Open Telemetry
if useOtel {
otelMiddleware, err := middlewares.NewOpenTelemetryMiddleware(
ctx,
middlewares.OTELConfig{
ServiceDomain: service.Domain,
BasePath: path.Path,
},
)
if err != nil {
return nil, err
}
handlerWithMiddlewares.Add(otelMiddleware)
}
// Timeout
if path.Timeout == 0 {
path.Timeout = config.DefaultTimeout
}
handlerWithMiddlewares.Add(middlewares.NewTimeoutMiddleware(path.Timeout))
// Max request size
if path.MaxSize == 0 {
path.MaxSize = config.DefaultMaxRequestSize
}
handlerWithMiddlewares.Add(middlewares.NewRequestSizeLimitMiddleware(path.MaxSize))
// Rate limits
if len(path.RateLimits) > 0 {
ratelimiter, err := middlewares.NewRateLimitMiddleware(path.RateLimits)
if err != nil {
return nil, err
}
handlerWithMiddlewares.Add(ratelimiter)
}
// Add anomaly detector
if service.AnomalyDetection != nil {
handlerWithMiddlewares.Add(
security.NewRoutingAnomalyDetector(
service.AnomalyDetection.HeaderName,
service.AnomalyDetection.TresholdForRating,
service.AnomalyDetection.MinScore,
service.AnomalyDetection.MaxScore).AddAnomalyScore,
)
}
// Add headers
if path.Headers != nil {
handlerWithMiddlewares.Add(middlewares.NewAddHeadersMiddleware(*path.Headers))
}
// GZIP compression
if path.Gzip != nil && *path.Gzip {
handlerWithMiddlewares.Add(middlewares.GzipMiddleware)
}
// Remove response headers
if len(path.OmitHeaders) > 0 {
handlerWithMiddlewares.Add(middlewares.NewOmitHeadersMiddleware(path.OmitHeaders))
}
// Minify files
minifyConfig := middlewares.MinifyConfig{
ALL: slices.Contains(path.Minify, "all"),
JS: slices.Contains(path.Minify, "js"),
HTML: slices.Contains(path.Minify, "html"),
CSS: slices.Contains(path.Minify, "css"),
JSON: slices.Contains(path.Minify, "json"),
SVG: slices.Contains(path.Minify, "svg"),
XML: slices.Contains(path.Minify, "xml"),
}
handlerWithMiddlewares.Add(middlewares.NewMinifyMiddleware(minifyConfig))
// OpenAPI validation
if path.OpenAPI != nil {
openapiMiddleware, err := middlewares.NewOpenAPIValidationMiddleware(*path.OpenAPI)
if err != nil {
return nil, err
}
handlerWithMiddlewares.Add(openapiMiddleware)
}
// Response cache
if path.Cache {
handlerWithMiddlewares.Add(middlewares.NewCacheMiddleware())
}
return handlerWithMiddlewares, nil
}