-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_task.go
61 lines (50 loc) · 1.13 KB
/
server_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package server
import (
"bytes"
"errors"
"io"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/titpetric/task-ui/server/config"
. "github.com/titpetric/task-ui/server/model"
. "github.com/titpetric/task-ui/server/repository"
)
func (svc *Server) Task(w http.ResponseWriter, r *http.Request) {
serverError := func(err error) {
render.JSON(w, r, InternalServerError(err))
}
notFoundError := func(err error) {
render.JSON(w, r, NotFoundError(err))
}
spec, err := config.Load(".", "Taskfile.yml")
if err != nil {
serverError(err)
return
}
id := chi.URLParam(r, "id")
task, err := FindTask(spec, id)
if err != nil {
notFoundError(err)
return
}
taskinfo := NewTaskInfo(task)
taskinfo.History = LoadHistory(id)
var (
out = new(bytes.Buffer)
data = &templateData{
Tasks: ListTasks(spec, FilterOutInternal, FilterOutNoDesc),
Current: taskinfo,
}
)
if err := svc.template(out, taskTemplate, data); err != nil {
if errors.Is(err, ErrNotFound) {
notFoundError(err)
return
}
serverError(err)
return
}
w.Header().Set("Content-Type", "text/html")
io.Copy(w, out)
}