-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
161 lines (139 loc) · 5.07 KB
/
server.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package handlers
import (
"context"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"time"
"github.com/lmika/broadtail/services/videomanager"
"github.com/mergestat/timediff"
"github.com/robfig/cron"
"github.com/gorilla/mux"
"github.com/lmika/broadtail/middleware/jobdispatcher"
"github.com/lmika/broadtail/middleware/render"
"github.com/lmika/broadtail/middleware/rssfetcher"
"github.com/lmika/broadtail/middleware/ujs"
"github.com/lmika/broadtail/providers/jobs"
"github.com/lmika/broadtail/providers/stormstore"
"github.com/lmika/broadtail/providers/youtubedl"
"github.com/lmika/broadtail/providers/ytdlsimulator"
"github.com/lmika/broadtail/services/feedsmanager"
"github.com/lmika/broadtail/services/jobsmanager"
"github.com/lmika/broadtail/services/ytdownload"
"github.com/pkg/errors"
)
type Config struct {
LibraryDir string
LibraryOwner string
JobDataFile string
VideoDataFile string
FeedsDataFile string
FeedItemsDataFile string
YTDownloadCommand []string
YTDownloadSimulator bool
TemplateFS fs.FS
AssetFS fs.FS
}
func Server(config Config) (handler http.Handler, closeFn func(), err error) {
dispatcher := jobs.New()
jobStore, err := stormstore.NewJobStore(config.JobDataFile)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot open job store")
}
videoStore, err := stormstore.NewVideoStore(config.VideoDataFile)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot open video store")
}
feedsStore, err := stormstore.NewFeedStore(config.FeedsDataFile)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot open feeds store")
}
feedItemStore, err := stormstore.NewFeedItemStore(config.FeedItemsDataFile)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot open feeds store")
}
rssFetcher := rssfetcher.New()
var youtubedlProvider ytdownload.DownloadProvider
if config.YTDownloadSimulator {
log.Println("Using youtuble-dl simulator")
youtubedlProvider = ytdlsimulator.New()
} else {
youtubedlProvider, err = youtubedl.New(config.YTDownloadCommand)
if err != nil {
return nil, nil, errors.Wrap(err, "cannot instantiate youtube-dl provider")
}
}
ytdownloadService := ytdownload.New(ytdownload.Config{
LibraryDir: config.LibraryDir,
LibraryOwner: config.LibraryOwner,
}, youtubedlProvider, feedsStore, videoStore)
feedsManager := feedsmanager.New(feedsStore, feedItemStore, rssFetcher)
jobsManager := jobsmanager.New(dispatcher, jobStore)
videoManager := videomanager.New(config.LibraryDir, videoStore)
go jobsManager.Start()
// Schedule updates every 15 minutes
c := cron.New()
if err := c.AddFunc("@every 15m", func() {
if err := feedsManager.UpdateAllFeeds(context.Background()); err != nil {
log.Printf("error updating all feeds: %v", err)
}
}); err != nil {
return nil, nil, errors.Wrap(err, "invalid feed update schedule")
}
c.Start()
indexHandlers := &indexHandlers{jobsManager: jobsManager, feedsManager: feedsManager}
ytdownloadHandlers := &youTubeDownloadHandlers{ytdownloadService: ytdownloadService, jobsManager: jobsManager}
detailsHandler := &detailsHandler{ytdownloadService: ytdownloadService, videoManager: videoManager}
videoHandler := &videoHandlers{videoManager: videoManager}
jobsHandlers := &jobsHandlers{jobsManager: jobsManager}
feedsHandlers := &feedsHandler{feedsManager: feedsManager}
feedItemsHandlers := &feedItemsHandler{feedsManager: feedsManager}
r := mux.NewRouter()
r.Handle("/", indexHandlers.Index()).Methods("GET")
r.Handle("/job/download/youtube", ytdownloadHandlers.CreateDownloadJob()).Methods("POST")
r.Handle("/quicklook", detailsHandler.QuickLook()).Methods("GET")
r.Handle("/details/video/{video_id}", detailsHandler.VideoDetails()).Methods("GET")
r.Handle("/videos", videoHandler.List()).Methods("GET")
r.Handle("/videos/{video_id}", videoHandler.Show()).Methods("GET")
r.Handle("/jobs", jobsHandlers.List()).Methods("GET")
r.Handle("/jobs/done", jobsHandlers.ClearDone()).Methods("DELETE")
r.Handle("/jobs/{job_id}", jobsHandlers.Show()).Methods("GET")
r.Handle("/jobs/{job_id}", jobsHandlers.Delete()).Methods("DELETE")
feedsHandlers.Routes(r)
feedItemsHandlers.Routes(r)
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.FS(config.AssetFS))))
handler = ujs.MethodRewriteHandler(r)
handler = jobdispatcher.New(dispatcher).Use(handler)
handler = render.New(
config.TemplateFS,
render.WithFuncs(template.FuncMap{
"formatTime": func(t time.Time) string {
if t.IsZero() {
return "never"
}
if dur := time.Since(t); dur < time.Duration(5*24)*time.Hour {
return timediff.TimeDiff(t)
}
return t.Format("2006-01-02 15:04:05 MST")
},
"formatDurationSec": func(durationInSecs int) string {
hrs := durationInSecs / 3600
mins := (durationInSecs / 60) % 60
secs := durationInSecs % 60
if hrs > 0 {
return fmt.Sprintf("%d:%02d:%02d", hrs, mins, secs)
}
return fmt.Sprintf("%d:%02d", mins, secs)
},
}),
).Use(handler)
closeFn = func() {
jobStore.Close()
videoStore.Close()
feedItemStore.Close()
feedsStore.Close()
}
return handler, closeFn, nil
}