-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
73 lines (59 loc) · 1.81 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
# npm dependencies
express = require 'express'
routes = require './routes'
http = require 'http'
path = require 'path'
socket_io = require 'socket.io'
_ = require 'underscore'
# lib dependencies
Redis = require('./lib/redis.coffee').Redis
NewRelic = require('./lib/new_relic.coffee').NewRelic
# config
redis_conf =
host: process.env.REDIS_HOST
port: process.env.REDIS_PORT
auth: process.env.REDIS_PASSWORD
new_relic_conf=
api_key: process.env.NEW_RELIC_API_KEY
account_id: process.env.NEW_RELIC_ID
app_id: 7821
app = express();
# all environments
app.set 'port', process.env.PORT || 3000
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.use express.favicon()
app.use express.logger('dev')
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use require('connect-coffee-script')(__dirname + '/public')
app.use require('stylus').middleware(__dirname + '/public')
app.use express.static(path.join(__dirname, 'public'))
# development only
if app.get('env') == 'development'
app.use express.errorHandler()
app.get('/', routes.index);
server = http.createServer(app).listen app.get('port'), ->
console.log 'Express server listening on port ' + app.get('port')
# configure socket.io
io = socket_io.listen server
io.configure ->
io.set 'transports', ['websocket', 'xhr-polling']
# connect to new relic
newrelic = new NewRelic new_relic_conf
# connect to redis
redis = new Redis
host: redis_conf.host
port: redis_conf.port
password: redis_conf.auth
# socket logic
io.sockets.on 'connection', (socket) ->
# push hits
redis.on 'hits', (hits) ->
socket.emit 'hits', _.map hits, (hit) -> JSON.parse(hit)
redis.on 'hit', (hit) ->
socket.emit 'hit', JSON.parse(hit)
# push newrelic stats
newrelic.on 'data', (data) ->
socket.emit 'new_relic', data