/
main.go
505 lines (443 loc) · 14.9 KB
/
main.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"math/rand"
"net"
"net/http"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/facebookgo/flagenv"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
// ViewTemplate displays /view
var ViewTemplate *template.Template
// IndexTemplate displays the index/root
var IndexTemplate *template.Template
// StartDate is the earliest date we will query in the db
var StartDate time.Time
// EndDate is the latest date we will query in the db
var EndDate time.Time
// Offices is all the valid Offices
var Offices []string
// AppName is the app name
var AppName = "countmyreps"
// Version is the semver
var Version = "3.1.3"
func init() {
var err error
rand.Seed(time.Now().UnixNano())
// funcMap contains the functions available to the view template
funcMap := template.FuncMap{
// totals sums all the exercises in []RepData
"totals": func(d []RepData) int {
return totalReps(d)
},
// allow easy converting of strings to JS string (turns freqData{{ OfficeName}}: freqData"OC" -> freqDataOC in JS)
"js": func(s string) template.JS {
return template.JS(s)
},
// d3ChartData correctly formats []RepData to the JS format so data can display
"d3ChartData": func(d []RepData) template.JS {
parts := make([]string, len(d))
for i, data := range d {
parts[i] = fmt.Sprintf("{State:'%s',freq:{pull_up:%d, sit_up:%d, push_up: %d, squat:%d}}",
data.Date,
data.ExerciseCounts[PullUps],
data.ExerciseCounts[SitUps],
data.ExerciseCounts[PushUps],
data.ExerciseCounts[Squats],
)
}
return template.JS(strings.Join(parts, ",\n"))
},
// d3ChartDataForOffice is a helper method to avoid complexities with nesting ranges in the template
"d3ChartDataForOffice": func(officeName string, reps map[string][]RepData) template.JS {
// TODO: DRY up with ^^
parts := make([]string, len(reps[officeName]))
for i, data := range reps[officeName] {
parts[i] = fmt.Sprintf("{State:'%s',freq:{pull_up:%d, sit_up:%d, push_up: %d, squat:%d}}",
data.Date,
data.ExerciseCounts[PullUps],
data.ExerciseCounts[SitUps],
data.ExerciseCounts[PushUps],
data.ExerciseCounts[Squats],
)
}
return template.JS(strings.Join(parts, ",\n"))
},
}
// parse tempaltes in init so we don't have to parse them on each request
// pro: single parsing
// con: have to restart the process to load file changes
ViewTemplate, err = template.New("view.html").Funcs(funcMap).ParseFiles(filepath.Join("go_templates", "view.html"))
if err != nil {
log.Fatalln(err)
}
IndexTemplate, err = template.New("index.html").Funcs(funcMap).ParseFiles(filepath.Join("go_templates", "index.html"))
if err != nil {
log.Fatalln(err)
}
}
// We expect the database to have these exact values
// TODO: it would be interesting to figure out how to have this dynamic (display too) and based off the email they send to
const (
PullUps = "Pull Ups"
SitUps = "Sit Ups"
PushUps = "Push Ups"
Squats = "Squats"
OldEmail = "pullups-pushups-airsquats-situps@countmyreps.com"
NewEmail = "pullups-pushups-squats-situps@countmyreps.com"
)
// Debug turns on more verbose logging
var Debug bool
// EmailSender allows us to swap out how we send the email (specifically, SendGrid vs Fake/Test)
var EmailSender Emailer
func main() {
var err error
// flag vars
var port int
var mysqlHost, mysqlPort, mysqlUser, mysqlPass, mysqlDBname string
var start, end string
// defaults for start and end vars
startDefault := fmt.Sprintf("%d-11-01", time.Now().Year())
endDefault := fmt.Sprintf("%d-11-31", time.Now().Year())
// get flags
flag.IntVar(&port, "port", 9126, "port to run site")
flag.StringVar(&start, "start-date", startDefault, "the start date to when querying the db")
flag.StringVar(&end, "end-date", endDefault, "the end date to when querying the db")
flag.StringVar(&mysqlHost, "mysql-host", "localhost", "mysql host")
flag.StringVar(&mysqlPort, "mysql-port", "3306", "mysql port")
flag.StringVar(&mysqlUser, "mysql-user", "root", "mysql root")
flag.StringVar(&mysqlPass, "mysql-pass", "", "mysql pass")
flag.StringVar(&mysqlDBname, "mysql-dbname", "countmyreps", "mysql dbname")
flag.BoolVar(&Debug, "debug", false, "set flag for verbose logging")
flagenv.Parse()
flag.Parse()
// validate flags
StartDate, err = time.Parse("2006-01-02", start)
if err != nil {
log.Fatal(err)
}
EndDate, err = time.Parse("2006-01-02", end)
if err != nil {
log.Fatal(err)
}
db := SetupDB(mysqlUser, mysqlPass, mysqlHost, mysqlPort, mysqlDBname)
log.Printf("starting on :%d", port)
s := NewServer(db, port, SendGridEmailer{})
if err := s.Serve(); err != nil {
log.Println("Unexpected error serving: ", err.Error())
}
}
// SetupDB initialized the DB conn and grabs initial data needed for the app (ie, Offices)
func SetupDB(mysqlUser, mysqlPass, mysqlHost, mysqlPort, mysqlDBname string) *sql.DB {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", mysqlUser, mysqlPass, mysqlHost, mysqlPort, mysqlDBname))
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
db.SetConnMaxLifetime(1 * time.Minute)
err = populateOfficesVar(db)
if err != nil {
log.Fatal(err)
}
return db
}
// Server contains the settings needed to run the server
type Server struct {
Port int
Mux *mux.Router
DB *sql.DB
dbname string
close chan struct{}
}
// NewServer creates a new server running against the give db
func NewServer(db *sql.DB, port int, emailer Emailer) *Server {
s := &Server{}
s.Port = port
s.DB = db
s.close = make(chan struct{})
EmailSender = emailer // TODO: should this be on the server? How will that pass down?
r := mux.NewRouter()
r.HandleFunc("/", s.IndexHandler)
r.HandleFunc("/view", s.ViewHandler)
r.HandleFunc("/json", s.JSONHandler)
r.HandleFunc("/healthcheck", s.HealthcheckHandler)
r.HandleFunc("/parseapi/index.php", s.ParseHandler) // backwards compatibility
r.PathPrefix("/").Handler(http.StripPrefix("", http.FileServer(http.Dir("web/")))) // mux specific workaround for fileserver; todo: use separate mux to avoid filtering these endpoints from logs?
r.Handle("/", mwPanic(mwLog(r)))
s.Mux = r
return s
}
// Serve blocks and starts a server
func (s *Server) Serve() error {
errc := make(chan error)
if s.Port == 0 {
l, err := net.Listen("tcp", ":0")
if err != nil {
errc <- err
}
s.Port = l.Addr().(*net.TCPAddr).Port
errc <- http.Serve(l, s.Mux)
} else {
errc <- http.ListenAndServe(fmt.Sprintf(":%d", s.Port), s.Mux)
}
for {
select {
case <-s.close:
return nil
case err := <-errc:
return err
}
}
}
// Close terminates the server
func (s *Server) Close() error {
close(s.close)
return nil
}
// errorHandler is a helper method to log and display errors. When invoked from a parent handler, the parent should then return
func errorHandler(w http.ResponseWriter, r *http.Request, code int, message string, err error) {
logError(r, err, message)
w.WriteHeader(code)
w.Write([]byte(fmt.Sprintf("%v - %s", http.StatusText(code), message)))
}
// ParseHandler handles SendGrid's inbound parse api
func (s *Server) ParseHandler(w http.ResponseWriter, r *http.Request) {
// NOTE: SendGrid's Inbound Parse API requires a 200 level response always, even on error, otherwise it will retry
// errMsg is parsed later to determine if we should send a success or error email
var errMsg string
var err error
to := r.PostFormValue("to")
from := r.PostFormValue("from")
subject := r.PostFormValue("subject")
logEvent(r, "parseapi", fmt.Sprintf("To: %s, From: %s, Subject: %s", to, from, subject))
defer func() {
var mailType string
if errMsg != "" {
mailType = "error - " + errMsg
// we don't want to send out a bunch of responses to spam hitting the server
// only send a response if the subject looked vaguely correct or the sender was from sendgrid.
parts := strings.Split(subject, ",")
if strings.Contains(from, "@sendgrid.com") || len(parts) == 4 {
err = s.SendErrorEmail(from, to, subject, errMsg)
}
} else {
mailType = "success"
err = s.SendSuccessEmail(from)
}
if err != nil {
logError(r, err, "unable to send response email: "+mailType)
}
}()
if to == "" || from == "" || subject == "" {
logEvent(r, "bad_parse", "unable to determine to or from or subject")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, fmt.Sprintf("Missing to, from, or subject: %q, %q, %q", to, from, subject))
return
}
from = extractEmailAddr(from)
if !strings.Contains(from, "@sendgrid.com") {
logEvent(r, "bad_parse", fmt.Sprintf("sender not from sendgrid - %s", from))
errMsg = fmt.Sprintf(ErrToAddrFmt, from)
return
}
if !(extractEmailAddr(to) == NewEmail || extractEmailAddr(to) == OldEmail) {
logEvent(r, "bad_parse", fmt.Sprintf("recipient not valid countmyreps address: %s", to))
errMsg = fmt.Sprintf(ErrToAddrFmt, to)
return
}
userID, err := getOrCreateUserID(s.DB, from)
if err != nil {
logError(r, err, "unable to create/get user")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to create and/or get user")
return
}
reps := strings.Split(subject, ",")
if len(reps) == 4 {
for i, rep := range reps {
count, err := strconv.Atoi(strings.TrimSpace(rep))
if err != nil {
logError(r, err, fmt.Sprintf("unable to convert %s to int", rep))
errMsg = fmt.Sprintf(ErrSubjectFmt, subject)
return
}
// protect against tricky people who spoof negative reps to other folks
if count < 0 {
count = -1 * count
}
var exercise string
switch i {
case 0:
exercise = PullUps
case 1:
exercise = PushUps
case 2:
exercise = Squats
case 3:
exercise = SitUps
default:
exercise = "unknown"
}
// TODO: move out of loop and use VALUES (), (), (), () and move to db.go
_, err = s.DB.Exec("INSERT INTO reps (exercise, count, user_id) VALUES (?, ?, ?)", exercise, count, userID)
if err != nil {
logError(r, err, "unable to insert rep")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to insert into the database")
return
}
}
} else if inListCaseInsenitive(subject, Offices) {
// TODO: remove offices; just use teams
office := formattedOffice(subject)
// todo: move to db.go
_, err = s.DB.Exec("UPDATE user SET office=(SELECT id FROM office where name=?) WHERE id=? LIMIT 1", office, userID)
if err != nil {
logError(r, err, "unable to update user's office")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to update office relationship in the database")
return
}
} else if strings.Contains(strings.ToLower(subject), "team add:") {
parts := strings.Split(subject, ":")
if len(parts) < 2 {
logError(r, err, "enexpected error splitting subject for team add")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to add to user teams")
return
}
err = addTeam(s.DB, sanitizeTeamName(parts[1]), userID)
if err != nil {
logError(r, err, "unable to add to user teams")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to add to user teams")
return
}
} else if strings.Contains(strings.ToLower(subject), "team remove:") {
parts := strings.Split(subject, ":")
if len(parts) < 2 {
logError(r, err, "enexpected error splitting subject for team remove")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to add to user teams")
return
}
err = removeTeam(s.DB, sanitizeTeamName(parts[1]), userID)
if err != nil {
logError(r, err, "unable to remove from user teams")
errMsg = fmt.Sprintf(ErrUnexpectedFmt, "unable to remove from user teams")
return
}
} else {
logEvent(r, "bad_parse", fmt.Sprintf("bad subject: %s", subject))
errMsg = fmt.Sprintf(ErrSubjectFmt, subject)
return
}
}
func sanitizeTeamName(teamName string) string {
var rns []rune
for _, r := range teamName {
if strings.ContainsAny(string(r), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_") {
rns = append(rns, r)
}
}
return string(rns)
}
// ViewData is the data needed to populate the view.html template
type ViewData struct {
UserEmail string
UserOffice string
UserTeams []string
TodaysReps []RepData
UserReps []RepData
TeamReps map[string][]RepData
TeamStats map[string]Stats
}
// RepData is a single entry (or aggregate for a day)
type RepData struct {
Date string
ExerciseCounts map[string]int
}
// Stats hold per-office stats
type Stats struct {
RepsPerPerson int
RepsPerPersonParticipating int
RepsPerPersonPerDay int
RepsPerPersonParticipatingPerDay int
PercentParticipating int
TotalReps int
HeadCount int
}
// ViewHandler handles /view (all the graphs, data, etc)
func (s *Server) ViewHandler(w http.ResponseWriter, r *http.Request) {
email := r.URL.Query().Get("email")
if email == "" {
errorHandler(w, r, http.StatusBadRequest, "you must provide an email query parameter", nil)
return
}
data := s.getViewData(email)
err := ViewTemplate.Execute(w, data)
if err != nil {
errorHandler(w, r, http.StatusInternalServerError, fmt.Sprintf("unable to execute %s template", "view.html"), err)
return
}
}
// JSONHandler displays the JSON payload needed to build a client based js page
func (s *Server) JSONHandler(w http.ResponseWriter, r *http.Request) {
email := r.URL.Query().Get("email")
if email == "" {
errorHandler(w, r, http.StatusBadRequest, "you must provide an email query parameter", nil)
return
}
data := s.getViewData(email)
w.Header().Set("content-type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
errorHandler(w, r, http.StatusInternalServerError, "unable to encode json", err)
}
}
func (s *Server) getViewData(email string) ViewData {
// TODO: clean up this hack by migrating user offices to user be one of their teams
officeAndTeamReps := getTeamReps(s.DB)
for k, v := range getOfficeReps(s.DB) {
officeAndTeamReps[k] = v
}
officeAndTeamStats := getTeamStats(s.DB)
for k, v := range getOfficeStats(s.DB) {
officeAndTeamStats[k] = v
}
data := ViewData{
UserEmail: email,
TodaysReps: getTodaysReps(s.DB, email),
UserOffice: getUserOffice(s.DB, email),
UserTeams: getUserTeams(s.DB, email),
TeamReps: officeAndTeamReps,
TeamStats: officeAndTeamStats,
UserReps: getUserReps(s.DB, email),
}
return data
}
// HeathcheckHandler verifies dependencies and reports if they are not in a good state
func (s *Server) HealthcheckHandler(w http.ResponseWriter, r *http.Request) {
_, err := s.DB.Exec("SELECT 1")
if err != nil {
logError(r, err, "healthcheck failed to query db")
w.Write([]byte("database issues\n"))
w.WriteHeader(http.StatusServiceUnavailable)
} else {
w.Write([]byte("database ok\n"))
}
}
// IndexHandler handles the root/index
func (s *Server) IndexHandler(w http.ResponseWriter, r *http.Request) {
err := IndexTemplate.Execute(w, nil)
if err != nil {
errorHandler(w, r, http.StatusInternalServerError, fmt.Sprintf("unable to execute %s template", "index.html"), err)
return
}
}