-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
66 lines (55 loc) · 1.34 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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
c := os.Getenv("COLOR")
if len(c) == 0 {
c = "cyan"
}
addr := os.Getenv("LISTEN")
if len(addr) == 0 {
addr = ":8080"
}
count := 0
m := http.NewServeMux()
s := http.Server{Addr: addr, Handler: m}
log.Printf("Server started\n")
// Healthcheck endpoint
m.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
})
// Simulate failure
boom, _ := ioutil.ReadFile("public/shutdown.html")
m.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(boom))
log.Printf("Received shutdown request\n")
go func() {
if err := s.Shutdown(context.Background()); err != nil {
log.Fatal(err)
}
}()
})
// Dashboard
dashboard, _ := ioutil.ReadFile("public/dashboard.html")
m.HandleFunc("/dashboard", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(dashboard))
log.Printf("GET %s\n", r.URL.Path)
})
// Default
index, _ := ioutil.ReadFile("public/index.html")
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
count += 1
fmt.Fprintf(w, string(index), c, count)
//log.Printf("GET %s\n", r.URL.Path)
})
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
log.Printf("Exiting")
}