7 "github.com/jhalter/mobius/hotline"
9 "go.uber.org/zap/zapcore"
16 defaultConfigPath = "/usr/local/var/mobius/config/" // matches Homebrew default config location
21 rand.Seed(time.Now().UnixNano())
23 ctx, cancelRoot := context.WithCancel(context.Background())
25 basePort := flag.Int("bind", defaultPort, "Bind address and port")
26 configDir := flag.String("config", defaultConfigPath, "Path to config root")
27 version := flag.Bool("version", false, "print version and exit")
28 logLevel := flag.String("log-level", "info", "Log level")
32 fmt.Printf("v%s\n", hotline.VERSION)
36 zapLvl, ok := zapLogLevel[*logLevel]
38 fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel)
42 cores := []zapcore.Core{newStdoutCore(zapLvl)}
43 l := zap.New(zapcore.NewTee(cores...))
44 defer func() { _ = l.Sync() }()
47 if _, err := os.Stat(*configDir); os.IsNotExist(err) {
48 logger.Fatalw("Configuration directory not found", "path", configDir)
51 hotline.FS = &hotline.OSFileStore{}
53 srv, err := hotline.NewServer(*configDir, "", *basePort, logger)
58 // Serve Hotline requests until program exit
59 logger.Fatal(srv.ListenAndServe(ctx, cancelRoot))
62 func newStdoutCore(level zapcore.Level) zapcore.Core {
63 encoderCfg := zap.NewProductionEncoderConfig()
64 encoderCfg.TimeKey = "timestamp"
65 encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
67 return zapcore.NewCore(
68 zapcore.NewConsoleEncoder(encoderCfg),
69 zapcore.Lock(os.Stdout),
74 var zapLogLevel = map[string]zapcore.Level{
75 "debug": zap.DebugLevel,
76 "info": zap.InfoLevel,
77 "warn": zap.WarnLevel,
78 "error": zap.ErrorLevel,