-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.coffee
125 lines (108 loc) · 4.33 KB
/
server.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
NotFound = (msg) ->
@name = "NotFound"
Error.call this, msg
Error.captureStackTrace this, arguments.callee
siteConf = require("./lib/getConfig")
process.title = siteConf.uri.replace(/http:\/\/(www)?/, "")
airbrake = require("airbrake").createClient(siteConf.airbrakeApiKey) if siteConf.airbrakeApiKey
process.addListener "uncaughtException", (err, stack) ->
console.log "Caught exception: " + err + "\n" + err.stack
console.log "\u0007"
airbrake.notify err if airbrake
connect = require("connect")
express = require("express")
assetManager = require("connect-assetmanager")
assetHandler = require("connect-assetmanager-handlers")
notifoMiddleware = require("connect-notifo")
DummyHelper = require("./lib/dummy-helper")
Bidder = require("./bidder").Bidder
#RedisStore = require("connect-redis")(express)
#sessionStore = new RedisStore
sessionStore = new express.session.MemoryStore()
app = module.exports = express.createServer()
app.listen siteConf.port, null
socketIo = new require("./lib/socket-io-server.js")(app, sessionStore)
authentication = new require("./lib/authentication.js")(app, siteConf)
assetsSettings =
js:
route: /\/static\/js\/[a-z0-9]+\/.*\.js/
path: "./public/js/"
dataType: "javascript"
files: [ "http://code.jquery.com/jquery-latest.js", siteConf.uri + "/socket.io/socket.io.js", "jquery.client.js" ]
debug: true
postManipulate: "^": [ insertSocketIoPort = (file, path, index, isLast, callback) -> callback file.replace(/.#socketIoPort#./, siteConf.port) ]
css:
route: /\/static\/css\/[a-z0-9]+\/.*\.css/
path: "./public/css/"
dataType: "css"
files: [ "reset.css", "client.css", "charity.css" ]
debug: true
postManipulate: "^": [ assetHandler.fixVendorPrefixes, assetHandler.fixGradients, assetHandler.replaceImageRefToBase64(__dirname + "/public"), assetHandler.yuiCssOptimize ]
app.configure "development", ->
[ [ "js", "updatedContent" ], [ "css", "updatedCss" ] ].forEach (group) ->
assetsSettings[group[0]].postManipulate["^"].push triggerUpdate = (file, path, index, isLast, callback) ->
callback file
dummyHelpers[group[1]]()
assetsMiddleware = assetManager(assetsSettings)
app.configure ->
app.set "view engine", "ejs"
app.set "views", __dirname + "/views"
app.configure ->
app.use express.bodyParser()
app.use express.cookieParser()
app.use assetsMiddleware
app.use express.session(
store: sessionStore
secret: siteConf.sessionSecret
)
app.use express.logger(format: ":response-time ms - :date - :req[x-real-ip] - :method :url :user-agent / :referrer")
app.use authentication.middleware.auth()
app.use authentication.middleware.normalizeUserData()
app.use express["static"](__dirname + "/public", maxAge: 86400000)
if siteConf.notifoAuth
app.use notifoMiddleware(siteConf.notifoAuth,
filter: (req, res, callback) ->
callback null, (not req.xhr and not (req.headers["x-real-ip"] or req.connection.remoteAddress).match(/192.168./))
format: (req, res, callback) ->
callback null,
title: ":req[x-real-ip]/:remote-addr @ :req[host]"
message: ":response-time ms - :date - :req[x-real-ip]/:remote-addr - :method :user-agent / :referrer"
)
app.configure "development", ->
app.use express.errorHandler(
dumpExceptions: true
showStack: true
)
app.all "/robots.txt", (req, res) ->
res.send "User-agent: *\nDisallow: /", "Content-Type": "text/plain"
app.configure "production", ->
app.use express.errorHandler()
app.all "/robots.txt", (req, res) ->
res.send "User-agent: *", "Content-Type": "text/plain"
app.dynamicHelpers
assetsCacheHashes: (req, res) ->
assetsMiddleware.cacheHashes
session: (req, res) ->
req.session
app.error (err, req, res, next) ->
console.log err
airbrake.notify err if airbrake
if err instanceof NotFound
res.render "errors/404"
else
res.render "errors/500"
app.all "/", (req, res) ->
req.session.uid = (0 | Math.random() * 1000000) unless req.session.uid
if req.session.user
res.locals key: "value"
res.render "index"
else
res.render "login"
app.all "/view", (req, res) ->
res.render "view"
app.all "/return", (req, res) ->
res.render "return"
dummyHelpers = new DummyHelper(app)
app.all "*", (req, res) ->
throw new NotFound
console.log "Running in " + (process.env.NODE_ENV or "development") + " mode @ " + siteConf.uri