/
app.coffee
200 lines (175 loc) · 5.7 KB
/
app.coffee
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
connect = require('connect')
express = require('express')
url = require('url')
fs = require('fs')
qs = require('qs')
async = require('async')
moment = require('moment')
gravatar = require('gravatar')
template = require('jqtpl')
Github = require('./github')
Jenkins = require('./jenkins')
Lastfm = require('./lastfm')
EndPoint = require('./endpoint')
helpers = require('./helpers/helpers')
settings = {}
redis = {}
app = module.exports = express.createServer()
io = require('socket.io').listen(app)
# Configuration
app.configure( ()->
app.set('views', __dirname + '/views')
app.set('view engine', 'html')
app.set('view cache', false)
app.register('.html', require('jqtpl').express)
app.use(app.router)
app.use(express.static(__dirname + '/public'))
return
)
app.configure('development', () ->
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
settings = require('./settings')
redis = require('redis').createClient()
return
)
app.configure('production', () ->
app.use(express.errorHandler())
settings = require('./settings-heroku')
# Redis to go on Heroku, local Redis in development
rtg = url.parse(process.env.REDISTOGO_URL)
redis = require('redis').createClient(rtg.port, rtg.hostname)
redis.auth(rtg.auth.split(":")[1])
# No websockets for Heroku
io.configure () ->
io.set('transports', ['xhr-polling'])
io.set('polling duration', 10)
return
)
app.helpers({
gravatar:gravatar,
moment:moment
})
# get discretionList from Redis
settings.discretionList = []
getDiscretionList = (listName) ->
redis.lrange listName, 0, -1, (err, replies) ->
for reply in replies
settings.discretionList.push(JSON.parse(reply))
getDiscretionList 'Discretions'
# Middleware functions
basicAuth = (req, res, next) ->
if settings.auth.enabled
connect.basicAuth(settings.auth.user, settings.auth.pass)(req, res, next)
else
next()
ipWhitelist = (req, res, next) ->
remote = req.connection.remoteAddress
if settings.jenkins.ip and remote != settings.jenkins.ip
res.writeHead 403
res.end '403 Forbidden'
console.log "IP whitelist: blocked request to '#{req.url}' from '#{remote}'"
else
console.log "IP whitelist: allowed request to '#{req.url}' from '#{remote}'"
next()
# Routes
app.get('/javascripts/:resource.js', (req, res) ->
resource = req.params.resource
helpers.compile resource, (compiled) ->
res.contentType('application/javascript').send(compiled)
)
app.get '/', basicAuth, (req, res) ->
messages = []
statuses = []
gotMessages = false
gotStatuses = false
getMessages = ->
redis.lrange 'Messages', 0, 5, (err, replies) ->
for reply in replies
reply = JSON.parse(reply)
reply.project = helpers.discretify(reply.project, settings.discretionList)
reply.relTime = moment(reply.timestamp).fromNow()
messages.push(reply)
async.sortBy messages, (message, callback) ->
callback(null, 1 / Date.parse(message.timestamp))
,(err, results) ->
messages = results
gotMessages = true
app.emit 'gotMessages'
getStatuses = ->
redis.hgetall 'Jenkins', (err, replies) ->
for project, status of replies
project = helpers.discretify(project, settings.discretionList)
if status != 'SUCCESS'
statuses.push({project: project, status: status})
gotStatuses = true
app.emit 'gotStatuses'
render = ->
app.emit 'pageload'
res.render 'index', {
title: 'Mint Source',
messages: messages,
statuses: statuses,
songsEnabled: settings.lastfm.enabled
}
getMessages()
getStatuses()
# Render once we have messages and statuses back
app.once 'gotStatuses', ->
render() if gotMessages
app.once 'gotMessages', ->
render() if gotStatuses
app.post('/github_prh', basicAuth, (req, res) ->
# GitHub post recieve hook
# Remember to post to http://user:password@your-app/github_prh
# if using basic auth - https://github.com/blog/237-basic-auth-post-receives
body = ''
req.on('data', (data) -> body += data )
req.on('end', () ->
g = new Github(body, redis)
g.once('message', (data) -> io.sockets.emit('message', data))
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('OK')
)
)
app.post('/jenkins_pbh', ipWhitelist, (req, res) ->
# Jenkins post build hook
# Requires https://wiki.jenkins-ci.org/display/JENKINS/Notification+Plugin
if !settings.jenkins.enabled
console.log('Jenkins disabled')
res.writeHead(404)
res.end()
else
body = ''
req.on('data', (data) ->
body += data
)
req.on('end', () ->
post = JSON.parse(body)
return unless post.build.phase == 'FINISHED'
post.name = helpers.discretify(post.name, settings.discretionList)
j = new Jenkins(post, redis)
j.on('data', (data) -> io.sockets.emit('jenkins', data))
return
)
res.writeHead(200, {'Content-Type': 'text/html'})
res.end('OK')
)
app.post '/endpoint', basicAuth, (req,res) ->
# Generic endpoint, send JSON here, show it on the status board
body = ''
req.on 'data', (data) -> body += data
req.on 'end', ->
post = JSON.parse(body)
e = new EndPoint(post, redis)
e.on('data', (data) -> io.sockets.emit('message', data))
res.writeHead 200, {'Content-Type': 'text/html'}
res.end 'OK'
if settings.lastfm.enabled
lfm = new Lastfm({
user: settings.lastfm.user
apiKey: settings.lastfm.apiKey
})
app.on 'pageload', () -> lfm.createRequest()
lfm.on('song', (data) -> io.sockets.emit('lastfm', data))
app.listen(process.env.PORT || 1337)
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env)