-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsystatus.go
52 lines (43 loc) · 1.73 KB
/
systatus.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
package systatus
import (
"fmt"
"net/http"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type SystatusOptions struct {
Mux *http.ServeMux
Prefix string
ExposeEnv bool
PrettyLogger bool
HealthHandlerOpts HealthHandlerOpts
CPUHandlerOpts CPUHandlerOpts
EnvHandlerOpts EnvHandlerOpts
DiskHandlerOpts DiskHandlerOpts
UptimeHandlerOpts UptimeHandlerOpts
MemHandlerOpts MemHandlerOpts
}
func useMiddlewares(handler func(w http.ResponseWriter, r *http.Request), middlewares []func(next http.HandlerFunc) http.HandlerFunc) func(w http.ResponseWriter, r *http.Request) {
for _, mw := range middlewares {
handler = mw(handler)
}
return handler
}
func Enable(opts SystatusOptions) {
if opts.PrettyLogger {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
mux := http.DefaultServeMux
if opts.Mux != nil {
mux = opts.Mux
}
mux.HandleFunc(fmt.Sprintf("%s/health", opts.Prefix), useMiddlewares(handleHealth(opts.HealthHandlerOpts), opts.CPUHandlerOpts.Middlewares))
mux.HandleFunc(fmt.Sprintf("%s/uptime", opts.Prefix), useMiddlewares(handleUptime(opts.UptimeHandlerOpts), opts.UptimeHandlerOpts.Middlewares))
mux.HandleFunc(fmt.Sprintf("%s/cpu", opts.Prefix), useMiddlewares(handleCPU(opts.CPUHandlerOpts), opts.CPUHandlerOpts.Middlewares))
mux.HandleFunc(fmt.Sprintf("%s/mem", opts.Prefix), useMiddlewares(HandleMem(opts.MemHandlerOpts), opts.MemHandlerOpts.Middlewares))
mux.HandleFunc(fmt.Sprintf("%s/disk", opts.Prefix), useMiddlewares(handleDisk(opts.DiskHandlerOpts), opts.DiskHandlerOpts.Middlewares))
if opts.ExposeEnv {
mux.HandleFunc(fmt.Sprintf("%s/env", opts.Prefix), useMiddlewares(handleEnv(opts.EnvHandlerOpts), opts.EnvHandlerOpts.Middlewares))
}
}