/
main.go
95 lines (82 loc) · 2.22 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
package main
import (
"context"
"flag"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/ahamlinman/hypcast/client"
"github.com/ahamlinman/hypcast/internal/api"
"github.com/ahamlinman/hypcast/internal/assets"
"github.com/ahamlinman/hypcast/internal/atsc"
"github.com/ahamlinman/hypcast/internal/atsc/tuner"
)
var (
flagAddr string
flagChannels string
flagAssets string
flagVideoPipeline string
)
func init() {
flag.StringVar(
&flagAddr, "addr", ":9200",
"Address for the HTTP server to listen on",
)
flag.StringVar(
&flagChannels, "channels", "/etc/hypcast/channels.conf",
"Path to the channels.conf file containing the list of available channels",
)
flag.StringVar(
&flagAssets, "assets", "",
"Path to client assets; overrides any embedded assets",
)
flag.StringVar(
&flagVideoPipeline, "video-pipeline", "default",
`Video pipeline implementation (default, lowpower, vaapi)`,
)
}
func main() {
flag.Parse()
log.Printf("Using channels from %s", flagChannels)
channels, err := readChannelsConf(flagChannels)
if err != nil {
log.Fatalf("Unable to read channels.conf: %v", err)
}
vp := tuner.ParseVideoPipeline(flagVideoPipeline)
log.Printf("Using %s video pipeline", vp)
tuner := tuner.NewTuner(channels, vp)
http.Handle("/api/", api.NewHandler(tuner))
if flagAssets != "" {
log.Printf("Using client assets from %s", flagAssets)
http.Handle("/", http.FileServer(
assets.FileSystem{FileSystem: http.Dir(flagAssets)},
))
} else if client.Build != nil {
log.Print("Using embedded client assets")
http.Handle("/", http.FileServer(
assets.FileSystem{FileSystem: http.FS(client.Build)},
))
}
server := http.Server{Addr: flagAddr}
go server.ListenAndServe()
log.Printf("Started Hypcast server on %s", flagAddr)
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
<-signalCh
log.Print("Shutting down")
stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(stopCtx)
}
func readChannelsConf(path string) ([]atsc.Channel, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return atsc.ParseChannelsConf(f)
}