-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
110 lines (88 loc) · 2.65 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
'use strict';
//dependencies
var config = require('./config'),
express = require('express'),
http = require('http'),
path = require('path'),
mongoose = require('mongoose'),
helmet = require('helmet'),
bodyParser = require('body-parser'),
fs = require('fs'),
morgan = require('morgan'),
sequelize = require('sequelize'),
mongoskin = require('mongoskin'),
responseTime = require('response-time'),
gcm = require('node-gcm'),
apn = require('apn');
//create express app
var app = express();
//keep reference to config
app.config = config;
//setup the web server
app.server = http.createServer(app);
//setup utilities
app.utility = {};
app.utility.slugify = require('./util/slugify');
app.utility.workflow = require('./util/workflow');
app.utility.json = require('./util/json');
app.utility.outcome = require('./util/outcome');
app.utility.hmac = require('./util/hmac');
app.db = {
adapters: {
mongoose: mongoose,
sequelize: sequelize,
mongoskin: mongoskin
}
};
//setup mongoose
app.db.mongoose = mongoose.createConnection(config.mongoose.uri);
app.db.mongoose.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.mongoskin = mongoskin.db(config.mongoskin.uri, {safe:true});
//setup notification service
app.notifications = {
transport: {
apn: apn,
gcm: gcm
}
};
//config data models
require('./models')(app);
//settings
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//middleware
if (app.get('env') === 'development') {
app.use(morgan('dev'));
}
if (app.get('env') === 'production') {
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'logs', 'access.log'), {flags: 'a'});
app.use(morgan('combined', {stream: accessLogStream}));
}
app.use(require('compression')());
app.use(require('serve-static')(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json({ type: ["application/json", "application/*+json"]}));
app.use(require('method-override')());
app.use(helmet());
app.use(responseTime());
//global locals
app.locals.projectName = app.config.projectName;
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.config.companyName;
//setup message queue
require('./queues')(app);
//setup routing middleware
require('./middleware')(app);
//setup routes
require('./routes')(app);
//custom (friendly) error handler
app.use(require('./views/http/index').http500);
//listen up
app.server.listen(app.config.port, app.config.ipAddress, function(){
//and... we're live
});
module.exports = app;