-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (36 loc) · 955 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type healthInfo struct {
Service string `json:"service"`
Status string `json:"status"`
Version string `json:"version"`
Uptime string `json:"uptime"`
}
var startTime = time.Now()
func main() {
http.HandleFunc("/", helloWorld)
http.HandleFunc("/healthz", healthStatus)
http.ListenAndServe(":8080", nil)
}
func helloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello!")
}
func healthStatus(w http.ResponseWriter, r *http.Request) {
healthInfo := healthInfo{}
healthInfo.Service = "Hello World Service"
healthInfo.Status = "OK"
healthInfo.Version = "0.0.1"
healthInfo.Uptime = fmt.Sprint("up since ", startTime.Format("2006-01-02 15:04:05"))
js, err := json.MarshalIndent(healthInfo,""," ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}