]> git.r.bdr.sh - rbdr/mobius/blobdiff - cmd/mobius-hotline-server/main.go
Add flag to enable optional stats HTTP endpoint
[rbdr/mobius] / cmd / mobius-hotline-server / main.go
index aa1f015ee2aefcbcffd86b7f60e459bcf86390e5..8a3135ebbabd8fbb1ff7f0c3b6c351e35c9c4453 100644 (file)
@@ -2,24 +2,33 @@ package main
 
 import (
        "context"
+       "encoding/json"
        "flag"
        "fmt"
        "github.com/jhalter/mobius/hotline"
        "go.uber.org/zap"
        "go.uber.org/zap/zapcore"
+       "io"
+       "log"
+       "math/rand"
+       "net/http"
        "os"
+       "runtime"
+       "time"
 )
 
 const (
-       defaultConfigPath = "/usr/local/var/mobius/config/" // matches Homebrew default config location
-       defaultPort       = 5500
+       defaultPort = 5500
 )
 
 func main() {
+       rand.Seed(time.Now().UnixNano())
+
        ctx, cancelRoot := context.WithCancel(context.Background())
 
        basePort := flag.Int("bind", defaultPort, "Bind address and port")
-       configDir := flag.String("config", defaultConfigPath, "Path to config root")
+       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")
        flag.Parse()
@@ -44,17 +53,41 @@ func main() {
                logger.Fatalw("Configuration directory not found", "path", configDir)
        }
 
-       hotline.FS = &hotline.OSFileStore{}
-
-       srv, err := hotline.NewServer(*configDir, "", *basePort, logger)
+       srv, err := hotline.NewServer(*configDir, "", *basePort, logger, &hotline.OSFileStore{})
        if err != nil {
                logger.Fatal(err)
        }
 
+       sh := statHandler{hlServer: srv}
+       if *statsPort != "" {
+               http.HandleFunc("/", sh.RenderStats)
+
+               go func(srv *hotline.Server) {
+                       // Use the default DefaultServeMux.
+                       err = http.ListenAndServe(":"+*statsPort, nil)
+                       if err != nil {
+                               log.Fatal(err)
+                       }
+               }(srv)
+       }
+
        // Serve Hotline requests until program exit
        logger.Fatal(srv.ListenAndServe(ctx, cancelRoot))
 }
 
+type statHandler struct {
+       hlServer *hotline.Server
+}
+
+func (sh *statHandler) RenderStats(w http.ResponseWriter, _ *http.Request) {
+       u, err := json.Marshal(sh.hlServer.Stats)
+       if err != nil {
+               panic(err)
+       }
+
+       _, _ = io.WriteString(w, string(u))
+}
+
 func newStdoutCore(level zapcore.Level) zapcore.Core {
        encoderCfg := zap.NewProductionEncoderConfig()
        encoderCfg.TimeKey = "timestamp"
@@ -73,3 +106,22 @@ var zapLogLevel = map[string]zapcore.Level{
        "warn":  zap.WarnLevel,
        "error": zap.ErrorLevel,
 }
+
+func defaultConfigPath() (cfgPath string) {
+       switch runtime.GOOS {
+       case "windows":
+               cfgPath = "config"
+       case "darwin":
+               if _, err := os.Stat("/usr/local/var/mobius/config/"); err == nil {
+                       cfgPath = "/usr/local/var/mobius/config/"
+               } else if _, err := os.Stat("/opt/homebrew/var/mobius/config"); err == nil {
+                       cfgPath = "/opt/homebrew/var/mobius/config/"
+               }
+       case "linux":
+               cfgPath = "/usr/local/var/mobius/config/"
+       default:
+               fmt.Printf("unsupported OS")
+       }
+
+       return cfgPath
+}