/
pagedata.go
181 lines (152 loc) · 4.62 KB
/
pagedata.go
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package web
import (
"bytes"
"errors"
"fmt"
"net/http"
"strings"
"syscall"
"time"
"github.com/ethpandaops/assertoor/pkg/coordinator/buildinfo"
"github.com/sirupsen/logrus"
)
var LayoutTemplateFiles = []string{
"_layout/layout.html",
"_layout/header.html",
"_layout/footer.html",
}
type PageData struct {
Active string
Meta *Meta
ShowSidebar bool
SidebarData interface{}
Data interface{}
Version string
Year int
Title string
Lang string
Debug bool
DebugTemplates []string
}
type Meta struct {
Title string
Description string
Domain string
Path string
Tlabel1 string
Tdata1 string
Tlabel2 string
Tdata2 string
Templates string
}
type ErrorPageData struct {
CallTime time.Time
CallURL string
ErrorMsg string
StackTrace string
Version string
}
func InitPageData(r *http.Request, active, path, title string, mainTemplates []string) *PageData {
fullTitle := fmt.Sprintf("%v - %v - %v", title, frontendConfig.SiteName, time.Now().Year())
if title == "" {
fullTitle = fmt.Sprintf("%v - %v", frontendConfig.SiteName, time.Now().Year())
}
host := ""
if r != nil {
host = r.Host
}
data := &PageData{
Meta: &Meta{
Title: fullTitle,
Description: "assertoor: testnet testing tool",
Domain: host,
Path: path,
Templates: strings.Join(mainTemplates, ","),
},
Active: active,
Data: &struct{}{},
Version: buildinfo.GetVersion(),
Year: time.Now().UTC().Year(),
Title: frontendConfig.SiteName,
Lang: "en-US",
Debug: frontendConfig.Debug,
}
if r != nil {
acceptedLangs := strings.Split(r.Header.Get("Accept-Language"), ",")
if len(acceptedLangs) > 0 {
if strings.Contains(acceptedLangs[0], "ru") || strings.Contains(acceptedLangs[0], "RU") {
data.Lang = "ru-RU"
}
}
for _, v := range r.Cookies() {
if v.Name == "language" {
data.Lang = v.Value
break
}
}
}
return data
}
// used to handle errors constructed by Template.ExecuteTemplate correctly
func HandleTemplateError(w http.ResponseWriter, r *http.Request, fileIdentifier, functionIdentifier, infoIdentifier string, err error) error {
// ignore network related errors
if err != nil && !errors.Is(err, syscall.EPIPE) && !errors.Is(err, syscall.ETIMEDOUT) {
logger.WithFields(logrus.Fields{
"file": fileIdentifier,
"function": functionIdentifier,
"info": infoIdentifier,
"error type": fmt.Sprintf("%T", err),
"route": r.URL.String(),
}).WithError(err).Error("error executing template")
//nolint:gocritic // ignore
http.Error(w, "Internal server error", http.StatusServiceUnavailable)
}
return err
}
func HandlePageError(w http.ResponseWriter, r *http.Request, pageError error) {
templateFiles := LayoutTemplateFiles
templateFiles = append(templateFiles, "_layout/500.html")
notFoundTemplate := GetTemplate(templateFiles...)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusInternalServerError)
data := InitPageData(r, "blockchain", r.URL.Path, "Internal Error", templateFiles)
errData := &ErrorPageData{
CallTime: time.Now(),
CallURL: r.URL.String(),
ErrorMsg: pageError.Error(),
Version: buildinfo.GetVersion(),
}
data.Data = errData
err := notFoundTemplate.ExecuteTemplate(w, "layout", data)
if err != nil {
logrus.Errorf("error executing page error template for %v route: %v", r.URL.String(), err)
//nolint:gocritic // ignore
http.Error(w, "Internal server error", http.StatusServiceUnavailable)
}
}
func HandleNotFound(w http.ResponseWriter, r *http.Request) {
templateFiles := LayoutTemplateFiles
templateFiles = append(templateFiles, "_layout/404.html")
notFoundTemplate := GetTemplate(templateFiles...)
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusNotFound)
data := InitPageData(r, "blockchain", r.URL.Path, "Not Found", templateFiles)
err := notFoundTemplate.ExecuteTemplate(w, "layout", data)
if err != nil {
logrus.Errorf("error executing not-found template for %v route: %v", r.URL.String(), err)
//nolint:gocritic // ignore
http.Error(w, "Internal server error", http.StatusServiceUnavailable)
}
}
func BuildPageHeader() (string, error) {
templateFiles := LayoutTemplateFiles
templateFiles = append(templateFiles, "_layout/blank.html")
blankTemplate := GetTemplate(templateFiles...)
data := InitPageData(nil, "blank", "", "", templateFiles)
var outBuf bytes.Buffer
err := blankTemplate.ExecuteTemplate(&outBuf, "header", data)
if err != nil {
return "", fmt.Errorf("error executing blank template: %v", err)
}
return outBuf.String(), nil
}