Implement top shares page

This commit is contained in:
Pijus Kamandulis
2025-06-23 14:37:06 +03:00
parent 260d2ec24b
commit d801debaf6
10 changed files with 279 additions and 9 deletions

View File

@@ -23,6 +23,7 @@ func NewWebServer(db *clover.DB, port int) *WebServer {
func (ws *WebServer) Start() error {
http.HandleFunc("/", ws.IndexHandler)
http.HandleFunc("/shares", ws.SharesHandler)
http.HandleFunc("/top-shares", ws.TopSharesHandler)
address := ":" + fmt.Sprint(ws.port)
println("Listening on", address)

39
web/topSharesHandler.go Normal file
View File

@@ -0,0 +1,39 @@
package web
import (
"html/template"
"net/http"
"pool-stats/database"
"pool-stats/helpers"
"pool-stats/models"
)
type TopSharesPageData struct {
Shares []models.ShareLog
}
func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("top_shares").Funcs(template.FuncMap{
"humanDiff": helpers.HumanDiff,
"formatCreateDate": helpers.FormatCreateDate,
})
tmpl, err := tmpl.ParseFiles("templates/top_shares.html")
if err != nil {
http.Error(w, "Failed to load template", 500)
return
}
topShares := database.ListTopShares(ws.db)
if topShares == nil {
http.Error(w, "Failed to load top shares", 500)
return
}
data := TopSharesPageData{
Shares: topShares,
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Failed to render template", 500)
return
}
}