diff --git a/templates/index.html b/templates/index.html
index 6b931eb..43fd07a 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -1,67 +1,19 @@
-{{define "index"}}
-
-
-
-
- Share Stats
-
-
-
- 🌟 Pool Share Stats
-
-
-
- Range |
- Highest Share Diff |
- Time |
-
- {{ range .Stats }}
-
- {{ .TimeWindowName }} |
-
- {{ if ne .SDiff 0.0 }} {{ humanDiff .SDiff }} {{ else }} - {{ end }}
- |
- {{ formatCreateDate .Time }} |
-
- {{ end }}
-
-
-
-
-
-{{ end }}
+{{ define "title" }}Share Stats{{ end }} {{ define "header" }}🌟 Pool Share
+Stats{{ end }} {{ define "content" }}
+
+
+ Range |
+ Highest Share Diff |
+ Time |
+
+ {{ range .Stats }}
+
+ {{ .TimeWindowName }} |
+
+ {{ if ne .SDiff 0.0 }} {{ humanDiff .SDiff }} {{ else }} - {{ end }}
+ |
+ {{ formatCreateDate .Time }} |
+
+ {{ end }}
+
+{{ end }} {{ template "layout" . }}
diff --git a/templates/layout.html b/templates/layout.html
new file mode 100644
index 0000000..f6150b7
--- /dev/null
+++ b/templates/layout.html
@@ -0,0 +1,67 @@
+{{ define "layout" }}
+
+
+
+
+ {{ template "title" . }}
+
+
+
+ {{ template "header" . }}
+
+ {{ template "content" . }} {{ template "navigation" . }}
+
+
+{{ end }} {{ define "navigation" }}
+
+{{ end }}
diff --git a/templates/shares.html b/templates/shares.html
index ede3665..8d75160 100644
--- a/templates/shares.html
+++ b/templates/shares.html
@@ -1,104 +1,48 @@
-{{ define "share_list" }}
-
-
-
-
- ckpool Share Browser
-
-
-
- ☀️ Pool Share Browser
+{{ define "title" }}Share Browser{{ end }} {{ define "header" }}☀️ Pool Share
+Browser{{ end }} {{ define "content" }}
+
+
+
+ Time |
+ Worker |
+ Address |
+ SDiff |
+ Result |
+ Hash |
+
+
+
+ {{ range .Shares }}
+
+ {{ formatCreateDate .CreateDate }} |
+ {{ .WorkerName }} |
+ {{ .Address }} |
+ {{ humanDiff .SDiff }} |
+ {{ if .Result }}✔️{{ else }}❌{{ end }} |
+ {{ .Hash }} |
+
+ {{ else }}
+
+ No shares found. |
+
+ {{ end }}
+
+
-
-
-
- Time |
- Worker |
- Address |
- SDiff |
- Result |
- Hash |
-
-
-
- {{ range .Shares }}
-
- {{ formatCreateDate .CreateDate }} |
- {{ .WorkerName }} |
- {{ .Address }} |
- {{ humanDiff .SDiff }} |
- {{ if .Result }}✔️{{ else }}❌{{ end }} |
- {{ .Hash }} |
-
- {{ else }}
-
- No shares found. |
-
- {{ end }}
-
-
+
+ {{ if gt .Page 1 }}
+
« Prev
+ {{ end }} {{ if gt .Page 2 }}
+
1
+ {{ if gt .Page 3 }}
+
...
+ {{ end }} {{ end }}
-
- {{ if gt .Page 1 }}
-
« Prev
- {{ end }} {{ if gt .Page 2 }}
-
1
- {{ if gt .Page 3 }}
-
...
- {{ end }} {{ end }}
+
{{ .Page }}
-
{{ .Page }}
-
- {{ if .HasMore }}
-
...
-
Next »
- {{ end }}
-
-
-
-
-
-{{ end }}
+ {{ if .HasMore }}
+
...
+
Next »
+ {{ end }}
+
+{{ end }} {{ template "layout" . }}
diff --git a/templates/top_shares.html b/templates/top_shares.html
index 197856a..18ebab5 100644
--- a/templates/top_shares.html
+++ b/templates/top_shares.html
@@ -1,84 +1,27 @@
-{{ define "top_shares" }}
-
-
-
-
- ckpool Top Shares
-
-
-
- ☀️ Pool Top Shares
-
-
-
-
- Time |
- Worker |
- Address |
- SDiff |
- Hash |
-
-
-
- {{ range .Shares }}
-
- {{ formatCreateDate .CreateDate }} |
- {{ .WorkerName }} |
- {{ .Address }} |
- {{ humanDiff .SDiff }} |
- {{ .Hash }} |
-
- {{ else }}
-
- No shares found. |
-
- {{ end }}
-
-
-
-
-
-{{ end }}
+{{ define "title" }}Top Shares{{ end }} {{ define "header" }}☀️ Pool Top
+Shares{{ end }} {{ define "content" }}
+
+
+
+ Time |
+ Worker |
+ SDiff |
+ Hash |
+
+
+
+ {{ range .Shares }}
+
+ {{ formatCreateDate .CreateDate }} |
+ {{ .WorkerName }} |
+ {{ humanDiff .SDiff }} |
+ {{ .Hash }} |
+
+ {{ else }}
+
+ No shares found. |
+
+ {{ end }}
+
+
+{{ end }} {{ template "layout" . }}
diff --git a/web/indexHandler.go b/web/indexHandler.go
index c21c802..d3361a3 100644
--- a/web/indexHandler.go
+++ b/web/indexHandler.go
@@ -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
diff --git a/web/server.go b/web/server.go
index 73b0fee..b46ae2e 100644
--- a/web/server.go
+++ b/web/server.go
@@ -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,
}
}
diff --git a/web/sharesHandler.go b/web/sharesHandler.go
index 26d030e..bc99579 100644
--- a/web/sharesHandler.go
+++ b/web/sharesHandler.go
@@ -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
}
diff --git a/web/topSharesHandler.go b/web/topSharesHandler.go
index 6e3b692..56e0ce0 100644
--- a/web/topSharesHandler.go
+++ b/web/topSharesHandler.go
@@ -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
}