Skip to content

Commit d2fc76d

Browse files
committed
Use embed.FS
1 parent a9c7e38 commit d2fc76d

File tree

2 files changed

+129
-124
lines changed

2 files changed

+129
-124
lines changed

app/server.go

Lines changed: 7 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
"bytes"
55
"context"
6+
"embed"
67
"errors"
78
"fmt"
89
"html/template"
@@ -18,6 +19,10 @@ import (
1819
"github.com/sirupsen/logrus"
1920
)
2021

22+
// Embed the file content as string.
23+
//go:embed static
24+
var staticFiles embed.FS
25+
2126
// HTTP allows interaction with the zackup webserver.
2227
type HTTP interface {
2328
// Start will start the HTTP server.
@@ -198,7 +203,7 @@ func tplPercentUsage(m HostMetrics, val uint64) float64 {
198203
return math.Floor(r*100) / 100
199204
}
200205

201-
var tpl = template.Must(template.New("index").Funcs(template.FuncMap{
206+
var tpl = template.Must(template.New("index.html").Funcs(template.FuncMap{
202207
"fmtTime": tplFmtTime,
203208
"fmtDuration": tplFmtDuration,
204209
"statusClass": tplStatusClass,
@@ -207,126 +212,4 @@ var tpl = template.Must(template.New("index").Funcs(template.FuncMap{
207212
"humanBytes": tplHumanBytes,
208213
"usageDetails": tplUsageDetails,
209214
"percentUsage": tplPercentUsage,
210-
}).Parse(`<!doctype html>
211-
<html>
212-
<head>
213-
<meta charset="UTF-8">
214-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
215-
<title>zackup overview</title>
216-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
217-
integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous">
218-
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css"
219-
integrity="sha256-7rF6RaSKyh16288E3hVdzQtHyzatA2MQRGu0cf6pqqM=" crossorigin="anonymous">
220-
<script>
221-
function toggleTimes() {
222-
const times = document.querySelectorAll("tbody time")
223-
for (let i = 0, len = times.length; i < len; ++i) {
224-
const el = times[i]
225-
if (!el.dataset.text) {
226-
el.dataset.text = el.innerText
227-
}
228-
if (el.innerText === el.dataset.text) {
229-
el.innerText = el.getAttribute("title")
230-
} else {
231-
el.innerText = el.dataset.text
232-
}
233-
}
234-
}
235-
236-
document.addEventListener("DOMContentLoaded", () => {
237-
const btn = document.querySelector("button.js-toggle-times")
238-
if (btn) {
239-
btn.addEventListener("click", ev => {
240-
ev.preventDefault()
241-
toggleTimes()
242-
})
243-
}
244-
})
245-
</script>
246-
<style>
247-
.progress {
248-
height: 3px;
249-
width: 150px;
250-
}
251-
</style>
252-
</head>
253-
254-
<body>
255-
<main class="container-fluid">
256-
<h1>zackup overview</h1>
257-
<table class="table table-sm table-hover table-striped">
258-
<caption class="small">
259-
<button class="btn btn-sm btn-outline-secondary js-toggle-times float-right" type="button">
260-
Toggle times
261-
</button>
262-
Date: {{ fmtTime .Time false }}
263-
<br>
264-
<a href="https://github.com/digineo/zackup">Digineo Zackup</a>
265-
&bull; <a href="https://github.com/digineo/zackup/issues">Issues</a>
266-
</caption>
267-
<thead>
268-
<tr>
269-
<th>Host</th>
270-
<th>Status</th>
271-
<th>last started</th>
272-
<th colspan="2" class="text-center">last succeeded</th>
273-
<th colspan="2" class="text-center">last failed</th>
274-
<th>scheduled for</th>
275-
<th>Space used</th>
276-
<th class="text-right">Compression factor</th>
277-
</tr>
278-
</thead>
279-
<tbody>
280-
{{ range .Hosts }}
281-
<tr>
282-
<td><tt>{{ .Host }}</tt></td>
283-
<td class="{{ statusClass . }}">
284-
<i class="{{ statusIcon . }} fa-fw"></i>&nbsp;{{ .Status }}
285-
</td>
286-
{{ if .StartedAt.IsZero }}
287-
<td>{{ na }}</td>
288-
<td class="text-center" colspan="2">{{ na }}</td>
289-
<td class="text-center" colspan="2">{{ na }}</td>
290-
<td>{{ na }}</td>
291-
<td>{{ na }}</td>
292-
<td class="text-right">{{ na }}</td>
293-
{{ else }}
294-
<td>{{ fmtTime .StartedAt true }}</td>
295-
{{ if .SucceededAt }}
296-
<td class="text-right">{{ fmtTime .SucceededAt true }}</td>
297-
<td><span class="badge badge-secondary">{{ fmtDuration .SuccessDuration }}</span></td>
298-
{{ else }}
299-
<td colspan="2" class="text-center">{{ na }}</td>
300-
{{ end }}
301-
{{ if .FailedAt }}
302-
<td class="text-right">{{ fmtTime .FailedAt true }}</td>
303-
<td><span class="badge badge-secondary">{{ fmtDuration .FailureDuration }}</span></td>
304-
{{ else }}
305-
<td colspan="2" class="text-center">{{ na }}</td>
306-
{{ end }}
307-
<td>
308-
{{ if .ScheduledAt }}
309-
{{ fmtTime .ScheduledAt true }}
310-
{{ else }}
311-
{{ na }}
312-
{{ end }}
313-
</td>
314-
<td title="{{ usageDetails . }}">
315-
<div class="progress">
316-
<div class="progress-bar bg-success" style="width:{{ percentUsage . .SpaceUsedByDataset }}%"></div>
317-
<div class="progress-bar bg-warning" style="width:{{ percentUsage . .SpaceUsedBySnapshots }}%"></div>
318-
<div class="progress-bar bg-info" style="width:{{ percentUsage . .SpaceUsedByChildren }}%"></div>
319-
<div class="progress-bar bg-danger" style="width:{{ percentUsage . .SpaceUsedByRefReservation }}%"></div>
320-
</div>
321-
<small>Total: {{ humanBytes .SpaceUsedTotal }}</small>
322-
</td>
323-
<td class="text-right">{{ printf "%0.2f" .CompressionFactor }}</td>
324-
{{ end }}
325-
</tr>
326-
{{ end }}
327-
</tbody>
328-
</table>
329-
</main>
330-
</body>
331-
</html>
332-
`))
215+
}).ParseFS(staticFiles, "static/index.html"))

app/static/index.html

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<title>zackup overview</title>
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
8+
integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous">
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css"
10+
integrity="sha256-7rF6RaSKyh16288E3hVdzQtHyzatA2MQRGu0cf6pqqM=" crossorigin="anonymous">
11+
<script>
12+
function toggleTimes() {
13+
const times = document.querySelectorAll("tbody time")
14+
for (let i = 0, len = times.length; i < len; ++i) {
15+
const el = times[i]
16+
if (!el.dataset.text) {
17+
el.dataset.text = el.innerText
18+
}
19+
if (el.innerText === el.dataset.text) {
20+
el.innerText = el.getAttribute("title")
21+
} else {
22+
el.innerText = el.dataset.text
23+
}
24+
}
25+
}
26+
27+
document.addEventListener("DOMContentLoaded", () => {
28+
const btn = document.querySelector("button.js-toggle-times")
29+
if (btn) {
30+
btn.addEventListener("click", ev => {
31+
ev.preventDefault()
32+
toggleTimes()
33+
})
34+
}
35+
})
36+
</script>
37+
<style>
38+
.progress {
39+
height: 3px;
40+
width: 150px;
41+
}
42+
</style>
43+
</head>
44+
45+
<body>
46+
<main class="container-fluid">
47+
<h1>zackup overview</h1>
48+
<table class="table table-sm table-hover table-striped">
49+
<caption class="small">
50+
<button class="btn btn-sm btn-outline-secondary js-toggle-times float-right" type="button">
51+
Toggle times
52+
</button>
53+
Date: {{ fmtTime .Time false }}
54+
<br>
55+
<a href="https://github.com/digineo/zackup">Digineo Zackup</a>
56+
&bull; <a href="https://github.com/digineo/zackup/issues">Issues</a>
57+
</caption>
58+
<thead>
59+
<tr>
60+
<th>Host</th>
61+
<th>Status</th>
62+
<th>last started</th>
63+
<th colspan="2" class="text-center">last succeeded</th>
64+
<th colspan="2" class="text-center">last failed</th>
65+
<th>scheduled for</th>
66+
<th>Space used</th>
67+
<th class="text-right">Compression factor</th>
68+
</tr>
69+
</thead>
70+
<tbody>
71+
{{ range .Hosts }}
72+
<tr>
73+
<td><tt>{{ .Host }}</tt></td>
74+
<td class="{{ statusClass . }}">
75+
<i class="{{ statusIcon . }} fa-fw"></i>&nbsp;{{ .Status }}
76+
</td>
77+
{{ if .StartedAt.IsZero }}
78+
<td>{{ na }}</td>
79+
<td class="text-center" colspan="2">{{ na }}</td>
80+
<td class="text-center" colspan="2">{{ na }}</td>
81+
<td>{{ na }}</td>
82+
<td>{{ na }}</td>
83+
<td class="text-right">{{ na }}</td>
84+
{{ else }}
85+
<td>{{ fmtTime .StartedAt true }}</td>
86+
{{ if .SucceededAt }}
87+
<td class="text-right">{{ fmtTime .SucceededAt true }}</td>
88+
<td><span class="badge badge-secondary">{{ fmtDuration .SuccessDuration }}</span></td>
89+
{{ else }}
90+
<td colspan="2" class="text-center">{{ na }}</td>
91+
{{ end }}
92+
{{ if .FailedAt }}
93+
<td class="text-right">{{ fmtTime .FailedAt true }}</td>
94+
<td><span class="badge badge-secondary">{{ fmtDuration .FailureDuration }}</span></td>
95+
{{ else }}
96+
<td colspan="2" class="text-center">{{ na }}</td>
97+
{{ end }}
98+
<td>
99+
{{ if .ScheduledAt }}
100+
{{ fmtTime .ScheduledAt true }}
101+
{{ else }}
102+
{{ na }}
103+
{{ end }}
104+
</td>
105+
<td title="{{ usageDetails . }}">
106+
<div class="progress">
107+
<div class="progress-bar bg-success" style="width:{{ percentUsage . .SpaceUsedByDataset }}%"></div>
108+
<div class="progress-bar bg-warning" style="width:{{ percentUsage . .SpaceUsedBySnapshots }}%"></div>
109+
<div class="progress-bar bg-info" style="width:{{ percentUsage . .SpaceUsedByChildren }}%"></div>
110+
<div class="progress-bar bg-danger" style="width:{{ percentUsage . .SpaceUsedByRefReservation }}%"></div>
111+
</div>
112+
<small>Total: {{ humanBytes .SpaceUsedTotal }}</small>
113+
</td>
114+
<td class="text-right">{{ printf "%0.2f" .CompressionFactor }}</td>
115+
{{ end }}
116+
</tr>
117+
{{ end }}
118+
</tbody>
119+
</table>
120+
</main>
121+
</body>
122+
</html>

0 commit comments

Comments
 (0)