-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
56 lines (43 loc) · 1.64 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
now = require("now")
express = require('express')
packageme = require("packageme")
rexsterAdapter = require("./adapter")
subscriber = require("./SignalSubscriber")
_ = require("underscore")
app = module.exports = express.createServer()
app.configure () ->
app.use express.bodyParser()
app.use express.methodOverride()
app.use packageme(__dirname+"/rexster-client").toURI("/client.js")
app.use app.router
app.configure 'development', () ->
app.use express.errorHandler({ dumpExceptions: true, showStack: true })
app.use express.static __dirname
app.configure 'production', () ->
app.use express.errorHandler()
app.listen 8000
everyone = now.initialize app
subscribers = []
everyone.now.bind = (clientId, event, resultHandler) ->
subscribers.push new subscriber clientId, event, resultHandler
everyone.now.unbind = (clientId, event) ->
_.each subscribers, (subscriber) ->
if subscriber.event == event and subscriber.clientId == clientId
subscribers = _.without subscribers, subscriber
everyone.now.execute = (clientId, req, resultHandler) ->
rexsterAdapter.proxyRequest req, (err, result) ->
if err
resultHandler err, null
return
try
# return response to req origin first
response = JSON.parse result
resultHandler null, response
catch err
resultHandler err, null
return
# notify all subscribers matching req as action event
_.each subscribers, (subscriber) ->
if subscriber.match(req) and subscriber.clientId != clientId
subscriber.notify req, response
console.log "rexter adapter listening on port #{app.address().port} in #{app.settings.env} mode"