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"
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")
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"
switch fileTransfer.Type {
case FileDownload:
+ s.Stats.DownloadCounter += 1
+
fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
if err != nil {
return err
}
}
case FileUpload:
+ s.Stats.UploadCounter += 1
+
destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName)
var file *os.File
i := 0
err = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, err error) error {
+ s.Stats.DownloadCounter += 1
+
if err != nil {
return err
}
nextAction = dlFldrActionResumeFile
}
- fmt.Printf("Next Action: %v\n", nextAction)
-
if _, err := conn.Write([]byte{0, uint8(nextAction)}); err != nil {
return err
}
)
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 {