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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
package main
import (
"net"
"net/http"
"text/template"
"time"
"git.darknebu.la/chaosdorf/freitagsfoo/src/db"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
// "git.darknebu.la/chaosdorf/freitagsfoo/src/db"
)
func initHTTPServer() {
// define the mux router and the routes
r := mux.NewRouter()
r.HandleFunc("/", indexHandler).Methods("GET")
r.HandleFunc("/upcoming", upcomingHandler).Methods("GET")
r.HandleFunc("/past", pastHandler).Methods("GET")
r.HandleFunc("/propose", proposeHandler).Methods("GET")
r.HandleFunc("/talk/{uuid}", talkHandler).Methods("GET")
// host static files from the ./hosted/static folder
fs := http.FileServer(http.Dir("./hosted/static"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
// host the uploaded slides
uploaded := http.FileServer(http.Dir(viper.GetString("uploadpath")))
r.PathPrefix("/uploads/").Handler(http.StripPrefix("/uploads/", uploaded))
// api
api := r.PathPrefix("/api").Subrouter()
api.HandleFunc("/propose", apiProposeHandler).Methods("POST")
api.HandleFunc("/fetch", apiFetchHandler).Methods("GET")
api.HandleFunc("/fetch/{key}", apiFetchSpecificHandler).Methods("GET")
// define the http server
host := viper.GetString("server.host")
port := viper.GetString("server.port")
logrus.Infof("HTTP server listening on %s:%s", host, port)
httpServer := http.Server{
Addr: net.JoinHostPort(host, port),
Handler: r,
}
// start the http server
logrus.Fatal(httpServer.ListenAndServe())
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
pgdb := db.Connect()
defer db.Disconnect(pgdb)
// fetch the next three talks
firstThreeTalks, err := db.UpcomingTalksLimited(pgdb)
if err != nil {
logrus.Warn(err)
return
}
// fetch the last three talks
lastThreeTalks, err := db.PastTalksLimited(pgdb)
if err != nil {
logrus.Warn(err)
return
}
upcomingCount, err := db.CountUpcomingTalks(pgdb)
if err != nil {
logrus.Warn(err)
return
}
pastCount, err := db.CountPastTalks(pgdb)
if err != nil {
logrus.Warn(err)
return
}
content := map[string]interface{}{
"upcomingTalks": firstThreeTalks,
"upcomingCount": upcomingCount,
"pastTalks": lastThreeTalks,
"pastCount": pastCount,
"All": false,
}
// define a template
t := template.New("")
t, err = t.ParseGlob("./hosted/tmpl/*.html")
if err != nil {
logrus.Warn(err)
return
}
// execute the template
err = t.ExecuteTemplate(w, "index", content)
if err != nil {
logrus.Warn(err)
}
}
func upcomingHandler(w http.ResponseWriter, r *http.Request) {
pgdb := db.Connect()
defer db.Disconnect(pgdb)
// fetch the next talks
upcomingTalks, err := db.UpcomingTalks(pgdb)
if err != nil {
logrus.Warn(err)
return
}
upcomingCount, err := db.CountUpcomingTalks(pgdb)
if err != nil {
logrus.Warn(err)
return
}
content := map[string]interface{}{
"upcomingTalks": upcomingTalks,
"upcomingCount": upcomingCount,
"All": true,
}
// define a template
t := template.New("")
t, err = t.ParseGlob("./hosted/tmpl/*.html")
if err != nil {
logrus.Warn(err)
return
}
// execute the template
err = t.ExecuteTemplate(w, "upcoming", content)
if err != nil {
logrus.Warn(err)
}
}
func pastHandler(w http.ResponseWriter, r *http.Request) {
pgdb := db.Connect()
defer db.Disconnect(pgdb)
// fetch the next talks
pastTalks, err := db.PastTalks(pgdb)
if err != nil {
logrus.Warn(err)
return
}
pastCount, err := db.CountPastTalks(pgdb)
if err != nil {
logrus.Warn(err)
return
}
content := map[string]interface{}{
"pastTalks": pastTalks,
"pastCount": pastCount,
"All": true,
}
// define a template
t := template.New("")
t, err = t.ParseGlob("./hosted/tmpl/*.html")
if err != nil {
logrus.Warn(err)
return
}
// execute the template
err = t.ExecuteTemplate(w, "past", content)
if err != nil {
logrus.Warn(err)
}
}
func proposeHandler(w http.ResponseWriter, r *http.Request) {
// find the date of the next friday
weekday := time.Now().Weekday()
date := time.Now()
for weekday != time.Friday {
date = date.Add(time.Duration(24) * time.Hour)
weekday = date.Weekday()
}
// format it for inserting into the date min paramter of <input>
layout := "2006-01-02"
nextFriday := date.Format(layout)
content := map[string]interface{}{
"nextFriday": nextFriday,
}
t := template.New("")
t, err := t.ParseGlob("./hosted/tmpl/*.html")
if err != nil {
logrus.Warn(err)
return
}
err = t.ExecuteTemplate(w, "propose", content)
if err != nil {
logrus.Warn(err)
}
}
func talkHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pgdb := db.Connect()
defer db.Disconnect(pgdb)
talk, err := db.TalkByUUID(pgdb, vars["uuid"])
if err != nil {
logrus.Warn(err)
return
}
content := map[string]interface{}{
"talk": talk,
}
t := template.New("")
t, err = t.ParseGlob("./hosted/tmpl/*.html")
if err != nil {
logrus.Warn(err)
return
}
err = t.ExecuteTemplate(w, "singleTalk", content)
if err != nil {
logrus.Warn(err)
}
}
|