tasks.go
45 lines1
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
package controllers
import (
"net/http"
"congo.gg/pkg/application"
"congo.gg/dev/models"
)
func Tasks() (string, *TasksController) {
return "tasks", &TasksController{}
}
type TasksController struct {
application.BaseController
}
func (c *TasksController) Setup(app *application.App) {
c.BaseController.Setup(app)
http.Handle("GET /tasks/active", app.Method(c, "ActiveTasks", RequireAuth()))
http.Handle("GET /tasks/{id}/logs", app.Method(c, "TaskLogs", RequireAuth()))
}
func (c TasksController) Handle(r *http.Request) application.Controller {
c.Request = r
return &c
}
// ActiveTasks returns running + recently completed tasks as HTMX partial.
func (c *TasksController) ActiveTasks(w http.ResponseWriter, r *http.Request) {
tasks, _ := models.Tasks.Search("WHERE Status IN ('running', 'completed', 'failed') ORDER BY CreatedAt DESC LIMIT 10")
c.Render(w, r, "task-card.html", tasks)
}
// TaskLogs returns the output of a specific task.
func (c *TasksController) TaskLogs(w http.ResponseWriter, r *http.Request) {
task, err := models.Tasks.Get(r.PathValue("id"))
if err != nil {
c.RenderError(w, r, err)
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(task.Output))
}