From 77af8817fc048465babe0f579b009ab4e4c86ca5 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 13 Jan 2024 04:55:19 +0400 Subject: [PATCH] An AreProcessesUp func was turned into a lambda func. --- main.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index a1b633e..e980e6d 100644 --- a/main.go +++ b/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) - } - } -}