From 5bfa8422ca1baedcf865c9c8f316b890ead8040b Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Tue, 27 May 2025 21:29:23 +0300 Subject: [PATCH] fix shit --- .gitignore | 1 + database/db.go | 6 +----- ingest/ingest.go | 2 +- main.go | 52 ++++++++++++++++++++++----------------------- models/share_log.go | 2 +- stats/stats.go | 2 +- web/handlers.go | 11 +++++++--- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 5c36f60..4766f46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ logs/ badgerdb/ +notes.md diff --git a/database/db.go b/database/db.go index b4261f9..30d83c8 100644 --- a/database/db.go +++ b/database/db.go @@ -15,10 +15,6 @@ const ( CollectionName = "shares" ) -type Handlers struct { - DB *clover.DB -} - func InitDatabase() (*clover.DB, error) { store, err := badgerstore.Open("badgerdb") if err != nil { @@ -79,4 +75,4 @@ func PrintAllHashes(db *clover.DB) { hash := doc.Get("Hash") fmt.Println(hash) } -} \ No newline at end of file +} diff --git a/ingest/ingest.go b/ingest/ingest.go index fee5228..985caf2 100644 --- a/ingest/ingest.go +++ b/ingest/ingest.go @@ -97,4 +97,4 @@ func IngestBlockDir(db *clover.DB, dirPath string) { } log.Printf("Ingested and deleted %s", dirPath) -} \ No newline at end of file +} diff --git a/main.go b/main.go index 53c5e00..5294e27 100644 --- a/main.go +++ b/main.go @@ -1,38 +1,36 @@ package main import ( - "fmt" - "log" - "net/http" - "os" - "os/signal" - "syscall" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" - "pool-stats/database" - "pool-stats/ingest" - "pool-stats/web" + "pool-stats/database" + "pool-stats/ingest" + "pool-stats/web" ) func main() { - db, err := database.InitDatabase() - if err != nil { - log.Fatalf("Failed to initialize database: %v", err) - } - defer db.Close() + db, err := database.InitDatabase() + if err != nil { + log.Fatalf("Failed to initialize database: %v", err) + } + defer db.Close() - database.PrintAllHashes(db) + go ingest.WatchAndIngest(db) - go ingest.WatchAndIngest(db) + go func() { + handlers := web.Handlers{DB: db} + http.HandleFunc("/", handlers.IndexHandler) + fmt.Println("Listening on :8081") + log.Fatal(http.ListenAndServe(":8081", nil)) + }() - go func() { - handlers := database.Handlers{DB: db} - http.HandleFunc("/", handlers.IndexHandler) - fmt.Println("Listening on :8081") - log.Fatal(http.ListenAndServe(":8081", nil)) - }() - - fmt.Println("Waiting for ctrl-c") - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - <-sigs + fmt.Println("Waiting for ctrl-c") + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + <-sigs } diff --git a/models/share_log.go b/models/share_log.go index ccfaf47..92c7baf 100644 --- a/models/share_log.go +++ b/models/share_log.go @@ -43,4 +43,4 @@ func (s *ShareLog) ParseCreateDate() (time.Time, error) { return time.Time{}, err } return time.Unix(sec, nsec), nil -} \ No newline at end of file +} diff --git a/stats/stats.go b/stats/stats.go index 0e6e4c4..3aa0266 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -69,4 +69,4 @@ func GetStats(db *clover.DB) ([]models.ShareStat, error) { } return stats, nil -} \ No newline at end of file +} diff --git a/web/handlers.go b/web/handlers.go index 7dbd85f..b045575 100644 --- a/web/handlers.go +++ b/web/handlers.go @@ -4,11 +4,16 @@ import ( "html/template" "net/http" - "pool-stats/database" "pool-stats/stats" + + "github.com/ostafen/clover/v2" ) -func (h database.Handlers) IndexHandler(w http.ResponseWriter, r *http.Request) { +type Handlers struct { + DB *clover.DB +} + +func (h Handlers) IndexHandler(w http.ResponseWriter, r *http.Request) { shareStats, err := stats.GetStats(h.DB) if err != nil { http.Error(w, "Failed to load stats", 500) @@ -17,4 +22,4 @@ func (h database.Handlers) IndexHandler(w http.ResponseWriter, r *http.Request) tmpl := template.Must(template.ParseFiles("templates/index.html")) tmpl.Execute(w, shareStats) -} \ No newline at end of file +}