Extract page layout
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"net/http"
|
||||
|
||||
"pool-stats/database"
|
||||
"pool-stats/helpers"
|
||||
"pool-stats/models"
|
||||
)
|
||||
|
||||
@@ -14,13 +13,10 @@ type IndexPageData struct {
|
||||
}
|
||||
|
||||
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl := template.New("index").Funcs(template.FuncMap{
|
||||
"humanDiff": helpers.HumanDiff,
|
||||
"formatCreateDate": helpers.FormatCreateDate,
|
||||
})
|
||||
tmpl, err := tmpl.ParseFiles("templates/index.html")
|
||||
tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/index.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to load template", 500)
|
||||
http.Error(w, "Failed to parse template", 500)
|
||||
println("Error parsing template:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,7 +29,8 @@ func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
indexData := IndexPageData{
|
||||
Stats: tws,
|
||||
}
|
||||
if err := tmpl.Execute(w, indexData); err != nil {
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "index.html", indexData); err != nil {
|
||||
http.Error(w, "Failed to render template", 500)
|
||||
println("Error rendering template:", err.Error())
|
||||
return
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"pool-stats/helpers"
|
||||
|
||||
"fmt"
|
||||
|
||||
@@ -9,14 +11,27 @@ import (
|
||||
)
|
||||
|
||||
type WebServer struct {
|
||||
db *clover.DB
|
||||
port int
|
||||
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,
|
||||
"formatCreateDate": helpers.FormatCreateDate,
|
||||
})
|
||||
|
||||
templates = template.Must(templates.ParseFiles(
|
||||
"templates/layout.html",
|
||||
))
|
||||
|
||||
return &WebServer{
|
||||
db: db,
|
||||
port: port,
|
||||
db: db,
|
||||
port: port,
|
||||
templates: templates,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"pool-stats/database"
|
||||
"pool-stats/helpers"
|
||||
"pool-stats/models"
|
||||
"strconv"
|
||||
)
|
||||
@@ -16,15 +15,10 @@ type SharePageData struct {
|
||||
}
|
||||
|
||||
func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl := template.New("share_list").Funcs(template.FuncMap{
|
||||
"add": func(a, b int) int { return a + b },
|
||||
"sub": func(a, b int) int { return a - b },
|
||||
"humanDiff": helpers.HumanDiff,
|
||||
"formatCreateDate": helpers.FormatCreateDate,
|
||||
})
|
||||
tmpl, err := tmpl.ParseFiles("templates/shares.html")
|
||||
tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/shares.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to load template", 500)
|
||||
http.Error(w, "Failed to parse template", 500)
|
||||
println("Error parsing template:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -52,7 +46,7 @@ func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
Page: offset/entriesPerPage + 1,
|
||||
HasMore: len(shareLogs) == entriesPerPage,
|
||||
}
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
if err := tmpl.ExecuteTemplate(w, "shares.html", data); err != nil {
|
||||
http.Error(w, "Failed to render template", 500)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"pool-stats/database"
|
||||
"pool-stats/helpers"
|
||||
"pool-stats/models"
|
||||
)
|
||||
|
||||
@@ -13,13 +12,10 @@ type TopSharesPageData struct {
|
||||
}
|
||||
|
||||
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")
|
||||
tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/top_shares.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to load template", 500)
|
||||
http.Error(w, "Failed to parse template", 500)
|
||||
println("Error parsing template:", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -32,7 +28,7 @@ func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := TopSharesPageData{
|
||||
Shares: topShares,
|
||||
}
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
if err := tmpl.ExecuteTemplate(w, "top_shares.html", data); err != nil {
|
||||
http.Error(w, "Failed to render template", 500)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user