1
0

Moved AreProcessesUp() handler to main.go.

This commit is contained in:
Alexander Andreev 2023-12-15 04:02:56 +04:00
parent df3714d071
commit e1f7dd81b6
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 18 additions and 24 deletions

24
http.go
View File

@ -1,24 +0,0 @@
package main
import (
"encoding/json"
"log"
"net/http"
)
// 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)
}
}
}

18
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -74,3 +75,20 @@ 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)
}
}
}