[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) {
|
func AreProcessesUp(processes *[]string, indented bool) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
proclist := NewProcessList()
|
proclist := make(ProcessList)
|
||||||
|
|
||||||
for _, proc := range *processes {
|
for _, proc := range *processes {
|
||||||
pids, err := GetProcessPIDs(proc)
|
pids, err := GetProcessPIDs(proc)
|
||||||
proclist.AddProcess(proc, err == nil && len(pids) > 0)
|
proclist[proc] = err == nil && len(pids) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
hdr := r.Header.Get("Accept")
|
switch r.Header.Get("Accept") {
|
||||||
|
case "application/xml":
|
||||||
if hdr == "application/xml" {
|
|
||||||
w.Header().Add("Content-Type", "application/xml")
|
w.Header().Add("Content-Type", "application/xml")
|
||||||
enc := xml.NewEncoder(w)
|
enc := xml.NewEncoder(w)
|
||||||
if indented {
|
if indented {
|
||||||
enc.Indent("", "\t")
|
enc.Indent("", "\t")
|
||||||
}
|
}
|
||||||
enc.Encode(proclist)
|
enc.Encode(proclist)
|
||||||
} else if hdr == "text/plain" {
|
case "text/plain":
|
||||||
w.Header().Add("Content-Type", "text/plain")
|
w.Header().Add("Content-Type", "text/plain")
|
||||||
var s []string
|
var s []string
|
||||||
for k, v := range *proclist {
|
for k, v := range proclist {
|
||||||
if v {
|
if v {
|
||||||
s = append(s, k)
|
s = append(s, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.Write([]byte(strings.Join(s, ",")))
|
w.Write([]byte(strings.Join(s, ",")))
|
||||||
} else {
|
default:
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
enc := json.NewEncoder(w)
|
enc := json.NewEncoder(w)
|
||||||
if indented {
|
if indented {
|
||||||
|
Loading…
Reference in New Issue
Block a user