hw/cmd/hw/main.go

58 lines
1.3 KiB
Go
Raw Permalink Normal View History

2018-01-09 16:26:12 -08:00
package main
import (
"encoding/json"
2018-02-01 17:36:00 -08:00
"fmt"
2018-01-09 16:26:12 -08:00
"log"
2018-03-08 13:45:07 -08:00
"math/rand"
2018-01-09 16:26:12 -08:00
"net/http"
2018-01-10 09:49:42 -08:00
"os"
2018-03-08 13:45:07 -08:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2018-03-08 14:08:37 -08:00
"mcquay.me/hw"
2018-03-08 13:45:07 -08:00
"mcquay.me/metrics"
2018-01-09 16:26:12 -08:00
)
func main() {
2018-03-08 13:45:07 -08:00
m, err := metrics.New("hw")
if err != nil {
log.Fatalf("metrics: %v", err)
}
2018-01-10 09:49:42 -08:00
hn, err := os.Hostname()
if err != nil {
log.Fatalf("hostname: %+v", err)
}
2018-03-08 13:45:07 -08:00
http.HandleFunc("/", m.WrapFunc("/", func(w http.ResponseWriter, req *http.Request) {
2018-01-09 16:26:12 -08:00
w.Header().Set("Content-Type", "application/json")
2018-03-08 16:15:44 -08:00
r := hw.V{
2018-01-10 09:49:42 -08:00
Hostname: hn,
2018-03-08 14:48:25 -08:00
V: hw.Version,
2018-03-08 16:16:14 -08:00
G: hw.Git,
2018-01-10 09:49:42 -08:00
}
2018-01-09 16:26:12 -08:00
if err := json.NewEncoder(w).Encode(r); err != nil {
log.Printf("json: %+v", err)
}
2018-03-08 13:45:07 -08:00
}))
http.HandleFunc("/env", m.WrapFunc("/env", func(w http.ResponseWriter, req *http.Request) {
2018-02-01 17:36:00 -08:00
for _, line := range os.Environ() {
fmt.Fprintf(w, "%v\n", line)
}
2018-03-08 13:45:07 -08:00
}))
codes := []int{
http.StatusBadGateway,
http.StatusBadRequest,
http.StatusUnauthorized,
}
http.HandleFunc("/bad", m.WrapFunc("/bad", func(w http.ResponseWriter, req *http.Request) {
code := codes[rand.Intn(len(codes))]
w.WriteHeader(code)
}))
2018-03-08 14:08:37 -08:00
http.HandleFunc("/live", m.WrapFunc("/live", hw.OK))
http.HandleFunc("/ready", m.WrapFunc("/ready", hw.OK))
2018-03-08 13:45:07 -08:00
http.Handle("/metrics", promhttp.Handler())
2018-01-09 16:26:12 -08:00
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("listen and serve: %v", err)
}
}