-
Notifications
You must be signed in to change notification settings - Fork 13
/
api.go
137 lines (115 loc) · 3.28 KB
/
api.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
// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package forwarder
import (
"bytes"
"context"
"net/http"
"net/http/pprof"
"sort"
"text/template"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// APIUnixSocket is the path to the Unix socket for the API server.
// It is currently only used in containerized environments.
const APIUnixSocket = "/tmp/forwarder.sock"
// APIHandler serves API endpoints.
// It provides health and readiness endpoints prometheus metrics, and pprof debug endpoints.
type APIHandler struct {
mux *http.ServeMux
ready func(ctx context.Context) bool
title string
patterns []string
}
type APIEndpoint struct {
Path string
Handler http.Handler
}
func NewAPIHandler(title string, r prometheus.Gatherer, ready func(ctx context.Context) bool, extraEndpoints ...APIEndpoint) *APIHandler {
m := http.NewServeMux()
a := &APIHandler{
mux: m,
ready: ready,
title: title,
}
var indexPatterns []string
handleFunc := func(pattern string, handler func(http.ResponseWriter, *http.Request)) {
indexPatterns = append(indexPatterns, pattern)
m.HandleFunc(pattern, handler)
}
handleFunc("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{
DisableCompression: true,
EnableOpenMetrics: true,
}).ServeHTTP)
handleFunc("/healthz", a.healthz)
handleFunc("/readyz", a.readyz)
for _, e := range extraEndpoints {
handleFunc(e.Path, e.Handler.ServeHTTP)
}
handleFunc("/debug/pprof/", pprof.Index)
m.HandleFunc("/debug/pprof/profile", pprof.Profile)
m.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
m.HandleFunc("/debug/pprof/trace", pprof.Trace)
sort.Strings(indexPatterns)
a.patterns = indexPatterns
m.HandleFunc("/", a.index)
return a
}
func (h *APIHandler) healthz(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("OK"))
}
func (h *APIHandler) readyz(w http.ResponseWriter, r *http.Request) {
if h.ready == nil || h.ready(r.Context()) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("OK"))
} else {
w.WriteHeader(http.StatusServiceUnavailable)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Service Unavailable"))
}
}
const indexTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Title}}</h1>
<ul>
{{range .Patterns}}
<li><a href="{{.}}">{{.}}</a></li>
{{end}}
</ul>
</body>
</html>
`
func (h *APIHandler) index(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
t, err := template.New("index").Parse(indexTemplate)
if err != nil {
w.Write([]byte(err.Error()))
return
}
var buf bytes.Buffer
if err := t.Execute(&buf, struct {
Title string
Patterns []string
}{
Title: h.title,
Patterns: h.patterns,
}); err != nil {
w.Write([]byte(err.Error()))
}
buf.WriteTo(w) //nolint:errcheck // ignore error
}
func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}