]>
Commit | Line | Data |
---|---|---|
1 | package main | |
2 | ||
3 | import ( | |
4 | "context" | |
5 | "flag" | |
6 | "fmt" | |
7 | "github.com/jhalter/mobius/hotline" | |
8 | "go.uber.org/zap" | |
9 | "go.uber.org/zap/zapcore" | |
10 | "math/rand" | |
11 | "os" | |
12 | "runtime" | |
13 | "time" | |
14 | ) | |
15 | ||
16 | const ( | |
17 | defaultPort = 5500 | |
18 | ) | |
19 | ||
20 | func main() { | |
21 | rand.Seed(time.Now().UnixNano()) | |
22 | ||
23 | ctx, cancelRoot := context.WithCancel(context.Background()) | |
24 | ||
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") | |
29 | flag.Parse() | |
30 | ||
31 | if *version { | |
32 | fmt.Printf("v%s\n", hotline.VERSION) | |
33 | os.Exit(0) | |
34 | } | |
35 | ||
36 | zapLvl, ok := zapLogLevel[*logLevel] | |
37 | if !ok { | |
38 | fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel) | |
39 | os.Exit(0) | |
40 | } | |
41 | ||
42 | cores := []zapcore.Core{newStdoutCore(zapLvl)} | |
43 | l := zap.New(zapcore.NewTee(cores...)) | |
44 | defer func() { _ = l.Sync() }() | |
45 | logger := l.Sugar() | |
46 | ||
47 | if _, err := os.Stat(*configDir); os.IsNotExist(err) { | |
48 | logger.Fatalw("Configuration directory not found", "path", configDir) | |
49 | } | |
50 | ||
51 | srv, err := hotline.NewServer(*configDir, "", *basePort, logger, &hotline.OSFileStore{}) | |
52 | if err != nil { | |
53 | logger.Fatal(err) | |
54 | } | |
55 | ||
56 | // Serve Hotline requests until program exit | |
57 | logger.Fatal(srv.ListenAndServe(ctx, cancelRoot)) | |
58 | } | |
59 | ||
60 | func newStdoutCore(level zapcore.Level) zapcore.Core { | |
61 | encoderCfg := zap.NewProductionEncoderConfig() | |
62 | encoderCfg.TimeKey = "timestamp" | |
63 | encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder | |
64 | ||
65 | return zapcore.NewCore( | |
66 | zapcore.NewConsoleEncoder(encoderCfg), | |
67 | zapcore.Lock(os.Stdout), | |
68 | level, | |
69 | ) | |
70 | } | |
71 | ||
72 | var zapLogLevel = map[string]zapcore.Level{ | |
73 | "debug": zap.DebugLevel, | |
74 | "info": zap.InfoLevel, | |
75 | "warn": zap.WarnLevel, | |
76 | "error": zap.ErrorLevel, | |
77 | } | |
78 | ||
79 | func defaultConfigPath() (cfgPath string) { | |
80 | switch runtime.GOOS { | |
81 | case "windows": | |
82 | cfgPath = "config" | |
83 | case "darwin": | |
84 | if _, err := os.Stat("/usr/local/var/mobius/config/"); err == nil { | |
85 | cfgPath = "/usr/local/var/mobius/config/" | |
86 | } else if _, err := os.Stat("/opt/homebrew/var/mobius/config"); err == nil { | |
87 | cfgPath = "/opt/homebrew/var/mobius/config/" | |
88 | } | |
89 | case "linux": | |
90 | cfgPath = "/usr/local/var/mobius/config/" | |
91 | default: | |
92 | fmt.Printf("unsupported OS") | |
93 | } | |
94 | ||
95 | return cfgPath | |
96 | } |