-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
115 lines (100 loc) · 2.93 KB
/
app.js
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
#!/usr/bin/env node
var config = require('./config');
var express = require('express');
var cf = require('cloudfoundry');
var fs = require('fs');
var winston = require('winston');
var sprintf = require('sprintf').sprintf;
var RedisStore = require('connect-redis')(express);
var dateFormat = require('./server/lib/dateformat');
// Handy to be able to require() a JSON file
require.extensions[".json"] = function (m) {
m.exports = JSON.parse(fs.readFileSync(m.filename));
};
// Handle uncaught exceptions
/*process.on('uncaughtException', function(err) {
winston.error(err.toString(), err);
});*/
// Creat server
var app = express.createServer();
var cachePreventionKey = dateFormat(new Date(), 'yyyymmddHHMMss');
// Use EJS as template engine
app.register('.html', require('ejs'));
// Server configurations:
var maxAge = 0;
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
maxAge = 31557600000;
app.use(express.errorHandler());
require('./server/lib/syslog');
winston.setLevels(winston.config.syslog.levels);
winston.add(winston.transports.SyslogLogger, config.syslog);
});
app.configure(function(){
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
key: 'lba',
secret: config.secret,
store: new RedisStore({
host: config.redis.credentials.hostname,
port: config.redis.credentials.port,
db: config.redis.credentials.db,
pass: config.redis.credentials.password
})
}));
app.set('views', __dirname + '/client');
app.set('view engine', 'html');
app.set('view options', { env: app.settings.env, layout: false });
app.use(app.router);
app.use(express.static(__dirname + '/client', { maxAge: maxAge }));
app.use(function(req, res){
if (req.accepts('html')) {
res.status(404).send('<h1>Not found</h1>');
} else if (req.accepts('json')) {
res.status(404).send({error: 'Not found'});
} else {
res.status(404).contentType('text/plain').send('Not found');
}
});
});
app.helpers({
sprintf: function() {
return sprintf.apply(null, arguments);
},
getCachePrevetionKey: function() {
return cachePreventionKey;
}
});
app.dynamicHelpers({
session: function(req){
return req.session;
}
});
/**
* Error handle.
*/
app.error(function(err, req, res) {
winston.error('500', err);
if (req.accepts('html')) {
res.status(500).send(err);
} else if (req.accepts('json')) {
res.status(500).send({error: err});
} else {
res.status(500).contentType('text/plain').send(JSON.stringify(err));
}
});
// Register routes...
require('./server/routes/index')(app);
require('./server/routes/monitor')(app);
module.exports = app;
if (!module.parent) {
app.listen(config.server.port);
winston.info('App running in ' + app.settings.env + ' mode on port ' + config.server.port);
if (cf.cloud) {
winston.info('Did i told you that i was running on the cloud?');
}
}