Fix job encode decode
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"local/firestormy/config"
|
||||
"local/firestormy/config/ns"
|
||||
"local/firestormy/scheduler"
|
||||
"local/router"
|
||||
"log"
|
||||
"net/http"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func (s *Server) Routes() error {
|
||||
@@ -12,6 +18,14 @@ func (s *Server) Routes() error {
|
||||
path string
|
||||
handler http.HandlerFunc
|
||||
}{
|
||||
{
|
||||
path: fmt.Sprintf("/upserts"),
|
||||
handler: s.gzip(s.authenticate(s.upserts)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("/list"),
|
||||
handler: s.gzip(s.authenticate(s.list)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("%s%s", wildcard, wildcard),
|
||||
handler: s.gzip(s.authenticate(s.static)),
|
||||
@@ -29,3 +43,47 @@ func (s *Server) Routes() error {
|
||||
func (s *Server) static(w http.ResponseWriter, r *http.Request) {
|
||||
s.fileServer.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) upserts(w http.ResponseWriter, r *http.Request) {
|
||||
upsert, err := newUpsertRequest(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
log.Println("received", upsert)
|
||||
http.Error(w, "not impl", http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
|
||||
jobs, err := config.Store.List(ns.Jobs)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
sort.Strings(jobs)
|
||||
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]["id"] = j.Name
|
||||
out[i]["cron"] = j.Schedule
|
||||
out[i]["language"] = j.Runner.String()
|
||||
out[i]["script"] = j.Raw
|
||||
out[i]["last"] = map[string]interface{}{
|
||||
"run": j.LastRun,
|
||||
"runtime": j.LastRuntime,
|
||||
"output": j.LastOutput,
|
||||
"status": j.LastStatus,
|
||||
}
|
||||
}
|
||||
json.NewEncoder(w).Encode(out)
|
||||
}
|
||||
|
||||
46
server/upserts.go
Executable file
46
server/upserts.go
Executable file
@@ -0,0 +1,46 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"local/firestormy/config"
|
||||
"local/firestormy/config/ns"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type upsertRequest struct {
|
||||
ID string `json:"id"`
|
||||
Language string `json:"language"`
|
||||
Cron string `json:"cron"`
|
||||
Script string `json:"script"`
|
||||
}
|
||||
|
||||
func newUpsertRequest(r io.Reader) (upsertRequest, error) {
|
||||
u := upsertRequest{}
|
||||
if err := json.NewDecoder(r).Decode(&u); err != nil {
|
||||
return u, err
|
||||
}
|
||||
err := u.validate()
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (u *upsertRequest) validate() error {
|
||||
if u.ID == "" {
|
||||
u.ID = uuid.New().String()
|
||||
} else if _, err := config.Store.Get(u.ID, ns.Jobs...); err != nil {
|
||||
return fmt.Errorf("ID provided but not accessible: %v", err)
|
||||
}
|
||||
if u.Language == "" {
|
||||
return errors.New("language required")
|
||||
}
|
||||
if u.Cron == "" {
|
||||
return errors.New("cron required")
|
||||
}
|
||||
if u.Script == "" {
|
||||
return errors.New("script required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user