-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (99 loc) · 3.46 KB
/
main.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
// A KrakenD server plugin which uses the Firetail Go Library to forward request and response data to the Firetail SaaS.
// Derived from the example KrakenD http server plugin provided here:
// https://www.krakend.io/docs/extending/http-server-plugins/
package main
import (
"context"
"errors"
"fmt"
"net/http"
firetail "github.com/FireTail-io/firetail-go-lib/middlewares/http"
)
const pluginName = "firetail-krakend-plugin"
type registerer string
// the symbol the plugin loader will try to load
var HandlerRegisterer = registerer(pluginName)
func (r registerer) RegisterHandlers(f func(name string, handler func(context.Context, map[string]interface{}, http.Handler) (http.Handler, error))) {
f(string(r), r.registerHandlers)
}
func (r registerer) registerHandlers(_ context.Context, extra map[string]interface{}, h http.Handler) (http.Handler, error) {
// This plugin requires config, so return an err if not found in the krakend.json's extra_config
config, ok := extra[pluginName].(map[string]interface{})
if !ok {
return h, errors.New("configuration not found")
}
// Extract options from config
options, err := extractOptions(config)
if err != nil {
return nil, err
}
// Create firetail middleware
firetailMiddleware, err := firetail.GetMiddleware(options)
if err != nil {
return nil, err
}
// Return the handler wrapped in the firetail middleware
return firetailMiddleware(h), nil
}
func extractOptions(config map[string]interface{}) (*firetail.Options, error) {
// This is ugly. Slightly annoying KrakenD gives us map[string]interface{}
options := firetail.Options{}
logsApiToken, hasLogsApiToken := config["logs-api-token"]
if hasLogsApiToken {
logsApiTokenString, ok := logsApiToken.(string)
if !ok {
return nil, fmt.Errorf(
"logs-api-token must be of type string; got '%#v' (%T)",
logsApiToken, logsApiToken,
)
}
options.LogsApiToken = logsApiTokenString
}
logsApiUrl, hasLogsApiUrl := config["logs-api-url"]
if hasLogsApiUrl {
logsApiUrlString, ok := logsApiUrl.(string)
if !ok {
return nil, fmt.Errorf(
"logs-api-url must be of type string; got '%#v' (%T)",
logsApiUrl, logsApiUrl,
)
}
options.LogsApiUrl = logsApiUrlString
}
openapiSpecPath, hasOpenApiSpecPath := config["openapi-spec-path"]
if hasOpenApiSpecPath {
openapiSpecPathString, ok := openapiSpecPath.(string)
if !ok {
return nil, fmt.Errorf(
"openapi-spec-path must be of type string; got '%#v' (%T)",
openapiSpecPath, openapiSpecPath,
)
}
options.OpenapiSpecPath = openapiSpecPathString
}
enableRequestValidation, hasEnableRequestValidation := config["enable-request-validation"]
if hasEnableRequestValidation {
enableRequestValidationBool, ok := enableRequestValidation.(bool)
if !ok {
return nil, errors.New("enable-request-validation must be of type bool")
}
options.EnableRequestValidation = enableRequestValidationBool
}
enableResponseValidation, hasEnableResponseValidation := config["enable-response-validation"]
if hasEnableResponseValidation {
enableResponseValidationBool, ok := enableResponseValidation.(bool)
if !ok {
return nil, errors.New("enable-response-validation must be of type bool")
}
options.EnableResponseValidation = enableResponseValidationBool
}
debugErrs, hasDebugErrs := config["debug-errs"]
if hasDebugErrs {
debugErrsBool, ok := debugErrs.(bool)
if !ok {
return nil, errors.New("debug-errs must be of type bool")
}
options.DebugErrs = debugErrsBool
}
return &options, nil
}