X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c789e2f35ea97fe2b390cf06dc1c914b913b7535..2d0f2abe62a5cc7272f7aeabc761bc7cf2147592:/cmd/mobius-hotline-server/main.go diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go index 8407449..7d3bf27 100644 --- a/cmd/mobius-hotline-server/main.go +++ b/cmd/mobius-hotline-server/main.go @@ -9,14 +9,13 @@ import ( "github.com/jhalter/mobius/hotline" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" "io" "log" - "math/rand" "net/http" "os" "path/filepath" "runtime" - "time" ) //go:embed mobius/config @@ -27,8 +26,6 @@ const ( ) func main() { - rand.Seed(time.Now().UnixNano()) - ctx, cancel := context.WithCancel(context.Background()) // TODO: implement graceful shutdown by closing context @@ -46,11 +43,14 @@ func main() { // } // }() - basePort := flag.Int("bind", defaultPort, "Bind address and port") + netInterface := flag.String("interface", "", "IP addr of interface to listen on. Defaults to all interfaces.") + basePort := flag.Int("bind", defaultPort, "Base Hotline server port. File transfer port is base port + 1.") statsPort := flag.String("stats-port", "", "Enable stats HTTP endpoint on address and port") configDir := flag.String("config", defaultConfigPath(), "Path to config root") version := flag.Bool("version", false, "print version and exit") logLevel := flag.String("log-level", "info", "Log level") + logFile := flag.String("log-file", "", "Path to log file") + init := flag.Bool("init", false, "Populate the config dir with default configuration") flag.Parse() @@ -66,7 +66,14 @@ func main() { os.Exit(0) } - cores := []zapcore.Core{newStdoutCore(zapLvl)} + cores := []zapcore.Core{ + newStdoutCore(zapLvl), + } + + if *logFile != "" { + cores = append(cores, newLogFileCore(*logFile, zapLvl)) + } + l := zap.New(zapcore.NewTee(cores...)) defer func() { _ = l.Sync() }() logger := l.Sugar() @@ -81,7 +88,6 @@ func main() { logger.Fatal(err) } logger.Infow("Config dir initialized at " + *configDir) - } else { logger.Infow("Existing config dir found. Skipping initialization.") } @@ -91,7 +97,7 @@ func main() { logger.Fatalw("Configuration directory not found. Correct the path or re-run with -init to generate initial config.", "path", configDir) } - srv, err := hotline.NewServer(*configDir, *basePort, logger, &hotline.OSFileStore{}) + srv, err := hotline.NewServer(*configDir, *netInterface, *basePort, logger, &hotline.OSFileStore{}) if err != nil { logger.Fatal(err) } @@ -138,6 +144,24 @@ func newStdoutCore(level zapcore.Level) zapcore.Core { ) } +func newLogFileCore(path string, level zapcore.Level) zapcore.Core { + encoderCfg := zap.NewProductionEncoderConfig() + encoderCfg.TimeKey = "timestamp" + encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder + writer := zapcore.AddSync(&lumberjack.Logger{ + Filename: path, + MaxSize: 100, // MB + MaxBackups: 3, + MaxAge: 365, // days + }) + + return zapcore.NewCore( + zapcore.NewConsoleEncoder(encoderCfg), + writer, + level, + ) +} + var zapLogLevel = map[string]zapcore.Level{ "debug": zap.DebugLevel, "info": zap.InfoLevel, @@ -208,7 +232,6 @@ func copyDir(src, dst string) error { } f.Close() } - } return nil