-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathweb.go
More file actions
139 lines (123 loc) · 3.6 KB
/
web.go
File metadata and controls
139 lines (123 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"html/template"
"log"
"math"
"net/http"
"os"
"os/signal"
"path/filepath"
"github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
)
var funcMap = template.FuncMap{
"plus": plus,
"minus": minus,
"minus64": minus64,
"commaSeparate": commaSeparate,
"twoDecimalPlaces": twoDecimalPlaces,
"blocksToTimeEstimate": blocksToTimeEstimate,
}
func plus(a, b int) int {
return a + b
}
func minus(a, b int) int {
return a - b
}
func minus64(a, b int64) int64 {
return a - b
}
func commaSeparate(number int64) string {
return humanize.Comma(number)
}
func twoDecimalPlaces(number float64) string {
number = math.Floor(number*100) / 100
return fmt.Sprintf("%.2f", number)
}
// renders the 'home' template which is currently located at "start.html".
func (td *WebUI) homePage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Frame-Options", "DENY")
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Referrer-Policy", "no-referrer")
err := td.templ.ExecuteTemplate(w, "home", td.TemplateData)
if err != nil {
log.Printf("Failed to Execute: %v", err)
return
}
// TODO: Use TemplateExecToString only when the template data is updated
// (i.e. block notification).
}
// WebUI represents the html web interface. It includes the template related
// data, methods for parsing the templates, and the http.HandlerFuncs registered
// with URL paths by the http router.
type WebUI struct {
TemplateData *templateFields
templ *template.Template
}
// NewWebUI is the constructor for WebUI. It creates a html/template.Template,
// loads the function map, and parses the template files.
func NewWebUI() (*WebUI, error) {
tmpl, err := parseTemplates()
if err != nil {
return nil, err
}
return &WebUI{
templ: tmpl,
}, nil
}
func parseTemplates() (*template.Template, error) {
fp := filepath.Join("public", "views", "*.html")
tmpl, err := template.New("home").Funcs(funcMap).ParseGlob(fp)
if err != nil {
return nil, err
}
return tmpl, nil
}
// See reloadsig*.go for an exported method
func (td *WebUI) reloadTemplatesSig(sig os.Signal) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, sig)
go func() {
for {
sigr := <-sigChan
log.Printf("Received %s", sig)
if sigr == sig {
tmpl, err := parseTemplates()
if err != nil {
log.Println(err)
continue
}
td.templ = tmpl
log.Println("Web UI html templates reparsed.")
}
}
}()
}
const (
secondsPerMinute = 60
secondsPerHour = 60 * secondsPerMinute
secondsPerDay = 24 * secondsPerHour
hourCutoffSecs = 72 * secondsPerHour
minuteCutoffSecs = 2 * secondsPerHour
)
// ceilDiv returns the ceiling of the result of dividing the given value.
func ceilDiv(numerator, denominator int64) int {
return int(math.Ceil(float64(numerator) / float64(denominator)))
}
// blocksToTimeEstimate returns a human-readable estimate for the amount of time
// a given number of blocks would take.
func blocksToTimeEstimate(startHeight int64, currentHeight int64) string {
blocksRemaining := startHeight - currentHeight
remainingSecs := blocksRemaining * int64(activeNetParams.TargetTimePerBlock.Seconds())
if remainingSecs > hourCutoffSecs {
value := ceilDiv(remainingSecs, secondsPerDay)
return english.Plural(value, "day", "")
} else if remainingSecs > minuteCutoffSecs {
value := ceilDiv(remainingSecs, secondsPerHour)
return english.Plural(value, "hour", "")
}
value := ceilDiv(remainingSecs, secondsPerMinute)
return english.Plural(value, "minute", "")
}