/
rootHandler.go
121 lines (95 loc) · 2.82 KB
/
rootHandler.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
package main
import (
"fmt"
"net/http"
"strconv"
)
func listHandler(w http.ResponseWriter, r *http.Request) {
pageSize, psErr := strconv.Atoi(r.URL.Query().Get("pageSize"))
if psErr != nil || pageSize < 1 {
pageSize = 2500
}
count, _ := countDomains()
maxPages := int(count / int64(pageSize))
if count%int64(pageSize) > 0 {
maxPages++
}
currentPage, cpErr := strconv.Atoi(r.URL.Query().Get("page"))
if cpErr != nil || currentPage < 1 || currentPage > maxPages {
currentPage = 1
}
domains, dbErr := listDomains(int32(pageSize), int32((currentPage-1)*pageSize))
runTemplate(w, r, "bimi/list.tmpl", map[string]any{
"Count": count,
"CurrentPage": currentPage,
"Domains": domains,
"Err": dbErr,
"MaxPage": maxPages,
"PageSize": pageSize,
"Title": "List Domains",
})
}
func listInvalidHandler(w http.ResponseWriter, r *http.Request) {
pageSize, psErr := strconv.Atoi(r.URL.Query().Get("pageSize"))
if psErr != nil || pageSize < 1 {
pageSize = 100
}
count, _ := countInvalidDomains()
maxPages := int(count / int64(pageSize))
if count%int64(pageSize) > 0 {
maxPages++
}
currentPage, cpErr := strconv.Atoi(r.URL.Query().Get("page"))
if cpErr != nil || currentPage < 1 || currentPage > maxPages {
currentPage = 1
}
domains, dbErr := listInvalidDomains(int32(pageSize), int32((currentPage-1)*pageSize))
runTemplate(w, r, "bimi/invalid.tmpl", map[string]any{
"Count": count,
"CurrentPage": currentPage,
"Domains": domains,
"Err": dbErr,
"MaxPage": maxPages,
"PageSize": pageSize,
"Title": "Invalid BIMI Logos",
})
}
func rootHandlerGet(w http.ResponseWriter, r *http.Request) {
runTemplate(w, r, "bimi/index.tmpl", map[string]any{
"H1": "Welcome",
"Title": "BIMI Explorer",
})
}
func rootHandlerPost(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
domain := r.Form.Get("domain")
if domain == "" {
http.Redirect(w, r, "/bimi/?err=You+must+enter+a+domain", http.StatusSeeOther)
return
}
http.Redirect(w, r, fmt.Sprintf("/bimi/%s/", domain), http.StatusSeeOther)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
pageSize, psErr := strconv.Atoi(r.URL.Query().Get("pageSize"))
if psErr != nil || pageSize < 1 {
pageSize = 144
}
count, _ := countDomains()
maxPages := int(count / int64(pageSize))
if count%int64(pageSize) > 0 {
maxPages++
}
currentPage, cpErr := strconv.Atoi(r.URL.Query().Get("page"))
if cpErr != nil || currentPage < 1 || currentPage > maxPages {
currentPage = 1
}
domains, dbErr := listDomains(int32(pageSize), int32((currentPage-1)*pageSize))
runTemplate(w, r, "bimi/view.tmpl", map[string]any{
"CurrentPage": currentPage,
"Domains": domains,
"Err": dbErr,
"MaxPage": maxPages,
"PageSize": pageSize,
"Title": "View BIMI Logos",
})
}