]> git.r.bdr.sh - rbdr/mobius/commitdiff
Add flag to enable optional stats HTTP endpoint
authorJeff Halter <redacted>
Thu, 9 Jun 2022 03:54:07 +0000 (20:54 -0700)
committerJeff Halter <redacted>
Thu, 9 Jun 2022 03:54:07 +0000 (20:54 -0700)
re: #29

TODO: add more stat counters

Usage:
```
./mobius-hotline-server -stats-port 5503
```

```
❯ curl -s localhost:5503 | jq .
{
  "LoginCount": 0,
  "StartTime": "2022-06-08T20:49:10.183921-07:00",
  "DownloadCounter": 0,
  "UploadCounter": 0
}
```

cmd/mobius-hotline-server/main.go
cmd/mobius-hotline-server/mobius-hotline-server [new file with mode: 0755]
hotline/server.go
hotline/stats.go

index 60327a803e8b2cfd27140012b5995ed4530aa387..8a3135ebbabd8fbb1ff7f0c3b6c351e35c9c4453 100644 (file)
@@ -2,12 +2,16 @@ 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"
@@ -23,6 +27,7 @@ func main() {
        ctx, cancelRoot := context.WithCancel(context.Background())
 
        basePort := flag.Int("bind", defaultPort, "Bind address and port")
+       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")
@@ -53,10 +58,36 @@ func main() {
                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"
diff --git a/cmd/mobius-hotline-server/mobius-hotline-server b/cmd/mobius-hotline-server/mobius-hotline-server
new file mode 100755 (executable)
index 0000000..6d785f9
Binary files /dev/null and b/cmd/mobius-hotline-server/mobius-hotline-server differ
index 1a6d163fade17544f2065bdf7af72e59bed0ffde..76cc248ba57088ebf950a449c4af9551d49361d2 100644 (file)
@@ -693,6 +693,8 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
 
        switch fileTransfer.Type {
        case FileDownload:
+               s.Stats.DownloadCounter += 1
+
                fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
                if err != nil {
                        return err
@@ -744,6 +746,8 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                        }
                }
        case FileUpload:
+               s.Stats.UploadCounter += 1
+
                destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName)
 
                var file *os.File
@@ -829,6 +833,8 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
 
                i := 0
                err = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, err error) error {
+                       s.Stats.DownloadCounter += 1
+
                        if err != nil {
                                return err
                        }
@@ -1039,8 +1045,6 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                                        nextAction = dlFldrActionResumeFile
                                }
 
-                               fmt.Printf("Next Action: %v\n", nextAction)
-
                                if _, err := conn.Write([]byte{0, uint8(nextAction)}); err != nil {
                                        return err
                                }
index dd8ea1d14dbad7944ce6095783b4d5af54e1717a..4b9119d6a21aa60516fc9af8e3c2e323d4d7400b 100644 (file)
@@ -6,9 +6,10 @@ import (
 )
 
 type Stats struct {
-       LoginCount int           `yaml:"login count"`
-       StartTime  time.Time     `yaml:"start time"`
-       Uptime     time.Duration `yaml:"uptime"`
+       LoginCount      int       `yaml:"login count"`
+       StartTime       time.Time `yaml:"start time"`
+       DownloadCounter int
+       UploadCounter   int
 }
 
 func (s *Stats) String() string {