56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// TasksHandler ...
|
|
func (s *Server) TasksHandler() httprouter.Handle {
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
tasks := s.tasks.Tasks()
|
|
|
|
data, err := json.Marshal(tasks)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(data)
|
|
}
|
|
}
|
|
|
|
// TaskHandler ...
|
|
func (s *Server) TaskHandler() httprouter.Handle {
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
uuid := p.ByName("uuid")
|
|
|
|
if uuid == "" {
|
|
log.Warn("no task uuid provided")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
t, ok := s.tasks.Lookup(uuid)
|
|
if !ok {
|
|
log.Warnf("no task found by uuid: %s", uuid)
|
|
http.Error(w, "Task Not Found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
data, err := json.Marshal(t.Result())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write(data)
|
|
|
|
}
|
|
}
|