-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (121 loc) · 3.22 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
package main
import (
"bytes"
"context"
"html/template"
"log"
"net/http"
"os"
"strings"
"time"
ics "github.com/arran4/golang-ical"
"github.com/joho/godotenv"
"golang.org/x/exp/slices"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
var uploader *s3manager.Uploader
type IndexCal struct {
Name string
Events int
Updated time.Time
URL string
}
type TemplateData struct {
IndexCals []IndexCal
}
func GetCourseNames(cal *ics.Calendar) []string {
var names []string
for _, v := range cal.Events() {
loc := v.GetProperty(ics.ComponentPropertyLocation)
if !slices.Contains(names, loc.Value) {
names = append(names, loc.Value)
}
}
return names
}
func GetUploader() *s3manager.Uploader {
s3Config := &aws.Config{
Region: aws.String(os.Getenv("AWS_REGION")),
Credentials: credentials.NewStaticCredentials(
os.Getenv("AWS_ACCESS_KEY_ID"),
os.Getenv("AWS_SECRET_ACCESS_KEY"),
os.Getenv("AWS_SESSION_TOKEN")),
}
session, err := session.NewSession(s3Config)
if err != nil {
log.Fatal("error creating s3 session", err)
}
return s3manager.NewUploader(session)
}
func upload(key string, data string) {
upInput := &s3manager.UploadInput{
Bucket: aws.String(os.Getenv("BUCKET_NAME")),
Key: aws.String(key),
Body: bytes.NewReader([]byte(data)),
ACL: aws.String("public-read"),
}
if strings.Contains(key, "html") {
upInput.ContentType = aws.String("html")
}
res, err := uploader.UploadWithContext(context.Background(), upInput)
if err != nil {
log.Fatal("error uploading file to s3 bucket", err)
}
log.Println("uploaded file successfully", res.Location)
}
func handleRequest() (string, error) {
err := godotenv.Load("env")
if err != nil {
log.Fatal("couldn't load .env", err)
}
resp, err := http.Get(os.Getenv("ICS_URL"))
if err != nil {
log.Fatal("couldn't make ics get request", err)
}
cal, err := ics.ParseCalendar(resp.Body)
if err != nil {
log.Fatal("couldn't parse response", err)
}
uploader = GetUploader()
courses := GetCourseNames(cal)
log.Println("found unique course calendars: ", len(courses))
data := TemplateData{}
for _, course := range courses {
newcal := ics.NewCalendar()
for _, v := range cal.Events() {
loc := v.GetProperty(ics.ComponentPropertyLocation)
if loc.Value == course {
start, _ := v.GetStartAt()
if start.Minute() == 59 {
v.SetStartAt(start.Truncate(time.Hour))
v.SetDuration(time.Minute * 59)
}
newcal.AddVEvent(v)
}
}
log.Println(course)
newcal.SetXWRCalName(course)
newcal.SetName(course)
newcal.SetProductId("d2l-ics-proxy")
d := IndexCal{
Name: course,
Events: len(newcal.Events()),
Updated: time.Now(),
URL: os.Getenv("PROXY_URL") + "cal-" + course + ".ics",
}
data.IndexCals = append(data.IndexCals, d)
upload("cal-"+course+".ics", newcal.Serialize())
}
tmpl := template.Must(template.ParseFiles("index.html"))
buffer := new(bytes.Buffer)
tmpl.Execute(buffer, data)
upload("index.html", buffer.String())
return "sucessfully uploaded ics files to s3", nil
}
func main() {
lambda.Start(handleRequest)
}