Browse Source

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

Removing methods and directly making and appending to ProcessList.
master
Alexander Andreev 7 months ago
parent
commit
a67ecc9c3d
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F
  1. 15
      server.go

15
server.go

@ -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 {

Loading…
Cancel
Save