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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package controller
import (
"log"
"net/http"
"strconv"
"strings"
"time"
"git.sr.ht/~gabrielgio/midr/db"
"git.sr.ht/~gabrielgio/midr/worker"
"github.com/gin-gonic/gin"
)
type Env struct {
Entries db.EntryModel
Worker worker.Worker
}
func logBytes(logc <-chan []byte) {
for l := range logc {
logs := strings.TrimRight(string(l), "\t \n")
log.Println(logs)
}
}
func (e *Env) GetEntries(c *gin.Context) {
entries := e.Entries.All()
c.HTML(http.StatusOK, "index", entries)
}
func (e *Env) GetEntry(c *gin.Context) {
id := c.Param("id")
if id != "" {
entry := e.Entries.Find(id)
c.HTML(http.StatusOK, "entry", entry)
} else {
c.HTML(http.StatusOK, "entry", db.Entry{})
}
}
func (e *Env) UpdateEntry(c *gin.Context) {
var entry db.Entry
c.ShouldBind(&entry)
e.Entries.Update(entry)
c.Redirect(http.StatusFound, "/")
}
func (e *Env) CreateEntry(c *gin.Context) {
var entry db.Entry
c.ShouldBind(&entry)
e.Entries.Create(&entry)
log := e.Worker.RunYtDlpWorker(&entry)
go logBytes(log)
c.Redirect(http.StatusFound, "/")
}
func (e *Env) DeleteEntry(c *gin.Context) {
var entry db.Entry
id := c.Param("id")
e.Entries.Delete(id)
u64, _ := strconv.ParseUint(id, 10, 32)
e.Worker.RemoveJob(uint(u64))
c.HTML(http.StatusOK, "entry", entry)
}
func (e *Env) GetJobs(c *gin.Context) {
jobs := e.Worker.GetJobs()
c.JSON(http.StatusOK, jobs)
}
func (e *Env) StartScheduler() {
e.Worker.StartReader()
go func() {
for {
entries := e.Entries.All()
for _, entry := range entries {
log := e.Worker.RunYtDlpWorker(&entry)
if log != nil {
go logBytes(log)
}
}
time.Sleep(30 * time.Second)
}
}()
}
|