[server.go] Use switch statement by Accept header.
Removing methods and directly making and appending to ProcessList.
This commit is contained in:
parent
006b832574
commit
a67ecc9c3d
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…
Reference in New Issue
Block a user