-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
176 lines (142 loc) · 3.63 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
package health
import (
"context"
"errors"
"log/slog"
"net"
"net/http"
"net/netip"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/go-http-utils/headers"
"github.com/go-http-utils/negotiator"
sauté "gitlab.com/biffen/saute"
"go.opentelemetry.io/otel/codes"
)
const (
// ContentType is the media (MIME) type returned by the server.
ContentType = "application/health+json"
// EnvHealthPort is ‘HEALTH_PORT’; the environment variable to configure the
// health check HTTP port.
EnvHealthPort = "HEALTH_PORT"
)
var (
server *http.Server
serverMu sync.Mutex
)
// HandleHTTP serves a health response over HTTP. It supports the GET, HEAD and
// OPTIONS methods as well as content negotiation.
func HandleHTTP(w http.ResponseWriter, req *http.Request) {
ctx, span := sauté.TraceFunc(req.Context(), nil)
defer span.End()
errorStatus := func(err error, status int) {
if err == nil {
http.Error(w, "", status)
span.SetStatus(codes.Error, http.StatusText(status))
return
}
http.Error(w, err.Error(), status)
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
switch req.Method {
case http.MethodOptions:
w.Header().Set(headers.Allow, strings.Join([]string{
http.MethodGet,
http.MethodHead,
http.MethodOptions,
}, ", "))
w.WriteHeader(http.StatusNoContent)
return
case http.MethodGet, http.MethodHead:
default:
errorStatus(nil, http.StatusMethodNotAllowed)
return
}
negotiator := negotiator.New(req.Header)
if negotiator.Charset("UTF-8") == "" {
http.Error(w, "", http.StatusNotAcceptable)
return
}
ct := negotiator.Type(ContentType, "application/json")
if ct == "" {
errorStatus(nil, http.StatusNotAcceptable)
return
}
w.Header().Set(headers.ContentEncoding, "UTF-8")
w.Header().Set(headers.ContentType, ct)
resp, err := CheckNow(ctx)
if err != nil {
errorStatus(err, http.StatusInternalServerError)
}
if resp.Status == StatusFail {
w.WriteHeader(http.StatusInternalServerError)
}
if req.Method == http.MethodHead {
w.Header().Set(headers.ContentLength, "0")
return
}
if _, err := resp.Write(w); err != nil {
errorStatus(err, http.StatusInternalServerError)
}
}
// StartServer starts an HTTP server at 0.0.0.0:${HEALTH_PORT:-9999} serving
// health checks. Can be called multiple times but will only start one server.
//
// Will block until the server is listening.
//
// The server will be stopped when the passed [context.Context] is cancelled.
func StartServer(ctx context.Context) error {
serverMu.Lock()
defer serverMu.Unlock()
if server == nil {
addr := netip.AddrPortFrom(netip.IPv4Unspecified(), port())
listener, err := net.Listen("tcp", addr.String())
if err != nil {
return err
}
mux := http.NewServeMux()
mux.HandleFunc("GET /{$}", HandleHTTP)
server = &http.Server{
Addr: addr.String(),
Handler: mux,
ReadHeaderTimeout: 30 * time.Second,
}
go func() {
if err := server.Serve(listener); err != nil &&
!errors.Is(err, http.ErrServerClosed) {
slog.ErrorContext(ctx, "health server error",
slog.Any("error", err),
)
}
serverMu.Lock()
defer serverMu.Unlock()
server = nil
}()
go func() {
<-ctx.Done()
if err := server.Shutdown(context.Background()); err != nil {
slog.ErrorContext(ctx, "error when stopping health server",
slog.Any("error", err),
)
}
}()
}
return nil
}
func port() uint16 {
if str := os.Getenv(EnvHealthPort); str != "" {
u, err := strconv.ParseUint(str, 0, 16)
if err == nil {
return uint16(u)
}
slog.Error("failed to parse "+EnvHealthPort,
logSubsystem,
slog.Any("error", err),
)
}
return 9_999
}