-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
150 lines (117 loc) · 4.57 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
package main
// hlswatch - keep track of hls viewer stats
// Copyright (C) 2017 Maximilian Pachl
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// --------------------------------------------------------------------------------------
// imports
// --------------------------------------------------------------------------------------
import (
"log"
"runtime"
"os"
"os/signal"
"net/http"
"syscall"
"context"
"time"
"github.com/faryon93/hlswatch/handler"
"github.com/faryon93/hlswatch/state"
"github.com/faryon93/hlswatch/config"
)
// --------------------------------------------------------------------------------------
// constants
// --------------------------------------------------------------------------------------
const (
SHUTDOWN_GRACEPERIOD = 5 * time.Second
)
// --------------------------------------------------------------------------------------
// global variables
// --------------------------------------------------------------------------------------
var (
// configuration options
configFile = "/etc/hlswatch.conf"
// runtime variables
Ctx *state.State = state.New()
)
// --------------------------------------------------------------------------------------
// application entry
// --------------------------------------------------------------------------------------
func main() {
log.Println(GetAppIdentifier())
// setup go environment to use all available cpu cores
runtime.GOMAXPROCS(runtime.NumCPU())
log.Println("using up to", runtime.NumCPU(), "cpu cores")
// parse command line arguments
if len(os.Args) > 1 {
configFile = os.Args[1]
}
// load and parse the configuration file
conf, err := config.Load(configFile)
if err != nil {
log.Println("failed to load configuration file:", err.Error())
os.Exit(-1)
}
Ctx.Conf = conf
// setup the http static file server serving the playlists
// TODO: gzip compression for playlist, caching in ram
rootfs := http.Dir(conf.Common.HlsPath)
mux := http.NewServeMux()
mux.Handle("/", handler.Hls(Ctx, http.FileServer(rootfs)))
route(mux, "/stats", handler.Stats, Ctx)
srv := &http.Server{Addr: conf.Common.Listen, Handler: mux}
// serve the content via http
go func() {
var err error = nil
// setup a tls server if configured
if conf.IsSslEnabled() {
err = srv.ListenAndServeTLS(conf.Common.SslCertificate,
conf.Common.SslPrivateKey)
// plain old http server
} else {
err = srv.ListenAndServe()
}
if err != nil {
log.Println("failed to start http server:", err.Error())
Ctx.Shutdown() // gracefull shutdown the application
}
}()
log.Println("http is listening on", conf.Common.Listen)
// fire the statistics computation task
// TODO: close connections, ...
go InfluxMetrics(Ctx)
go StreamWatcher(Ctx)
// wait for a signal to shutdown the application
wait(os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
log.Println("gracefully shutting down application...")
// gracefully shutdown the http server
ctx, _ := context.WithTimeout(context.Background(), SHUTDOWN_GRACEPERIOD)
srv.Shutdown(ctx)
log.Println("application successfully exited")
}
// --------------------------------------------------------------------------------------
// helper functions
// --------------------------------------------------------------------------------------
func wait(sig ...os.Signal) {
signals := make(chan os.Signal)
signal.Notify(signals, sig...)
// wait for an OS signal or a signal by the close Channel
select {
case <- signals:
case <- Ctx.CloseChan:
}
}
func route(mux *http.ServeMux, pattern string, handler handler.Handler, ctx *state.State) {
f := http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
handler(ctx, w, r)
})
mux.Handle(pattern, f)
}