Add a /tasks endpoing and extra metdata to crawl tasks
This commit is contained in:
55
internal/task_handlers.go
Normal file
55
internal/task_handlers.go
Normal file
@@ -0,0 +1,55 @@
|
||||
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)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user