forked from lwander/spin-kub-v2-demo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
56 lines (46 loc) · 1.16 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
package main
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func index(w http.ResponseWriter, r *http.Request) {
if rand.Int63n(100) < errRate {
w.WriteHeader(http.StatusInternalServerError)
return
}
// fmt.Printf("Handling %+v\n", r)
bs, err := ioutil.ReadFile("./content/index.html")
if err != nil {
fmt.Printf("Couldn't read index.html: %v", err)
os.Exit(1)
}
io.WriteString(w, string(bs[:]))
}
func healthz(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "ok")
}
var errRate int64 = 0
func main() {
failRate := os.Getenv("FAIL_RATE")
if failRate != "" {
var err error
errRate, err = strconv.ParseInt(failRate, 10, 64)
if err != nil {
fmt.Println(err)
}
}
http.HandleFunc("/", prometheus.InstrumentHandlerFunc("index", index))
http.HandleFunc("/healthz", prometheus.InstrumentHandlerFunc("healthz", healthz))
http.Handle("/metrics", promhttp.Handler())
port := ":8000"
fmt.Printf("Starting to service on port %s\n", port)
http.ListenAndServe(port, nil)
}