forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_endpoints.go
91 lines (76 loc) · 2.33 KB
/
http_endpoints.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"github.com/Jeffail/benthos/lib/util/service"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
yaml "gopkg.in/yaml.v2"
)
//------------------------------------------------------------------------------
var endpoints []string
func registerEndpoint(path string, handler http.HandlerFunc) {
endpoints = append(endpoints, path)
http.HandleFunc(path, handler)
http.HandleFunc("/benthos"+path, handler)
}
func handleVersion(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("{\"version\":\"%v\", \"built\":\"%v\"}", service.Version, service.DateBuilt)))
}
func handleEndpoints(w http.ResponseWriter, r *http.Request) {
res := struct {
Endpoints []string `json:"endpoints"`
}{
Endpoints: endpoints,
}
resBytes, err := json.Marshal(res)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
} else {
w.Write(resBytes)
}
}
func registerHTTPEndpoints(
conf Config,
log log.Modular,
stats metrics.Type,
) {
handlePing := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
}
handleStackTrace := func(w http.ResponseWriter, r *http.Request) {
stackSlice := make([]byte, 1024*100)
s := runtime.Stack(stackSlice, true)
w.Write(stackSlice[:s])
}
handlePrintJSONConfig := func(w http.ResponseWriter, r *http.Request) {
resBytes, err := json.Marshal(conf)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
w.Write(resBytes)
}
handlePrintYAMLConfig := func(w http.ResponseWriter, r *http.Request) {
resBytes, err := yaml.Marshal(conf)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
w.Write(resBytes)
}
registerEndpoint("/config/json", handlePrintJSONConfig)
registerEndpoint("/config/yaml", handlePrintYAMLConfig)
registerEndpoint("/stack", handleStackTrace)
registerEndpoint("/ping", handlePing)
registerEndpoint("/version", handleVersion)
registerEndpoint("/endpoints", handleEndpoints)
// If we want to expose a JSON stats endpoint we register the endpoints.
if wHandlerFunc, ok := stats.(metrics.WithHandlerFunc); ok {
registerEndpoint("/stats", wHandlerFunc.HandlerFunc())
registerEndpoint("/metrics", wHandlerFunc.HandlerFunc())
}
}
//------------------------------------------------------------------------------