-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhttp.go
105 lines (84 loc) · 2.09 KB
/
http.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/blind-oracle/psql-streamer/common"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var httpServer *http.Server
func httpInit() {
r := http.NewServeMux()
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Alive and well\n")
})
r.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
problem := false
body := ""
for _, v := range commonObjs {
err := v.Status()
if err != nil {
problem = true
}
body += common.LoggerHeader(v) + err.Error()
}
if problem {
w.WriteHeader(500)
}
// Make linter happy
_, _ = w.Write([]byte(body))
})
r.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
for _, s := range gatherStats() {
fmt.Fprintf(w, "%s\n", s)
}
})
r.HandleFunc("/debug/", func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) != 3 {
w.WriteHeader(400)
fmt.Fprintf(w, "Request should be in form /debug/[off|on]\n")
return
}
var debug bool
switch parts[2] {
case "on":
debug = true
case "off":
debug = false
default:
w.WriteHeader(400)
fmt.Fprintf(w, "Debug can be [on] or [off]\n")
return
}
for _, v := range commonObjs {
v.SetDebug(debug)
}
fmt.Fprintf(w, "Debug is now %t\n", debug)
})
r.Handle("/metrics", promhttp.Handler())
httpServer = &http.Server{
Addr: cfg.HTTP.Listen,
Handler: r,
}
go func() {
if err := httpServer.ListenAndServe(); err == http.ErrServerClosed {
log.Println("HTTP was shut down gracefully")
} else if err != nil {
log.Fatalf("HTTP: Failed to listen on %s: %v", cfg.HTTP.Listen, err)
}
}()
time.Sleep(10 * time.Millisecond) // Give it time to die
log.Printf("HTTP listening to %s", cfg.HTTP.Listen)
}
func httpShutdown() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := httpServer.Shutdown(ctx); err != nil {
log.Printf("Unable to shutdown HTTP: %s", err)
}
cancel()
log.Println("HTTP shutdown finished")
}