Add health with update on POST

This commit is contained in:
Julien Salleyron
2017-02-06 16:41:38 +01:00
parent 1362371fa1
commit 531a7e2b13

24
app.go
View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/websocket"
// "github.com/pkg/profile"
"log"
@ -32,6 +33,7 @@ func main() {
http.HandleFunc("/bench", benchHandler)
http.HandleFunc("/", whoamI)
http.HandleFunc("/api", api)
http.HandleFunc("/health", healthHandler)
fmt.Println("Starting up on port " + port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
@ -127,3 +129,25 @@ func api(w http.ResponseWriter, req *http.Request) {
}
json.NewEncoder(w).Encode(data)
}
type healthState struct {
StatusCode int
}
var currentHealthState = healthState{200}
func healthHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
var statusCode int
err := json.NewDecoder(req.Body).Decode(&statusCode)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
} else {
fmt.Printf("Update health check status code [%d]\n", statusCode)
currentHealthState.StatusCode = statusCode
}
} else {
w.WriteHeader(currentHealthState.StatusCode)
}
}