From: Jeff Halter Date: Thu, 9 Jun 2022 03:54:07 +0000 (-0700) Subject: Add flag to enable optional stats HTTP endpoint X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/23411fc23dcf82e0ed35a780bfdda2341bedf819 Add flag to enable optional stats HTTP endpoint 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 } ``` --- diff --git a/cmd/mobius-hotline-server/main.go b/cmd/mobius-hotline-server/main.go index 60327a8..8a3135e 100644 --- a/cmd/mobius-hotline-server/main.go +++ b/cmd/mobius-hotline-server/main.go @@ -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 index 0000000..6d785f9 Binary files /dev/null and b/cmd/mobius-hotline-server/mobius-hotline-server differ diff --git a/hotline/server.go b/hotline/server.go index 1a6d163..76cc248 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -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 } diff --git a/hotline/stats.go b/hotline/stats.go index dd8ea1d..4b9119d 100644 --- a/hotline/stats.go +++ b/hotline/stats.go @@ -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 {