-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
112 lines (92 loc) · 2.8 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"time"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
const (
dataSubdirEpisodes = "ep"
dataSubdirMetadata = "meta"
hitLoggingPeriod = 24 * time.Hour
websrvClientReadTimout = 15 * time.Second
ytAPIRespiteUnit = 5 * time.Minute
)
var (
flagUseSyslog = flag.Bool("syslog", false,
"send log statements to syslog rather than writing them to stderr")
flagConfigPath = flag.String("config", "config.json",
"path to config file")
flagDataPath = flag.String("data", "data",
"path to directory to change into and write data (created if needed)")
flagDataClean = flag.Bool("dataclean", false,
"during initialisation, remove files in the data directory that are irrelevant given the current config")
flagPrintVersion = flag.Bool("version", false,
"print version information then exit")
)
func main() {
cfg, err := setup()
if err != nil {
log.Fatal(err)
}
err = run(cfg)
if err != nil {
log.Fatal(err)
}
}
func run(cfg *config) error {
apiKey := cfg.YTDataAPIKey
log.Printf("Using YouTube Data API key ending %s", apiKey[len(apiKey)-5:])
var cleanc chan *cleaningWhitelist
if *flagDataClean {
cleanc = make(chan *cleaningWhitelist)
}
for i := range cfg.Podcasts {
ytAPI, err := youtube.NewService(context.Background(), option.WithAPIKey(apiKey))
if err != nil {
return err
}
wat, err := newWatcher(
ytAPI, cfg, &cfg.Podcasts[i], cleanc)
if err != nil {
log.Fatal(err)
}
go wat.watch()
}
if *flagDataClean {
n, err := clean(len(cfg.Podcasts), cleanc)
if err != nil {
return err
}
log.Printf("Clean removed %d files", n)
}
// Run a webserver to serve the episode and metadata files.
mux := http.NewServeMux()
files := newHitLoggingFsys(http.Dir("."), hitLoggingPeriod, cfg.ServeDirectoryListings)
mux.Handle("/", http.FileServer(files))
mux.HandleFunc(httpHealthPrefix, healthHandler)
websrv := http.Server{
Addr: fmt.Sprint(cfg.ServeHost, ":", cfg.ServePort),
Handler: mux,
// Conserve # open FDs by pruning persistent (keep-alive) HTTP conns.
ReadTimeout: websrvClientReadTimout,
}
err := websrv.ListenAndServe()
// @todo #0 When listening on cfg.ServeHost fails and an alternative address is listened
// on, cfg.ServeHost should not be used in watcher#buildURL.
// How about instead of automatically falling back to trying to listen on all
// interfaces, add a serve_host_fallback:"localhost" to config? Then if
// neither serve_host or serve_host_fallback work, it's a fatal error.
if err != nil {
samePortAllInterfaces := fmt.Sprint(":", cfg.ServePort)
log.Printf("Web server could not listen on %v, trying %v instead",
websrv.Addr, samePortAllInterfaces)
websrv.Addr = samePortAllInterfaces
err = websrv.ListenAndServe()
}
return err
}