-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (100 loc) · 3.75 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
package main
import (
"bytes"
"context"
_ "embed"
"fmt"
"html/template"
"os"
"slices"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/rs/zerolog/log"
"github.com/lthummus/seattle-sports-today/events"
"github.com/lthummus/seattle-sports-today/notifier"
"github.com/lthummus/seattle-sports-today/uploader"
)
//go:embed index.gohtml
var templateString string
var pageTemplate *template.Template
type templateParams struct {
Events []*events.Event
GeneratedDate string
FullGeneratedDate template.HTML
}
func init() {
var err error
pageTemplate, err = template.New("").Parse(templateString)
if err != nil {
log.Fatal().Err(err).Msg("could not parse template")
}
}
func renderPage(foundEvents []*events.Event) ([]byte, error) {
generatedDateString := events.SeattleCurrentTime.Format("Monday Jan _2, 2006")
log.Info().Int("num_events", len(foundEvents)).Str("formatted_date", generatedDateString).Msg("rendering page")
buf := bytes.NewBuffer(nil)
// we have to do things this way because by default the Go HTML templating system will strip out comments. We can force it not
// to do that by passing this as a template.HTML already so the templating system will plonk it in there no questions asked.
//#nosec G203 -- This is generated by us with no involvement from the end user
generatedTimestamp := template.HTML(fmt.Sprintf("<!-- Generated at: %s -->", events.SeattleCurrentTime.Format(time.RFC1123)))
err := pageTemplate.Execute(buf, &templateParams{Events: foundEvents, GeneratedDate: generatedDateString, FullGeneratedDate: generatedTimestamp})
if err != nil {
return nil, fmt.Errorf("renderPage: could not render: %w", err)
}
return buf.Bytes(), nil
}
func eventHandler(ctx context.Context) error {
defer func() {
if err := recover(); err != nil {
_ = notifier.Notify(context.Background(), fmt.Sprintf("ERROR: uncaught panic: %v", err), notifier.PriorityHigh, notifier.EmojiSiren)
}
}()
log.Info().Msg("getting today's games")
foundEvents, err := events.GetTodaysGames(ctx)
if err != nil {
_ = notifier.Notify(ctx, fmt.Sprintf("ERROR: could not get today's games: %s", err.Error()), notifier.PriorityHigh, notifier.EmojiSiren)
return err
}
log.Info().Int("games_found", len(foundEvents)).Msg("found games")
slices.SortFunc(foundEvents, func(a, b *events.Event) int {
return int(a.RawTime - b.RawTime)
})
for _, curr := range foundEvents {
log.Info().Str("team_name", curr.TeamName).Str("venue", curr.Venue).Str("local_time", curr.LocalTime).Str("opponent", curr.Opponent).Int64("raw_time", curr.RawTime).Msg("found event")
}
log.Info().Msg("rendering page")
page, err := renderPage(foundEvents)
if err != nil {
_ = notifier.Notify(ctx, fmt.Sprintf("ERROR: could not render page: %s", err.Error()), notifier.PriorityHigh, notifier.EmojiSiren)
return err
}
log.Info().Msg("render complete")
if os.Getenv("_HANDLER") != "" || os.Getenv("UPLOAD_ANYWAY") == "true" {
log.Info().Msg("beginning upload")
err = uploader.Upload(ctx, page)
if err != nil {
_ = notifier.Notify(ctx, fmt.Sprintf("ERROR: upload page: %s", err.Error()), notifier.PriorityHigh, notifier.EmojiSiren)
return err
}
log.Info().Msg("upload complete")
} else {
log.Warn().Msg("detected running locally, not uploading")
}
log.Info().Msg("all in a day's work...")
err = notifier.Notify(ctx, fmt.Sprintf("Everything worked! Found %d game(s)", len(foundEvents)), notifier.PriorityDefault, notifier.EmojiParty)
if err != nil {
log.Warn().Err(err).Msg("error sending notification")
}
return nil
}
func main() {
log.Info().Msg("hello world")
if os.Getenv("_HANDLER") != "" {
lambda.Start(eventHandler)
} else {
err := eventHandler(context.Background())
if err != nil {
log.Error().Err(err).Msg("error running handler")
}
}
}