2021-08-21 14:15:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-08-23 00:45:44 +00:00
|
|
|
"net/http"
|
2021-08-21 14:15:29 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"github.uio.no/torjus/dogtamer"
|
2021-09-02 00:49:11 +00:00
|
|
|
"github.uio.no/torjus/dogtamer/config"
|
2021-08-21 14:15:29 +00:00
|
|
|
"github.uio.no/torjus/dogtamer/server"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExitSuccess = 0
|
|
|
|
ExitGenericError = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cli.VersionPrinter = func(c *cli.Context) {
|
|
|
|
fmt.Println(dogtamer.GetVersion())
|
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "dogtamer",
|
|
|
|
Version: dogtamer.Version,
|
|
|
|
Authors: []*cli.Author{
|
|
|
|
{
|
|
|
|
Name: "Torjus Håkestad",
|
|
|
|
Email: "torjus@usit.uio.no",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Path to config-file",
|
|
|
|
Aliases: []string{"c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "serve",
|
|
|
|
Usage: "Start dogtamer server",
|
|
|
|
Action: ActionServe,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error: %s\n", err)
|
|
|
|
os.Exit(ExitGenericError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ActionServe(c *cli.Context) error {
|
2021-09-02 00:49:11 +00:00
|
|
|
logger := setupServerLogger()
|
|
|
|
cfg, err := config.FromDefaultLocations()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("No config file found.")
|
|
|
|
}
|
2021-08-21 14:15:29 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2021-09-02 00:49:11 +00:00
|
|
|
// Setup RTMP-server
|
|
|
|
s := server.NewRTMPServer(ctx, cfg.RTMPListenAddr)
|
2021-08-21 14:15:29 +00:00
|
|
|
s.Logger = logger
|
|
|
|
|
2021-09-02 00:49:11 +00:00
|
|
|
// Setup web-server
|
|
|
|
webDone := make(chan struct{})
|
|
|
|
if cfg.HTTPServerEnable {
|
|
|
|
ws := server.NewWebServer(ctx, s)
|
|
|
|
ws.ListenAddr = cfg.HTTPListenAddr
|
|
|
|
ws.Logger = logger
|
|
|
|
go func() {
|
2021-09-02 00:53:25 +00:00
|
|
|
s.Logger.Infow("Starting HTTP server.", "listen_addr", ws.ListenAddr)
|
2021-09-02 00:49:11 +00:00
|
|
|
err := ws.Serve()
|
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2021-09-02 00:53:25 +00:00
|
|
|
s.Logger.Infow("HTTP server shut down with error.", "err", err)
|
2021-09-02 00:49:11 +00:00
|
|
|
}
|
|
|
|
close(webDone)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
close(webDone)
|
|
|
|
}
|
2021-08-21 14:15:29 +00:00
|
|
|
// Listen for SIGINT
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigChan, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-sigChan
|
2021-09-02 00:49:11 +00:00
|
|
|
s.Logger.Debug("Got shutdown signal.")
|
2021-08-21 14:15:29 +00:00
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
2021-09-02 00:49:11 +00:00
|
|
|
// Start RTMP-server
|
2021-09-02 00:53:25 +00:00
|
|
|
s.Logger.Infow("Starting RTMP server.", "listen_addr", s.ListenAddr)
|
2021-08-21 14:15:29 +00:00
|
|
|
if err := s.Listen(); err != nil {
|
|
|
|
return cli.Exit(err, ExitGenericError)
|
|
|
|
}
|
2021-09-02 00:49:11 +00:00
|
|
|
|
|
|
|
// Wait for webserver to exit, if started
|
|
|
|
<-webDone
|
2021-08-21 14:15:29 +00:00
|
|
|
s.Logger.Info("Server shut down.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupServerLogger() *zap.SugaredLogger {
|
|
|
|
logEncoderConfig := zap.NewProductionEncoderConfig()
|
|
|
|
logEncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
|
|
|
|
logEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
|
|
|
logEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
logEncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
|
|
|
|
|
|
|
|
rootLoggerConfig := &zap.Config{
|
2021-09-02 00:49:11 +00:00
|
|
|
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
|
2021-08-21 14:15:29 +00:00
|
|
|
OutputPaths: []string{"stdout"},
|
|
|
|
ErrorOutputPaths: []string{"stdout"},
|
|
|
|
Encoding: "console",
|
|
|
|
EncoderConfig: logEncoderConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
rootLogger, err := rootLoggerConfig.Build()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootLogger.Named("RTMP").WithOptions().Sugar()
|
|
|
|
}
|