-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (36 loc) · 840 Bytes
/
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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
sm := http.NewServeMux()
sm.Handle("/health", http.HandlerFunc(healthHandler))
server := &http.Server{
Addr: ":8080",
Handler: sm,
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
log.Printf("listening on port 8080")
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
sig := <-signalChan
log.Printf("%s signal caught, shutting down", sig)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("shutdown failed: %v", err)
}
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("HEALTHY"))
}