From 1cc12afa16f68bd49e81eee9c032093ca5c80d47 Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Tue, 27 May 2025 21:56:19 +0300 Subject: [PATCH] add configuration --- config/config.go | 23 +++++++++++++++++++++++ database/db.go | 4 ++-- ingest/ingest.go | 19 ++++++++++++++----- main.go | 19 ++++++++++--------- web/handlers.go | 10 ++-------- web/server.go | 28 ++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 24 deletions(-) create mode 100644 config/config.go create mode 100644 web/server.go diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..528efda --- /dev/null +++ b/config/config.go @@ -0,0 +1,23 @@ +package config + +import "flag" + +type Config struct { + Port int `json:"port"` + LogPath string `json:"logPath"` + DatabasePath string `json:"databasePath"` +} + +func ParseFlags() Config { + port := flag.Int("Port", 8080, "Listen port") + logPath := flag.String("LogPath", "logs", "Path to log files") + databasePath := flag.String("DatabasePath", "badgerdb", "Path to the database directory") + + flag.Parse() + + return Config{ + Port: *port, + LogPath: *logPath, + DatabasePath: *databasePath, + } +} diff --git a/database/db.go b/database/db.go index 30d83c8..923254e 100644 --- a/database/db.go +++ b/database/db.go @@ -15,8 +15,8 @@ const ( CollectionName = "shares" ) -func InitDatabase() (*clover.DB, error) { - store, err := badgerstore.Open("badgerdb") +func InitDatabase(path string) (*clover.DB, error) { + store, err := badgerstore.Open(path) if err != nil { return nil, fmt.Errorf("failed to open BadgerDB store: %v", err) } diff --git a/ingest/ingest.go b/ingest/ingest.go index 985caf2..a961c5d 100644 --- a/ingest/ingest.go +++ b/ingest/ingest.go @@ -18,17 +18,26 @@ import ( const logsDir = "/home/pk/pro/pkstats/logs" -func WatchAndIngest(db *clover.DB) { +type Ingestor struct { + db *clover.DB + logPath string +} + +func NewIngestor(db *clover.DB, path string) *Ingestor { + return &Ingestor{db: db, logPath: path} +} + +func (this *Ingestor) WatchAndIngest() { ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() for { <-ticker.C - IngestClosedBlocks(db) + this.ingestClosedBlocks() } } -func IngestClosedBlocks(db *clover.DB) { +func (this *Ingestor) ingestClosedBlocks() { entries, err := os.ReadDir(logsDir) if err != nil { log.Println("Error reading logsDir:", err) @@ -54,12 +63,12 @@ func IngestClosedBlocks(db *clover.DB) { // Ingest all except last (current block dir) for _, dir := range blockDirs[:len(blockDirs)-1] { - IngestBlockDir(db, filepath.Join(logsDir, dir.Name())) + this.ingestBlockDir(this.db, filepath.Join(logsDir, dir.Name())) _ = os.RemoveAll(filepath.Join(logsDir, dir.Name())) } } -func IngestBlockDir(db *clover.DB, dirPath string) { +func (this *Ingestor) ingestBlockDir(db *clover.DB, dirPath string) { files, err := os.ReadDir(dirPath) if err != nil { log.Printf("Failed to read block dir %s: %v", dirPath, err) diff --git a/main.go b/main.go index 5294e27..6aba21c 100644 --- a/main.go +++ b/main.go @@ -3,31 +3,32 @@ package main import ( "fmt" "log" - "net/http" "os" "os/signal" "syscall" + "pool-stats/config" "pool-stats/database" "pool-stats/ingest" "pool-stats/web" ) func main() { - db, err := database.InitDatabase() + config := config.ParseFlags() + + db, err := database.InitDatabase(config.DatabasePath) if err != nil { log.Fatalf("Failed to initialize database: %v", err) } defer db.Close() - go ingest.WatchAndIngest(db) + ingestor := ingest.NewIngestor(db, config.LogPath) + go ingestor.WatchAndIngest() - go func() { - handlers := web.Handlers{DB: db} - http.HandleFunc("/", handlers.IndexHandler) - fmt.Println("Listening on :8081") - log.Fatal(http.ListenAndServe(":8081", nil)) - }() + webServer := web.NewWebServer(db, config.Port) + if err := webServer.Start(); err != nil { + log.Fatalf("Failed to start web server: %v", err) + } fmt.Println("Waiting for ctrl-c") sigs := make(chan os.Signal, 1) diff --git a/web/handlers.go b/web/handlers.go index b045575..08c6d4a 100644 --- a/web/handlers.go +++ b/web/handlers.go @@ -5,16 +5,10 @@ import ( "net/http" "pool-stats/stats" - - "github.com/ostafen/clover/v2" ) -type Handlers struct { - DB *clover.DB -} - -func (h Handlers) IndexHandler(w http.ResponseWriter, r *http.Request) { - shareStats, err := stats.GetStats(h.DB) +func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) { + shareStats, err := stats.GetStats(ws.db) if err != nil { http.Error(w, "Failed to load stats", 500) return diff --git a/web/server.go b/web/server.go new file mode 100644 index 0000000..3439e5f --- /dev/null +++ b/web/server.go @@ -0,0 +1,28 @@ +package web + +import ( + "net/http" + + "fmt" + + "github.com/ostafen/clover/v2" +) + +type WebServer struct { + db *clover.DB + port int +} + +func NewWebServer(db *clover.DB, port int) *WebServer { + return &WebServer{ + db: db, + port: port, + } +} + +func (ws *WebServer) Start() error { + http.HandleFunc("/", ws.IndexHandler) + address := ":" + fmt.Sprint(ws.port) + println("Listening on", address) + return http.ListenAndServe(address, nil) +}