1package controller
2
3import (
4 "net/http"
5 "time"
6
7 "git.sr.ht/~gabrielgio/midr/db"
8 "git.sr.ht/~gabrielgio/midr/worker"
9 "github.com/gin-gonic/gin"
10)
11
12type Env struct {
13 Entries db.EntryModel
14 Worker worker.Worker
15}
16
17func (e *Env) GetEntries(c *gin.Context) {
18 entries := e.Entries.All()
19 c.HTML(http.StatusOK, "index", entries)
20}
21
22func (e *Env) GetEntry(c *gin.Context) {
23 id := c.Param("id")
24 if id != "" {
25 entry := e.Entries.Find(id)
26 c.HTML(http.StatusOK, "entry", entry)
27 } else {
28 c.HTML(http.StatusOK, "entry", db.Entry{})
29 }
30}
31
32func (e *Env) UpdateEntry(c *gin.Context) {
33 var entry db.Entry
34 c.ShouldBind(&entry)
35 e.Entries.Update(entry)
36 c.Redirect(http.StatusFound, "/")
37}
38
39func (e *Env) CreateEntry(c *gin.Context) {
40 var entry db.Entry
41 c.ShouldBind(&entry)
42 e.Entries.Create(&entry)
43 e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder)
44 c.Redirect(http.StatusFound, "/")
45}
46
47func (e *Env) DeleteEntry(c *gin.Context) {
48 var entry db.Entry
49 id := c.Param("id")
50 e.Entries.Delete(id)
51 c.HTML(http.StatusOK, "entry", entry)
52}
53
54func (e *Env) GetJobs(c *gin.Context) {
55 jobs := e.Worker.GetJobs()
56 c.JSON(http.StatusOK, jobs)
57}
58
59func (e *Env) StartScheduler() {
60 e.Worker.StartReader()
61 go func() {
62 for true {
63 entries := e.Entries.All()
64
65 for _, entry := range entries {
66 e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder)
67 }
68 time.Sleep(30 * time.Minute)
69 }
70 }()
71}