-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
296 lines (254 loc) · 9.06 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"bytes"
"context"
_ "embed"
"encoding/json"
"errors"
"expvar"
"flag"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"runtime"
"runtime/debug"
"syscall"
"time"
)
func main() {
if err := run(context.Background(), os.Stdout, os.Args, Version); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
// Version is set at build time using ldflags.
// It is optional and can be omitted if not required.
// Refer to [handleGetHealth] for more information.
var Version string
// run initiates and starts the [http.Server], blocking until the context is canceled by OS signals.
// It listens on a port specified by the -port flag, defaulting to 8080.
// This function is inspired by techniques discussed in the [blog post] By Mat Ryer:
//
// [blog post]: https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years
func run(ctx context.Context, w io.Writer, args []string, version string) error {
var port uint
fs := flag.NewFlagSet(args[0], flag.ExitOnError)
fs.SetOutput(w)
fs.UintVar(&port, "port", 8080, "port for HTTP API")
if err := fs.Parse(args[1:]); err != nil {
return err
}
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
// NOTE: Removed `defer cancel()` since we want to control when to cancel the context
// We'll call it explicitly after server shutdown
// Initialize your resources here, for example:
// - Database connections
// - Message queue clients
// - Cache clients
// - External API clients
// Example:
// db, err := sql.Open(...)
// if err != nil {
// return fmt.Errorf("database init: %w", err)
// }
slog.SetDefault(slog.New(slog.NewJSONHandler(w, nil)))
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: route(slog.Default(), version),
ReadHeaderTimeout: 10 * time.Second,
}
errChan := make(chan error, 1)
go func() {
slog.InfoContext(ctx, "server started", slog.Uint64("port", uint64(port)), slog.String("version", version))
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
errChan <- err
}
}()
select {
case err := <-errChan:
return err
case <-ctx.Done():
slog.InfoContext(ctx, "shutting down server")
// Create a new context for shutdown with timeout
ctx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
// Shutdown the HTTP server first
if err := server.Shutdown(ctx); err != nil {
return fmt.Errorf("server shutdown: %w", err)
}
// After server is shutdown, cancel the main context to close other resources
cancel()
// Add cleanup code here, in reverse order of initialization
// Give each cleanup operation its own timeout if needed
// Example cleanup sequence:
// 1. Close application services that depend on other resources
// if err := myService.Shutdown(ctx); err != nil {
// return fmt.Errorf("service shutdown: %w", err)
// }
// 2. Close message queue connections
// if err := mqClient.Close(); err != nil {
// return fmt.Errorf("mq shutdown: %w", err)
// }
// 3. Close cache connections
// if err := cacheClient.Close(); err != nil {
// return fmt.Errorf("cache shutdown: %w", err)
// }
// 4. Close database connections
// if err := db.Close(); err != nil {
// return fmt.Errorf("database shutdown: %w", err)
// }
return nil
}
}
// route sets up and returns an [http.Handler] for all the server routes.
// It is the single source of truth for all the routes.
// You can add custom [http.Handler] as needed.
func route(log *slog.Logger, version string) http.Handler {
mux := http.NewServeMux()
mux.Handle("GET /health", handleGetHealth(version))
mux.Handle("GET /openapi.yaml", handleGetOpenAPI(version))
mux.Handle("/debug/", handleGetDebug())
handler := accesslog(mux, log)
handler = recovery(handler, log)
return handler
}
// handleGetHealth returns an [http.HandlerFunc] that responds with the health status of the service.
// It includes the service version, VCS revision, build time, and modified status.
// The service version can be set at build time using the VERSION variable (e.g., 'make build VERSION=v1.0.0').
func handleGetHealth(version string) http.HandlerFunc {
type responseBody struct {
Version string `json:"Version"`
Uptime string `json:"Uptime"`
LastCommitHash string `json:"LastCommitHash"`
LastCommitTime time.Time `json:"LastCommitTime"`
DirtyBuild bool `json:"DirtyBuild"`
}
res := responseBody{Version: version}
buildInfo, _ := debug.ReadBuildInfo()
for _, kv := range buildInfo.Settings {
if kv.Value == "" {
continue
}
switch kv.Key {
case "vcs.revision":
res.LastCommitHash = kv.Value
case "vcs.time":
res.LastCommitTime, _ = time.Parse(time.RFC3339, kv.Value)
case "vcs.modified":
res.DirtyBuild = kv.Value == "true"
}
}
up := time.Now()
return func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
res.Uptime = time.Since(up).String()
if err := json.NewEncoder(w).Encode(res); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
// handleGetDebug returns an [http.Handler] for debug routes, including pprof and expvar routes.
func handleGetDebug() http.Handler {
mux := http.NewServeMux()
// NOTE: this route is same as defined in net/http/pprof init function
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
// NOTE: this route is same as defined in expvar init function
mux.Handle("/debug/vars", expvar.Handler())
return mux
}
// handleGetOpenAPI returns an [http.HandlerFunc] that serves the OpenAPI specification YAML file.
// The file is embedded in the binary using the go:embed directive.
func handleGetOpenAPI(version string) http.HandlerFunc {
body := bytes.Replace(openAPI, []byte("${{ VERSION }}"), []byte(version), 1)
return func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/yaml")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(body)
}
}
// openAPI holds the embedded OpenAPI YAML file.
// Remove this and the api/openapi.yaml file if you prefer not to serve OpenAPI.
//
//go:embed api/openapi.yaml
var openAPI []byte
// accesslog is a middleware that logs request and response details,
// including latency, method, path, query parameters, IP address, response status, and bytes sent.
func accesslog(next http.Handler, log *slog.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wr := responseRecorder{ResponseWriter: w}
next.ServeHTTP(&wr, r)
log.InfoContext(r.Context(), "accessed",
slog.String("latency", time.Since(start).String()),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.String("query", r.URL.RawQuery),
slog.String("ip", r.RemoteAddr),
slog.Int("status", wr.status),
slog.Int("bytes", wr.numBytes))
})
}
// recovery is a middleware that recovers from panics during HTTP handler execution and logs the error details.
// It must be the last middleware in the chain to ensure it captures all panics.
func recovery(next http.Handler, log *slog.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wr := responseRecorder{ResponseWriter: w}
defer func() {
err := recover()
if err == nil {
return
}
if err, ok := err.(error); ok && errors.Is(err, http.ErrAbortHandler) {
// Handle the abort gracefully
return
}
stack := make([]byte, 1024)
n := runtime.Stack(stack, true)
log.ErrorContext(r.Context(), "panic!",
slog.Any("error", err),
slog.String("stack", string(stack[:n])),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.String("query", r.URL.RawQuery),
slog.String("ip", r.RemoteAddr))
if wr.status > 0 {
// response was already sent, nothing we can do
return
}
// send error response
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
}()
next.ServeHTTP(&wr, r)
})
}
// responseRecorder is a wrapper around [http.ResponseWriter] that records the status and bytes written during the response.
// It implements the [http.ResponseWriter] interface by embedding the original ResponseWriter.
type responseRecorder struct {
http.ResponseWriter
status int
numBytes int
}
// Header implements the [http.ResponseWriter] interface.
func (re *responseRecorder) Header() http.Header {
return re.ResponseWriter.Header()
}
// Write implements the [http.ResponseWriter] interface.
func (re *responseRecorder) Write(b []byte) (int, error) {
re.numBytes += len(b)
return re.ResponseWriter.Write(b)
}
// WriteHeader implements the [http.ResponseWriter] interface.
func (re *responseRecorder) WriteHeader(statusCode int) {
re.status = statusCode
re.ResponseWriter.WriteHeader(statusCode)
}