firestormy/server/list.go

48 lines
1.3 KiB
Go
Executable File

package server
import (
"encoding/json"
"net/http"
"sort"
"gitea.inhome.blapointe.com/local/firestormy/config"
"gitea.inhome.blapointe.com/local/firestormy/config/ns"
"gitea.inhome.blapointe.com/local/firestormy/scheduler"
"gitea.inhome.blapointe.com/local/logb"
)
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
logb.Debugf("[access] list %s", r.URL.Path)
jobs, err := config.Store.List(ns.Jobs)
logb.Debugf("[access] list : %v: %+v", err, jobs)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out := make([]map[string]interface{}, len(jobs))
for i, job := range jobs {
out[i] = make(map[string]interface{})
b, err := config.Store.Get(job, ns.Jobs...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
j := &scheduler.Job{}
if err := j.Decode(b); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out[i] = toMap(j, false)
}
sort.Slice(out, func(i, j int) bool {
if out[i]["disabled"].(bool) == out[j]["disabled"].(bool) {
} else if out[i]["disabled"].(bool) {
return false
} else if out[j]["disabled"].(bool) {
return true
}
return out[i]["title"].(string) < out[j]["title"].(string)
})
json.NewEncoder(w).Encode(out)
}