-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
267 lines (233 loc) · 6.51 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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/bellinghamcodes/website/internal/instagram"
"github.com/bellinghamcodes/website/internal/meetup"
"github.com/codegangsta/cli"
blackfriday "gopkg.in/russross/blackfriday.v2"
)
type handleFunc func(w http.ResponseWriter, req *http.Request)
var version = "Unknown"
func main() {
app := cli.NewApp()
app.Version = version
app.Usage = "bellingam.codes website"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "port",
Value: 3000,
EnvVar: "PORT",
Usage: "tcp port to listen on",
},
cli.StringFlag{
Name: "host",
Value: "",
EnvVar: "HOST",
Usage: "ip address/host to listen on",
},
cli.StringFlag{
Name: "slack-team",
EnvVar: "SLACK_TEAM",
Usage: "slack team name, as found in the slack URL",
},
cli.StringFlag{
Name: "slack-token",
EnvVar: "SLACK_TOKEN",
Usage: "access token for your slack team",
},
cli.StringFlag{
Name: "mailchimp-token",
EnvVar: "MAILCHIMP_TOKEN",
Usage: "api token for your mailchimp account",
},
cli.StringFlag{
Name: "mailchimp-list",
EnvVar: "MAILCHIMP_LIST",
Usage: "id of the mailchimp list",
},
cli.StringFlag{
Name: "meetup",
EnvVar: "MEETUP_NAME",
Usage: "url name of the meetup.com group",
},
cli.IntFlag{
Name: "meetup-fetch-interval",
EnvVar: "MEETUP_FETCH_INTERVAL",
Usage: "fetch interval in minutes for meetup event information",
Value: 30,
},
cli.StringFlag{
Name: "organization-name",
Value: "bellingham.codes",
EnvVar: "ORGANIZATION_NAME",
Usage: "name of the organization",
},
cli.StringFlag{
Name: "twitter",
EnvVar: "TWITTER_USERNAME",
Usage: "twitter account to link to in footer",
},
cli.StringFlag{
Name: "instagram",
EnvVar: "INSTAGRAM_USERNAME",
Usage: "instagram account to link to in footer",
},
cli.IntFlag{
Name: "instagram-fetch-interval",
EnvVar: "INSTAGRAM_FETCH_INTERVAL",
Usage: "fetch interval in minutes for instagram photos",
Value: 30,
},
cli.IntFlag{
Name: "instagram-count",
EnvVar: "INSTAGRAM_COUNT",
Usage: "maximum number of photos to show from instagram",
Value: 9,
},
cli.StringFlag{
Name: "facebook",
EnvVar: "FACEBOOK_PAGE",
Usage: "facebook page to link to in footer",
},
cli.StringFlag{
Name: "coc-github-repo",
EnvVar: "CODE_OF_CONDUCT_GITHUB_REPO",
Usage: "github repository to fetch code of conduct from",
Value: "bellinghamcodes/code-of-conduct",
},
cli.IntFlag{
Name: "coc-fetch-interval",
EnvVar: "CODE_OF_CONDUCT_FETCH_INTERVAL",
Usage: "fetch interval in minutes for code of conduct",
Value: 90,
},
}
app.Action = run
app.Run(os.Args)
}
func run(c *cli.Context) error {
groupName := c.String("meetup")
instagramUser := c.String("instagram")
interval := time.Duration(c.Int("meetup-fetch-interval"))
homePageServer := &HomePageServer{
GroupName: c.String("organization-name"),
TwitterUsername: c.String("twitter"),
InstagramUsername: instagramUser,
FacebookPage: c.String("facebook"),
MeetupGroupName: groupName,
}
eventsChan := make(chan []meetup.Event)
go meetupLoop(groupName, eventsChan, time.Minute*interval)
imagesChan := make(chan []Image)
imagesInterval := time.Duration(c.Int("instagram-fetch-interval"))
max := c.Int("instagram-count")
go instagramLoop(instagramUser, imagesChan, max, time.Minute*imagesInterval)
cocServer := &HTMLServer{
GroupName: c.String("organization-name"),
Title: "Community Code of Conduct",
}
cocChan := make(chan template.HTML)
cocInterval := time.Duration(c.Int("coc-fetch-interval"))
repo := c.String("coc-github-repo")
go codeOfConductLoop(repo, time.Minute*cocInterval, cocChan)
go func() {
for {
select {
case events := <-eventsChan:
homePageServer.Events = events
case images := <-imagesChan:
homePageServer.Images = images
case coc := <-cocChan:
cocServer.Content = coc
}
}
}()
return serve(c, homePageServer, cocServer)
}
func serve(c *cli.Context, homePageServer http.Handler, codeOfConductServer http.Handler) error {
http.HandleFunc("/request-invite", inviteRequestHandler(c))
http.HandleFunc("/status", statusHandler(c))
http.Handle("/code-of-conduct", codeOfConductServer)
http.Handle("/", homePageServer)
addr := fmt.Sprintf("%s:%d", c.String("host"), c.Int("port"))
log.Printf("Starting bellingham.codes v%s on %s\n", version, addr)
return http.ListenAndServe(addr, nil)
}
func meetupLoop(groupName string, c chan []meetup.Event, interval time.Duration) {
client := meetup.Client{
GroupURLName: groupName,
}
for {
events, err := client.FetchEvents()
if err != nil {
log.Printf("Meetup.com fetch error: %s\n", err)
goto SLEEP
}
log.Printf("Successfully fetched %d events from Meetup.com", len(events))
c <- events
SLEEP:
time.Sleep(interval)
}
}
func instagramLoop(username string, c chan []Image, max int, interval time.Duration) {
client := instagram.Client{}
for {
var images []Image
count := 0
media, err := client.MediaForUser(username)
if err != nil {
log.Printf("Instagram fetch error: %s\n", err)
goto SLEEP
}
for _, m := range media {
// Skip non-images
if m.Type != instagram.ImageMediaType {
continue
}
images = append(images, Image{
Src: m.Thumbnail,
Link: "https://www.instagram.com/" + username + "/",
Alt: m.Caption,
})
count++
// Limit based on max
if count >= max {
break
}
}
log.Printf("Successfully fetched %d images from Instagram", len(images))
c <- images
SLEEP:
time.Sleep(interval)
}
}
// codeOfConductLoop fetches the content for the Code of Conduct page regularly
// (as defined by the provided interval).
func codeOfConductLoop(repo string, interval time.Duration, c chan template.HTML) {
client := http.Client{Timeout: time.Second * 15}
for {
var data []byte
resp, err := client.Get("https://raw.githubusercontent.com/" + repo + "/master/README.md")
if err != nil {
log.Printf("Error fetching code of conduct: %s\n", err)
goto SLEEP
}
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error fetching code of conduct: %s\n", err)
goto SLEEP
}
log.Printf("Successfully fetched code of conduct (%d bytes) ", len(data))
data = blackfriday.Run(data, blackfriday.WithExtensions(blackfriday.Autolink|blackfriday.AutoHeadingIDs|blackfriday.HeadingIDs))
c <- template.HTML(data)
SLEEP:
time.Sleep(interval)
}
}