-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (136 loc) · 4.13 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
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
162
163
164
165
166
167
168
169
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
// avoid having to bundle this in the docker image
_ "time/tzdata"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
l := logrus.New()
var (
listen = flag.String("listen", "localhost:8080", "Address to listen on")
metricsListen = flag.String("metrics-listen", "localhost:8090", "Address to serve metrics on (prom at /metrics), if set")
configPath = flag.String("config", "", "path to config file")
debug = flag.Bool("debug", false, "enable debug logging")
)
flag.Parse()
if *listen == "" {
l.Fatal("-listen must be provided")
}
if *configPath == "" {
l.Fatal("-config must be provided")
}
if *debug {
l.Level = logrus.DebugLevel
} else {
l.Level = logrus.InfoLevel
}
cfg, err := loadAndValdiateConfig(*configPath)
if err != nil {
l.Fatal(err)
}
// ensure db exists/make it, to avoid those out of memory errors
if err := os.MkdirAll(filepath.Dir(cfg.DBPath), 0755); err != nil {
l.Fatalf("ensuring %s exists: %v", filepath.Dir(cfg.DBPath), err)
}
db, err := newDB(cfg.DBPath)
if err != nil {
l.WithError(err).Fatalf("opening database at %s", cfg.DBPath)
}
defer db.Close()
rec := newRecorder(db)
ds := newDiskChunkStore(rec, cfg.ChunkDir, "/segment")
ss := newSessionStore(db)
pl := newPlaylist(l.WithField("component", "playlist"), cfg.Streams, rec, ds, ss)
is := newIcyServer(l.WithField("component", "icyServer"), cfg.Streams, rec, ds)
idx := newIndex(l.WithField("component", "index"), cfg.Streams)
gc := newGarbageCollector(l.WithField("component", "gc"), db, ds)
mux := http.NewServeMux()
mux.HandleFunc("/m3u8", pl.ServePlaylist)
mux.HandleFunc("/icecast", is.ServeIcecast)
mux.Handle("/segment/", http.StripPrefix("/segment/", ds))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
idx.ServeHTTP(w, r)
})
srv := &http.Server{
Addr: *listen,
Handler: mux,
}
var g run.Group
g.Add(run.SignalHandler(ctx, os.Interrupt))
g.Add(gc.Run, gc.Interrupt)
for _, s := range cfg.Streams {
fcs, err := ds.FetcherStore(s.ID)
if err != nil {
l.WithError(err).Fatalf("creating fetcher store for %s", s.ID)
}
f, err := newFetcher(l.WithField("component", "fetcher").WithField("stationid", s.ID), fcs, s.ID, s.URL)
if err != nil {
l.WithError(err).Fatal("creating fetcher")
}
g.Add(f.Run, f.Interrupt)
if s.NowPlayingURL != "" {
npf, err := newNowPlayingFetcher(l.WithField("component", "nowplaying").WithField("stationid", s.ID), db, s.ID, s.NowPlayingURL)
if err != nil {
l.WithError(err).Fatal("creating now playing fetcher")
}
g.Add(npf.Run, npf.Interrupt)
}
}
if *metricsListen != "" {
ph := promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
ErrorLog: l,
}),
)
pm := http.NewServeMux()
pm.Handle("/metrics", ph)
metricsSrv := &http.Server{
Addr: *metricsListen,
Handler: pm,
}
prometheus.MustRegister(newMetricsCollector(db))
g.Add(func() error {
l.Printf("Listing for metrics on %s", *metricsListen)
if err := metricsSrv.ListenAndServe(); err != nil {
return fmt.Errorf("serving metrics: %v", err)
}
return nil
}, func(error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := metricsSrv.Shutdown(ctx); err != nil {
log.Printf("shutting down metrics server: %v", err)
}
log.Print("returning metrics shutdown")
})
}
g.Add(func() error {
l.Infof("listening on %s", *listen)
return srv.ListenAndServe()
}, func(error) {
// use a new context, as upstream will be canceled by now
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
})
if err := g.Run(); err != nil {
l.Fatal(err)
}
}