-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
220 lines (188 loc) · 4.58 KB
/
templates.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"html/template"
"io"
"io/ioutil"
"strings"
"github.com/gin-gonic/gin"
)
const ITEMS_ON_PAGE = 100
//
// Types used in templates.
//
type IndexTmpl struct {
TotalArchived int
Latest []ArchiveLinkTmpl // urls
}
type SubThreadsTmpl struct {
page int
Threads []ArchiveLinkTmpl // urls
}
type ArchiveLinkTmpl struct {
ArchiveTime int
ThreadId string
ThreadTitle string
Subreddit string
}
type SubsListTmpl struct {
page int
Subs []string
}
type ArchiveTmpl struct {
ArchiveTime string
ThreadId string
ThreadTitle string
ReplyId string
Subreddit string
ThreadHTML template.HTML
}
type RedirectTmpl struct {
Route string
}
type ThreadTmpl struct {
ThreadTitle string
ThreadContent template.HTML
ThreadContentLink string
Subreddit string
Replies []*CommentTmpl
Author string
Time string
}
type CommentTmpl struct {
CommentId string
ThreadId string
CommentContent template.HTML
Children []*CommentTmpl
Author string
Time string
Continues bool
Score string
}
type TemplateError struct {
}
func (err *TemplateError) Error() string {
return "Templating error."
}
func LoadTemplates() {
var allFiles []string
files, err := ioutil.ReadDir("./templates")
if err != nil {
Log("Error loading templates", err.Error()).Fatal()
}
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".tmpl") {
allFiles = append(allFiles, "./templates/"+filename)
}
}
templates, err = template.ParseFiles(allFiles...)
if err != nil {
Log("Error loading templates", err.Error()).Fatal()
}
}
func RenderErrorPage(errStatus int, w gin.ResponseWriter) {
tp := templates.Lookup("error.tmpl").Lookup("internal")
w.WriteHeader(errStatus)
switch errStatus {
case 400:
tp = templates.Lookup("error.tmpl").Lookup("invalidreq")
break
case 404:
tp = templates.Lookup("error.tmpl").Lookup("notfound")
break
case 500:
tp = templates.Lookup("error.tmpl").Lookup("internal")
break
default:
tp = templates.Lookup("error.tmpl").Lookup("other")
tp.Execute(w, struct{ Code int }{errStatus})
return
}
tp.Execute(w, nil)
}
func RenderRedirectPage(route string, w io.Writer) {
tp := templates.Lookup("redirect.tmpl").Lookup("page")
tp.Execute(w, RedirectTmpl{
Route: route,
})
}
func RenderAlreadyExists(route string, w gin.ResponseWriter) {
w.WriteHeader(409)
tp := templates.Lookup("redirect.tmpl").Lookup("conflict")
tp.Execute(w, RedirectTmpl{
Route: route,
})
}
func RenderSubsList(page int, w gin.ResponseWriter) int {
if err, sTmpl := querySubsList(page, ITEMS_ON_PAGE); err != nil {
return 500
} else {
tp := templates.Lookup("index.tmpl").Lookup("sublist")
tp.Execute(w, sTmpl)
}
return 200
}
func RenderSubThreads(page int, w gin.ResponseWriter, sub string) int {
if err, sTmpl := querySubArchives(page, ITEMS_ON_PAGE, sub); err != nil {
return 500
} else {
tp := templates.Lookup("index.tmpl").Lookup("subthreads")
tp.Execute(w, &SubThreadsTmpl{page, sTmpl})
}
return 200
}
func RenderIndexPage(w io.Writer) int {
count := 0
rows, qErr := dbReadOnly.Query(`SELECT COUNT(DISTINCT thread_id) from threads`)
defer rows.Close()
if qErr != nil {
Log("Error with thread count query", qErr.Error()).Error()
return 500
}
rows.Next()
rows.Scan(&count)
err, latestCreated := queryLatestArchives(10)
if err != nil {
return 500
}
t := templates.Lookup("index.tmpl").Lookup("index")
t.Execute(w, IndexTmpl{count, latestCreated})
return 200
}
func RenderAboutPage(w io.Writer) int {
count := 0
rows, qErr := dbReadOnly.Query(`SELECT COUNT(DISTINCT thread_id) from threads`)
defer rows.Close()
if qErr != nil {
Log("Error with thread count query", qErr.Error()).Error()
return 500
}
rows.Next()
rows.Scan(&count)
err, latestCreated := queryLatestArchives(10)
if err != nil {
return 500
}
t := templates.Lookup("index.tmpl").Lookup("about")
t.Execute(w, IndexTmpl{count, latestCreated})
return 200
}
func RenderThreadPage(fileId string, w gin.ResponseWriter) int {
fnameParts := strings.Split(fileId, "-")
threadId := fnameParts[0]
continuingReply := ""
// If thread is part of a longer comment thread the comment's ID will be the second element.
if len(fnameParts) > 1 {
continuingReply = fnameParts[1]
}
if arch, err := GetArchiveQuery(threadId, continuingReply); err != nil {
Log("Error getting archive.", err.Error())
return 500
} else if arch == nil {
return 404
} else {
t := templates.Lookup("thread.tmpl").Lookup("archive")
t.Execute(w, arch)
return 200
}
}