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"}}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Share Stats</title>
<style>
body {
font-family: sans-serif;
background: #111;
color: #eee;
text-align: center;
padding: 2em;
}
table {
margin: auto;
border-collapse: collapse;
}
th,
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 }}
{{ define "title" }}Share Stats{{ end }} {{ define "header" }}🌟 Pool Share
Stats{{ end }} {{ define "content" }}
<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>
{{ end }} {{ template "layout" . }}

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" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>ckpool Share Browser</title>
<style>
body {
font-family: sans-serif;
background: #111;
color: #eee;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
padding: 8px 12px;
border: 1px solid #444;
text-align: left;
white-space: nowrap;
}
th {
background-color: #222;
}
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 Share Browser</h1>
{{ define "title" }}Share Browser{{ end }} {{ define "header" }}☀️ Pool Share
Browser{{ end }} {{ define "content" }}
<table>
<thead>
<tr>
<th>Time</th>
<th>Worker</th>
<th>Address</th>
<th>SDiff</th>
<th>Result</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>{{ 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>
<table>
<thead>
<tr>
<th>Time</th>
<th>Worker</th>
<th>Address</th>
<th>SDiff</th>
<th>Result</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>{{ 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>
{{ 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 }}
<div>
{{ 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>
<a class="page-link current" href="?page={{ .Page }}">{{ .Page }}</a>
{{ if .HasMore }}
<span class="page-link">...</span>
<a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a>
{{ end }}
</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 }}
{{ if .HasMore }}
<span class="page-link">...</span>
<a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a>
{{ end }}
</div>
{{ end }} {{ template "layout" . }}

View File

@ -1,84 +1,27 @@
{{ define "top_shares" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>ckpool Top Shares</title>
<style>
body {
font-family: sans-serif;
background: #111;
color: #eee;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
padding: 8px 12px;
border: 1px solid #444;
text-align: left;
white-space: nowrap;
}
th {
background-color: #222;
}
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 }}
{{ define "title" }}Top Shares{{ end }} {{ define "header" }}☀️ Pool Top
Shares{{ end }} {{ define "content" }}
<table>
<thead>
<tr>
<th>Time</th>
<th>Worker</th>
<th>SDiff</th>
<th>Hash</th>
</tr>
</thead>
<tbody>
{{ range .Shares }}
<tr>
<td>{{ formatCreateDate .CreateDate }}</td>
<td>{{ .WorkerName }}</td>
<td>{{ humanDiff .SDiff }}</td>
<td><code style="font-size: small">{{ .Hash }}</code></td>
</tr>
{{ else }}
<tr>
<td colspan="4">No shares found.</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }} {{ template "layout" . }}

View File

@ -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

View File

@ -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,
}
}

View File

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

View File

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