1
0
Fork 0

[server.go] Use switch statement by Accept header.

Removing methods and directly making and appending to ProcessList.
This commit is contained in:
Alexander Andreev 2022-01-06 03:17:26 +04:00
parent 006b832574
commit a67ecc9c3d
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
1 changed files with 7 additions and 8 deletions

View File

@ -46,32 +46,31 @@ func ShutdownHTTPServer(srv *http.Server) {
func AreProcessesUp(processes *[]string, indented bool) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
proclist := NewProcessList()
proclist := make(ProcessList)
for _, proc := range *processes {
pids, err := GetProcessPIDs(proc)
proclist.AddProcess(proc, err == nil && len(pids) > 0)
proclist[proc] = err == nil && len(pids) > 0
}
hdr := r.Header.Get("Accept")
if hdr == "application/xml" {
switch r.Header.Get("Accept") {
case "application/xml":
w.Header().Add("Content-Type", "application/xml")
enc := xml.NewEncoder(w)
if indented {
enc.Indent("", "\t")
}
enc.Encode(proclist)
} else if hdr == "text/plain" {
case "text/plain":
w.Header().Add("Content-Type", "text/plain")
var s []string
for k, v := range *proclist {
for k, v := range proclist {
if v {
s = append(s, k)
}
}
w.Write([]byte(strings.Join(s, ",")))
} else {
default:
w.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(w)
if indented {