]> git.r.bdr.sh - rbdr/mobius/blame - cmd/mobius-hotline-server/main.go
Remove accidental commit
[rbdr/mobius] / cmd / mobius-hotline-server / main.go
CommitLineData
6988a057
JH
1package main
2
3import (
4 "context"
23411fc2 5 "encoding/json"
6988a057
JH
6 "flag"
7 "fmt"
22c599ab 8 "github.com/jhalter/mobius/hotline"
6988a057
JH
9 "go.uber.org/zap"
10 "go.uber.org/zap/zapcore"
23411fc2
JH
11 "io"
12 "log"
93df4153 13 "math/rand"
23411fc2 14 "net/http"
6988a057 15 "os"
18a8614d 16 "runtime"
93df4153 17 "time"
6988a057
JH
18)
19
20const (
18a8614d 21 defaultPort = 5500
6988a057
JH
22)
23
24func main() {
93df4153
JH
25 rand.Seed(time.Now().UnixNano())
26
6988a057
JH
27 ctx, cancelRoot := context.WithCancel(context.Background())
28
29 basePort := flag.Int("bind", defaultPort, "Bind address and port")
23411fc2 30 statsPort := flag.String("stats-port", "", "Enable stats HTTP endpoint on address and port")
18a8614d 31 configDir := flag.String("config", defaultConfigPath(), "Path to config root")
6988a057
JH
32 version := flag.Bool("version", false, "print version and exit")
33 logLevel := flag.String("log-level", "info", "Log level")
34 flag.Parse()
35
36 if *version {
37 fmt.Printf("v%s\n", hotline.VERSION)
38 os.Exit(0)
39 }
40
41 zapLvl, ok := zapLogLevel[*logLevel]
42 if !ok {
43 fmt.Printf("Invalid log level %s. Must be debug, info, warn, or error.\n", *logLevel)
44 os.Exit(0)
45 }
46
47 cores := []zapcore.Core{newStdoutCore(zapLvl)}
48 l := zap.New(zapcore.NewTee(cores...))
49 defer func() { _ = l.Sync() }()
50 logger := l.Sugar()
51
52 if _, err := os.Stat(*configDir); os.IsNotExist(err) {
53 logger.Fatalw("Configuration directory not found", "path", configDir)
54 }
55
b196a50a 56 srv, err := hotline.NewServer(*configDir, "", *basePort, logger, &hotline.OSFileStore{})
6988a057
JH
57 if err != nil {
58 logger.Fatal(err)
59 }
60
23411fc2
JH
61 sh := statHandler{hlServer: srv}
62 if *statsPort != "" {
63 http.HandleFunc("/", sh.RenderStats)
64
65 go func(srv *hotline.Server) {
66 // Use the default DefaultServeMux.
67 err = http.ListenAndServe(":"+*statsPort, nil)
68 if err != nil {
69 log.Fatal(err)
70 }
71 }(srv)
72 }
73
6988a057
JH
74 // Serve Hotline requests until program exit
75 logger.Fatal(srv.ListenAndServe(ctx, cancelRoot))
76}
77
23411fc2
JH
78type statHandler struct {
79 hlServer *hotline.Server
80}
81
82func (sh *statHandler) RenderStats(w http.ResponseWriter, _ *http.Request) {
83 u, err := json.Marshal(sh.hlServer.Stats)
84 if err != nil {
85 panic(err)
86 }
87
88 _, _ = io.WriteString(w, string(u))
89}
90
6988a057
JH
91func newStdoutCore(level zapcore.Level) zapcore.Core {
92 encoderCfg := zap.NewProductionEncoderConfig()
93 encoderCfg.TimeKey = "timestamp"
94 encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
95
96 return zapcore.NewCore(
97 zapcore.NewConsoleEncoder(encoderCfg),
98 zapcore.Lock(os.Stdout),
99 level,
100 )
101}
102
103var zapLogLevel = map[string]zapcore.Level{
104 "debug": zap.DebugLevel,
105 "info": zap.InfoLevel,
106 "warn": zap.WarnLevel,
107 "error": zap.ErrorLevel,
108}
18a8614d
JH
109
110func defaultConfigPath() (cfgPath string) {
111 switch runtime.GOOS {
112 case "windows":
113 cfgPath = "config"
114 case "darwin":
115 if _, err := os.Stat("/usr/local/var/mobius/config/"); err == nil {
116 cfgPath = "/usr/local/var/mobius/config/"
117 } else if _, err := os.Stat("/opt/homebrew/var/mobius/config"); err == nil {
118 cfgPath = "/opt/homebrew/var/mobius/config/"
119 }
120 case "linux":
121 cfgPath = "/usr/local/var/mobius/config/"
122 default:
123 fmt.Printf("unsupported OS")
124 }
125
126 return cfgPath
127}