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

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