This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (90 loc) · 2.44 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
package main
import (
_ "embed"
"net/http"
"os"
"github.com/oslokommune/okctl-hello/pkg/communicationtest"
"github.com/oslokommune/okctl-hello/pkg/killswitch"
"github.com/oslokommune/okctl-hello/pkg/loadtest"
"github.com/oslokommune/okctl-hello/pkg/health"
"github.com/oslokommune/okctl-hello/pkg/logging"
"github.com/sirupsen/logrus"
"github.com/oslokommune/okctl-hello/pkg/content"
"github.com/oslokommune/okctl-hello/pkg/monitoring"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//go:embed public/index.html
var indexHtml []byte
//go:embed public/logo.png
var logo []byte
var logger = &logrus.Logger{
Out: os.Stdout,
Formatter: &logrus.JSONFormatter{},
Level: logrus.InfoLevel,
}
type route struct {
path string
handler http.Handler
}
var routes = []route{
{
path: "/",
handler: monitoring.NewHitCounterMiddleware(logging.NewLoggingMiddleware(
logger,
content.StaticHtmlHandler(indexHtml),
)),
},
{
path: "/logo.png",
handler: monitoring.NewOlliCounterMiddleware(content.LogoHandler(logo)),
},
{
path: "/metrics",
handler: promhttp.Handler(),
},
{
path: "/health",
handler: func() http.Handler {
if debug := os.Getenv("DEBUG"); debug == "true" {
return health.DebugHandler(logger)
}
return health.HandlerFunc()
}(),
},
{
path: "/burn-cpu",
handler: loadtest.HandlerFunc(),
},
{
path: "/commtest",
handler: communicationtest.HandlerFunc(),
},
{
path: "/kill",
handler: killswitch.HandlerFunc(logger),
},
}
func main() {
server := http.NewServeMux()
for _, route := range routes {
server.Handle(route.path, route.handler)
}
if rawDSN := os.Getenv("DSN"); rawDSN != "" {
logger.Info("Found DSN. Enabling Postgres integration")
err := enablePostgres(server, logger, rawDSN)
if err != nil {
logger.Fatal(err)
}
} else {
logger.Info("No DSN found. Ignoring Postgres integration")
server.HandleFunc("/postgres/read", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write([]byte("Postgres integration is disabled. Use the DSN environment variable to activate."))
})
server.HandleFunc("/postgres/write", func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
_, _ = writer.Write([]byte("Postgres integration is disabled. Use the DSN environment variable to activate."))
})
}
logger.Fatal(http.ListenAndServe(":3000", server))
}