]> git.r.bdr.sh - rbdr/mobius/blobdiff - cmd/mobius-hotline-server/main.go
Add initial HTTP API endpoints
[rbdr/mobius] / cmd / mobius-hotline-server / main.go
index 04b3c57a3597920b5a15a06df7b4d2c4748d6908..33ba7c2125155d511b507d1a34aa86674310db79 100644 (file)
@@ -3,7 +3,6 @@ package main
 import (
        "context"
        "embed"
-       "encoding/json"
        "flag"
        "fmt"
        "github.com/jhalter/mobius/hotline"
@@ -12,7 +11,6 @@ import (
        "io"
        "log"
        "log/slog"
-       "net/http"
        "os"
        "os/signal"
        "path"
@@ -44,7 +42,7 @@ func main() {
 
        netInterface := flag.String("interface", "", "IP addr of interface to listen on.  Defaults to all interfaces.")
        basePort := flag.Int("bind", 5500, "Base Hotline server port.  File transfer port is base port + 1.")
-       statsPort := flag.String("stats-port", "", "Enable stats HTTP endpoint on address and port")
+       apiAddr := flag.String("api-addr", "", "Enable HTTP API endpoint on address and port")
        configDir := flag.String("config", configSearchPaths(), "Path to config root")
        printVersion := flag.Bool("version", false, "Print version and exit")
        logLevel := flag.String("log-level", "info", "Log level")
@@ -161,26 +159,9 @@ func main() {
                }
        }
 
-       reloadHandler := func(reloadFunc func()) func(w http.ResponseWriter, _ *http.Request) {
-               return func(w http.ResponseWriter, _ *http.Request) {
-                       reloadFunc()
-
-                       _, _ = io.WriteString(w, `{ "msg": "config reloaded" }`)
-               }
-       }
-
-       sh := APIHandler{hlServer: srv}
-       if *statsPort != "" {
-               http.HandleFunc("/", sh.RenderStats)
-               http.HandleFunc("/api/v1/stats", sh.RenderStats)
-               http.HandleFunc("/api/v1/reload", reloadHandler(reloadFunc))
-
-               go func(srv *hotline.Server) {
-                       err = http.ListenAndServe(":"+*statsPort, nil)
-                       if err != nil {
-                               log.Fatal(err)
-                       }
-               }(srv)
+       if *apiAddr != "" {
+               sh := mobius.NewAPIServer(srv, reloadFunc, slogger)
+               go sh.Serve(*apiAddr)
        }
 
        go func() {
@@ -214,19 +195,6 @@ func main() {
        log.Fatal(srv.ListenAndServe(ctx))
 }
 
-type APIHandler struct {
-       hlServer *hotline.Server
-}
-
-func (sh *APIHandler) RenderStats(w http.ResponseWriter, _ *http.Request) {
-       u, err := json.Marshal(sh.hlServer.CurrentStats())
-       if err != nil {
-               panic(err)
-       }
-
-       _, _ = io.WriteString(w, string(u))
-}
-
 func configSearchPaths() string {
        for _, cfgPath := range mobius.ConfigSearchOrder {
                if _, err := os.Stat(cfgPath); err == nil {