refactor-code-modules #1

Merged
pk merged 3 commits from refactor-code-modules into master 2025-05-27 19:30:13 +01:00
7 changed files with 38 additions and 38 deletions
Showing only changes of commit 5bfa8422ca - Show all commits

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
logs/ logs/
badgerdb/ badgerdb/
notes.md

View File

@ -15,10 +15,6 @@ const (
CollectionName = "shares" CollectionName = "shares"
) )
type Handlers struct {
DB *clover.DB
}
func InitDatabase() (*clover.DB, error) { func InitDatabase() (*clover.DB, error) {
store, err := badgerstore.Open("badgerdb") store, err := badgerstore.Open("badgerdb")
if err != nil { if err != nil {
@ -79,4 +75,4 @@ func PrintAllHashes(db *clover.DB) {
hash := doc.Get("Hash") hash := doc.Get("Hash")
fmt.Println(hash) fmt.Println(hash)
} }
} }

View File

@ -97,4 +97,4 @@ func IngestBlockDir(db *clover.DB, dirPath string) {
} }
log.Printf("Ingested and deleted %s", dirPath) log.Printf("Ingested and deleted %s", dirPath)
} }

52
main.go
View File

@ -1,38 +1,36 @@
package main package main
import ( import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"pool-stats/database" "pool-stats/database"
"pool-stats/ingest" "pool-stats/ingest"
"pool-stats/web" "pool-stats/web"
) )
func main() { func main() {
db, err := database.InitDatabase() db, err := database.InitDatabase()
if err != nil { if err != nil {
log.Fatalf("Failed to initialize database: %v", err) log.Fatalf("Failed to initialize database: %v", err)
} }
defer db.Close() 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() { fmt.Println("Waiting for ctrl-c")
handlers := database.Handlers{DB: db} sigs := make(chan os.Signal, 1)
http.HandleFunc("/", handlers.IndexHandler) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("Listening on :8081") <-sigs
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
} }

View File

@ -43,4 +43,4 @@ func (s *ShareLog) ParseCreateDate() (time.Time, error) {
return time.Time{}, err return time.Time{}, err
} }
return time.Unix(sec, nsec), nil return time.Unix(sec, nsec), nil
} }

View File

@ -69,4 +69,4 @@ func GetStats(db *clover.DB) ([]models.ShareStat, error) {
} }
return stats, nil return stats, nil
} }

View File

@ -4,11 +4,16 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"pool-stats/database"
"pool-stats/stats" "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) shareStats, err := stats.GetStats(h.DB)
if err != nil { if err != nil {
http.Error(w, "Failed to load stats", 500) 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 := template.Must(template.ParseFiles("templates/index.html"))
tmpl.Execute(w, shareStats) tmpl.Execute(w, shareStats)
} }