1
0

An AreProcessesUp func was turned into a lambda func.

This commit is contained in:
Alexander Andreev 2024-01-13 04:55:19 +04:00
parent 37bc8b0f1b
commit 77af8817fc
Signed by: Arav
GPG Key ID: D22A817D95815393

31
main.go
View File

@ -87,7 +87,19 @@ func main() {
}
router := http.NewServeMux()
router.HandleFunc("/processes", AreProcessesUp(&conf.Processes))
router.HandleFunc("/processes", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Add("Allow", "GET")
return
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(GetProcessesState(&conf.Processes)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to encode a process list: %s\n", err)
}
})
srv := &http.Server{
Addr: conf.ListenAddress,
@ -127,20 +139,3 @@ func main() {
}
}
}
// AreProcessesUp sends back status of watched processes.
func AreProcessesUp(processes *[]Process) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Header().Add("Allow", "GET")
return
}
w.Header().Add("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(GetProcessesState(processes)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Failed to encode a process list: %s\n", err)
}
}
}