Extract page layout

This commit is contained in:
Pijus Kamandulis
2025-06-23 18:42:00 +03:00
parent be637f4540
commit f66fbcc454
8 changed files with 190 additions and 282 deletions

View File

@@ -1,67 +1,19 @@
{{define "index"}} {{ define "title" }}Share Stats{{ end }} {{ define "header" }}🌟 Pool Share
<!DOCTYPE html> Stats{{ end }} {{ define "content" }}
<html> <table>
<head> <tr>
<meta charset="UTF-8" /> <th>Range</th>
<title>Share Stats</title> <th>Highest Share Diff</th>
<style> <th>Time</th>
body { </tr>
font-family: sans-serif; {{ range .Stats }}
background: #111; <tr>
color: #eee; <td>{{ .TimeWindowName }}</td>
text-align: center; <td>
padding: 2em; {{ if ne .SDiff 0.0 }} {{ humanDiff .SDiff }} {{ else }} - {{ end }}
} </td>
table { <td>{{ formatCreateDate .Time }}</td>
margin: auto; </tr>
border-collapse: collapse; {{ end }}
} </table>
th, {{ end }} {{ template "layout" . }}
td {
padding: 0.5em 1em;
border: 1px solid #444;
}
th {
background-color: #222;
}
tr:nth-child(even) {
background-color: #1a1a1a;
}
a {
color: #0af;
text-decoration: none;
}
li {
display: inline;
margin: 0 10px;
}
</style>
</head>
<body>
<h1>🌟 Pool Share Stats</h1>
<table>
<tr>
<th>Range</th>
<th>Highest Share Diff</th>
<th>Time</th>
</tr>
{{ range .Stats }}
<tr>
<td>{{ .TimeWindowName }}</td>
<td>
{{ if ne .SDiff 0.0 }} {{ humanDiff .SDiff }} {{ else }} - {{ end }}
</td>
<td>{{ formatCreateDate .Time }}</td>
</tr>
{{ end }}
</table>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
<li><a href="/top-shares">Top Shares</a></li>
</ul>
</body>
</html>
{{ end }}

67
templates/layout.html Normal file
View File

