An AreProcessesUp func was turned into a lambda func.
This commit is contained in:
parent
37bc8b0f1b
commit
77af8817fc
31
main.go
31
main.go
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user