49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package web
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"pool-stats/helpers"
|
|
|
|
"fmt"
|
|
|
|
"github.com/ostafen/clover/v2"
|
|
)
|
|
|
|
type WebServer struct {
|
|
db *clover.DB
|
|
port int
|
|
templates *template.Template
|
|
}
|
|
|
|
func NewWebServer(db *clover.DB, port int) *WebServer {
|
|
templates := template.New("base").Funcs(template.FuncMap{
|
|
"add": func(a, b int) int { return a + b },
|
|
"sub": func(a, b int) int { return a - b },
|
|
"humanDiff": helpers.HumanDiff,
|
|
"formatHashrate": helpers.FormatHashrate,
|
|
"formatCreateDate": helpers.FormatCreateDate,
|
|
})
|
|
|
|
templates = template.Must(templates.ParseFiles(
|
|
"templates/layout.html",
|
|
))
|
|
|
|
return &WebServer{
|
|
db: db,
|
|
port: port,
|
|
templates: templates,
|
|
}
|
|
}
|
|
|
|
func (ws *WebServer) Start() error {
|
|
http.HandleFunc("/", ws.IndexHandler)
|
|
http.HandleFunc("/shares", ws.SharesHandler)
|
|
http.HandleFunc("/top-shares", ws.TopSharesHandler)
|
|
http.HandleFunc("/daily-stats", ws.DailyStatsHandler)
|
|
|
|
address := ":" + fmt.Sprint(ws.port)
|
|
println("Listening on", address)
|
|
return http.ListenAndServe(address, nil)
|
|
}
|