add configuration

This commit is contained in:
Pijus Kamandulis 2025-05-27 21:56:19 +03:00
parent d836830f45
commit 1cc12afa16
6 changed files with 79 additions and 24 deletions

23
config/config.go Normal file
View File

@ -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,
}
}

View File

@ -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)
}

View File

@ -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)

19
main.go
View File

@ -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)

View File

@ -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

28
web/server.go Normal file
View File

@ -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)
}