health check
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user