@@ -0,0 +1,67 @@
{{ define "layout" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{ template "title" . }}</title>
<style>
body {
font-family: sans-serif;
background: #111;
color: #eee;
padding: 20px;
text-align: center;
}
table {
border-collapse: collapse;
margin: auto;
margin-bottom: 20px;
}
table.fw {
width: 100%;
}
th,
td {
padding: 8px 12px;
border: 1px solid #444;
text-align: left;
white-space: nowrap;
}
th {
background-color: #222;
}
tr:nth-child(even) {
background-color: #1a1a1a;
}
a.page-link {
margin: 0 5px;
text-decoration: none;
color: #0af;
}
a.page-link.current {
font-weight: bold;
color: #fff;
}
a {
color: #0af;
text-decoration: none;
}
li {
display: inline;
margin: 0 10px;
}
</style>
</head>
<body>
<h1>{{ template "header" . }}</h1>
{{ template "content" . }} {{ template "navigation" . }}
</body>
</html>
{{ end }} {{ define "navigation" }}
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
<li><a href="/top-shares">Top Shares</a></li>
</ul>
{{ end }}

View File

@@ -1,104 +1,48 @@
{{ define "share_list" }} {{ define "title" }}Share Browser{{ end }} {{ define "header" }}☀️ Pool Share
<!DOCTYPE html> Browser{{ end }} {{ define "content" }}
<html lang="en"> <table>
<head> <thead>
<meta charset="UTF-8" /> <tr>
<title>ckpool Share Browser</title> <th>Time</th>
<style> <th>Worker</th>
body { <th>Address</th>
font-family: sans-serif; <th>SDiff</th>
background: #111; <th>Result</th>
color: #eee; <th>Hash</th>
padding: 20px; </tr>
} </thead>
table { <tbody>
width: 100%; {{ range .Shares }}
border-collapse: collapse; <tr>
margin-bottom: 20px; <td>{{ formatCreateDate .CreateDate }}</td>
} <td>{{ .WorkerName }}</td>
th, <td>{{ .Address }}</td>
td { <td>{{ humanDiff .SDiff }}</td>
padding: 8px 12px; <td>{{ if .Result }}✔️{{ else }}❌{{ end }}</td>
border: 1px solid #444; <td><code style="font-size: small">{{ .Hash }}</code></td>
text-align: left; </tr>
white-space: nowrap; {{ else }}
} <tr>
th { <td colspan="6">No shares found.</td>
background-color: #222; </tr>
} {{ end }}
a.page-link { </tbody>
margin: 0 5px; </table>
text-decoration: none;
color: #0af;
}
a.page-link.current {
font-weight: bold;
color: #fff;
}
a {
color: #0af;
text-decoration: none;
}
li {
display: inline;
margin: 0 10px;
}
</style>
</head>
<body>
<h1>☀️ Pool Share Browser</h1>
<table> <div>
<thead> {{ if gt .Page 1 }}
<tr> <a class="page-link" href="?page={{ sub .Page 1 }}">&laquo; Prev</a>
<th>Time</th> {{ end }} {{ if gt .Page 2 }}
<th>Worker</th> <a class="page-link" href="?page=1">1</a>
<th>Address</th> {{ if gt .Page 3 }}
<th>SDiff</th> <span class="page-link">...</span>
<th>Result</th> {{ end }} {{ end }}
<th>Hash</th>
</tr>
</thead>
<tbody>
{{ range .Shares }}
<tr>
<td>{{ formatCreateDate .CreateDate }}</td>
<td>{{ .WorkerName }}</td>
<td>{{ .Address }}</td>
<td>{{ humanDiff .SDiff }}</td>
<td>{{ if .Result }}✔️{{ else }}❌{{ end }}</td>
<td><code style="font-size: small">{{ .Hash }}</code></td>
</tr>
{{ else }}
<tr>
<td colspan="6">No shares found.</td>
</tr>
{{ end }}
</tbody>
</table>
<div> <a class="page-link current" href="?page={{ .Page }}">{{ .Page }}</a>
{{ if gt .Page 1 }}
<a class="page-link" href="?page={{ sub .Page 1 }}">&laquo; Prev</a>
{{ end }} {{ if gt .Page 2 }}
<a class="page-link" href="?page=1">1</a>
{{ if gt .Page 3 }}
<span class="page-link">...</span>
{{ end }} {{ end }}
<a class="page-link current" href="?page={{ .Page }}">{{ .Page }}</a> {{ if .HasMore }}
<span class="page-link">...</span>
{{ if .HasMore }} <a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a>
<span class="page-link">...</span> {{ end }}
<a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a> </div>
{{ end }} {{ end }} {{ template "layout" . }}
</div>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
<li><a href="/top-shares">Top Shares</a></li>
</ul>
</body>
</html>
{{ end }}

View File

@@ -1,84 +1,27 @@
{{ define "top_shares" }} {{ define "title" }}Top Shares{{ end }} {{ define "header" }}☀️ Pool Top
<!DOCTYPE html> Shares{{ end }} {{ define "content" }}
<html lang="en"> <table>
<head> <thead>
<meta charset="UTF-8" /> <tr>
<title>ckpool Top Shares</title> <th>Time</th>
<style> <th>Worker</th>
body { <th>SDiff</th>
font-family: sans-serif; <th>Hash</th>
background: #111; </tr>
color: #eee; </thead>
padding: 20px; <tbody>
} {{ range .Shares }}
table { <tr>
width: 100%; <td>{{ formatCreateDate .CreateDate }}</td>
border-collapse: collapse; <td>{{ .WorkerName }}</td>
margin-bottom: 20px; <td>{{ humanDiff .SDiff }}</td>
} <td><code style="font-size: small">{{ .Hash }}</code></td>
th, </tr>
td { {{ else }}
padding: 8px 12px; <tr>
border: 1px solid #444; <td colspan="4">No shares found.</td>
text-align: left; </tr>
white-space: nowrap; {{ end }}
} </tbody>
th { </table>
background-color: #222; {{ end }} {{ template "layout" . }}
}
a.page-link {
margin: 0 5px;
text-decoration: none;
color: #0af;
}
a.page-link.current {
font-weight: bold;
color: #fff;
}
a {
color: #0af;
text-decoration: none;
}
li {
display: inline;
margin: 0 10px;
}
</style>
</head>
<body>
<h1>☀️ Pool Top Shares</h1>
<table>
<thead>
<tr>
<th>Time</th>
<th>Worker</th>
<th>Address</th>
<th>SDiff</th>
<th>Hash</th>
</tr>
</thead>
<tbody>
{{ range .Shares }}
<tr>
<td>{{ formatCreateDate .CreateDate }}</td>
<td>{{ .WorkerName }}</td>
<td>{{ .Address }}</td>
<td>{{ humanDiff .SDiff }}</td>
<td><code style="font-size: small">{{ .Hash }}</code></td>
</tr>
{{ else }}
<tr>
<td colspan="6">No shares found.</td>
</tr>
{{ end }}
</tbody>
</table>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
<li><a href="/top-shares">Top Shares</a></li>
</ul>
</body>
</html>
{{ end }}

View File

@@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"pool-stats/database" "pool-stats/database"
"pool-stats/helpers"
"pool-stats/models" "pool-stats/models"
) )
@@ -14,13 +13,10 @@ type IndexPageData struct {
} }
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) { func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("index").Funcs(template.FuncMap{ tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/index.html")
"humanDiff": helpers.HumanDiff,
"formatCreateDate": helpers.FormatCreateDate,
})
tmpl, err := tmpl.ParseFiles("templates/index.html")
if err != nil { 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 return
} }
@@ -33,7 +29,8 @@ func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
indexData := IndexPageData{ indexData := IndexPageData{
Stats: tws, 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) http.Error(w, "Failed to render template", 500)
println("Error rendering template:", err.Error()) println("Error rendering template:", err.Error())
return return

View File

@@ -1,7 +1,9 @@
package web package web
import ( import (
"html/template"
"net/http" "net/http"
"pool-stats/helpers"
"fmt" "fmt"
@@ -9,14 +11,27 @@ import (
) )
type WebServer struct { type WebServer struct {
db *clover.DB db *clover.DB
port int port int
templates *template.Template
} }
func NewWebServer(db *clover.DB, port int) *WebServer { 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{ return &WebServer{
db: db, db: db,
port: port, port: port,
templates: templates,
} }
} }

View File

@@ -4,7 +4,6 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"pool-stats/database" "pool-stats/database"
"pool-stats/helpers"
"pool-stats/models" "pool-stats/models"
"strconv" "strconv"
) )
@@ -16,15 +15,10 @@ type SharePageData struct {
} }
func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) { func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("share_list").Funcs(template.FuncMap{ tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/shares.html")
"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")
if err != nil { 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 return
} }
@@ -52,7 +46,7 @@ func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
Page: offset/entriesPerPage + 1, Page: offset/entriesPerPage + 1,
HasMore: len(shareLogs) == entriesPerPage, 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) http.Error(w, "Failed to render template", 500)
return return
} }

View File

@@ -4,7 +4,6 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"pool-stats/database" "pool-stats/database"
"pool-stats/helpers"
"pool-stats/models" "pool-stats/models"
) )
@@ -13,13 +12,10 @@ type TopSharesPageData struct {
} }
func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) { func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("top_shares").Funcs(template.FuncMap{ tmpl, err := template.Must(ws.templates.Clone()).ParseFiles("templates/top_shares.html")
"humanDiff": helpers.HumanDiff,
"formatCreateDate": helpers.FormatCreateDate,
})
tmpl, err := tmpl.ParseFiles("templates/top_shares.html")
if err != nil { 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 return
} }
@@ -32,7 +28,7 @@ func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) {
data := TopSharesPageData{ data := TopSharesPageData{
Shares: topShares, 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) http.Error(w, "Failed to render template", 500)
return return
} }