2022-01-02 21:30:56 +04:00
|
|
|
package main
|
2021-12-24 23:28:50 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-01-02 21:56:35 +04:00
|
|
|
"strings"
|
2021-12-24 23:28:50 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-01-02 02:56:46 +04:00
|
|
|
func CreateAndStartHTTPServer(conf *Configuration) *http.Server {
|
2021-12-24 23:28:50 +04:00
|
|
|
router := http.NewServeMux()
|
2022-01-02 21:30:56 +04:00
|
|
|
router.HandleFunc("/processes", AreProcessesUp(&conf.Processes, conf.IndentedOutput))
|
2021-12-24 23:28:50 +04:00
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: conf.ListenAddress,
|
|
|
|
Handler: router,
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 5 * time.Second,
|
|
|
|
IdleTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatalf("ListenAndServe: %s\n", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2022-01-02 02:56:46 +04:00
|
|
|
func ShutdownHTTPServer(srv *http.Server) {
|
2021-12-24 23:28:50 +04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
srv.SetKeepAlivesEnabled(false)
|
|
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AreProcessesUp handles a GET /processes request and sends back status of given
|
|
|
|
// processes.
|
2022-01-02 21:30:56 +04:00
|
|
|
func AreProcessesUp(processes *[]string, indented bool) func(http.ResponseWriter, *http.Request) {
|
2021-12-24 23:28:50 +04:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodGet {
|
2022-01-06 03:17:26 +04:00
|
|
|
proclist := make(ProcessList)
|
2021-12-24 23:28:50 +04:00
|
|
|
|
|
|
|
for _, proc := range *processes {
|
2022-01-02 05:26:24 +04:00
|
|
|
pids, err := GetProcessPIDs(proc)
|
2022-01-06 03:17:26 +04:00
|
|
|
proclist[proc] = err == nil && len(pids) > 0
|
2021-12-24 23:28:50 +04:00
|
|
|
}
|
|
|
|
|
2022-01-06 03:17:26 +04:00
|
|
|
switch r.Header.Get("Accept") {
|
|
|
|
case "application/xml":
|
2021-12-24 23:28:50 +04:00
|
|
|
w.Header().Add("Content-Type", "application/xml")
|
|
|
|
enc := xml.NewEncoder(w)
|
2022-01-02 21:30:56 +04:00
|
|
|
if indented {
|
|
|
|
enc.Indent("", "\t")
|
|
|
|
}
|
2021-12-24 23:28:50 +04:00
|
|
|
enc.Encode(proclist)
|
2022-01-06 03:17:26 +04:00
|
|
|
case "text/plain":
|
2022-01-02 21:56:35 +04:00
|
|
|
w.Header().Add("Content-Type", "text/plain")
|
|
|
|
var s []string
|
2022-01-06 03:17:26 +04:00
|
|
|
for k, v := range proclist {
|
2022-01-02 21:56:35 +04:00
|
|
|
if v {
|
|
|
|
s = append(s, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Write([]byte(strings.Join(s, ",")))
|
2022-01-06 03:17:26 +04:00
|
|
|
default:
|
2021-12-24 23:28:50 +04:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
enc := json.NewEncoder(w)
|
2022-01-02 21:30:56 +04:00
|
|
|
if indented {
|
|
|
|
enc.SetIndent("", "\t")
|
|
|
|
}
|
2021-12-24 23:28:50 +04:00
|
|
|
enc.Encode(proclist)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
w.Header().Add("Allow", "GET")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|