health check

This commit is contained in:
“nishant-sharma”
2021-03-31 10:14:54 +05:30
parent 0fdb4a8489
commit 6a79176349
2 changed files with 21 additions and 0 deletions

View File

@@ -9,6 +9,10 @@ import (
"net/http"
)
var (
healthyBool bool = true
)
func eventsHandler(w http.ResponseWriter, r *http.Request) {
var reader io.Reader
reader = r.Body
@@ -22,3 +26,18 @@ func eventsHandler(w http.ResponseWriter, r *http.Request) {
lib.RequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
io.WriteString(w, "ok")
}
func healthHandler(w http.ResponseWriter, r *http.Request){
if healthyBool {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, "true")
} else {
http.Error(w, "server unhealthy", http.StatusServiceUnavailable)
return
}
}
func healthToggleHandler(w http.ResponseWriter, r *http.Request){
healthyBool = !healthyBool
io.WriteString(w, "toggled")
}

View File

@@ -39,6 +39,8 @@ func NewServer(port string) (*Server, error) {
router := mux.NewRouter()
router.HandleFunc("/events", eventsHandler).Methods("POST")
router.HandleFunc("/health", healthHandler).Methods("GET")
router.HandleFunc("/health/toggle", healthToggleHandler).Methods("GET")
httpServer := &http.Server{Addr: ":" + port, Handler: router}
newServer := &Server{HttpServer: httpServer, Listener: listener}