-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.js
170 lines (128 loc) · 4.79 KB
/
server.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
var express = require('express'),
path = require('path'),
fs = require('fs'),
bodyParser = require('body-parser'),
util = require('util'),
expressValidator = require('express-validator'),
session = require('express-session'),
mongoStore = require('connect-mongo')(session),
passport = require('passport'),
compression = require('compression'),
busboy = require('connect-busboy'),
httpProxy = require('http-proxy'),
url = require('url'),
config = require('./lib/config/config.js');
/**
* Main application file
*
* Note: consider storing express settings in a separate file: require('./lib/config/express')(app);
*/
// Set default node environment to development
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// get an express instance
var app = express();
if (env == 'development') {
app.use(require('connect-livereload')());
// Disable caching of scripts for easier testing
app.use(function noCache(req, res, next) {
if (req.url.indexOf('/scripts/') === 0) {
res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
res.header('Pragma', 'no-cache');
res.header('Expires', 0);
}
next();
});
app.use(express.static(path.join(config.root, '.tmp')));
app.use(express.static(path.join(config.root, 'app')));
//app.use(express.static(path.join(config.root, ''));
app.set('views', config.root + '/app/views');
}
if(env == 'production') {
var compression = require('compression');
app.use(compression());
app.use(express.static(path.join(config.root, 'public')));
app.set('views', config.root + '/views');
}
app.use(busboy());
app.use(bodyParser({limit: '10mb'}));
app.use(expressValidator());
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
// set configuration for settings
if (env !== 'test') {
// by default we persist sessions using the Mongo database
app.use(session(
{
name: config.sessionSettings.name,
secret: config.sessionSettings.secret,
cookie: {maxAge: 30 * 24 * 60 * 60 * 1000}, // session expires after 30 days (unit is ms)
resave: true,
saveUninitialized: true,
store: new mongoStore({db: config.sessionSettings.mongoStoreDbName, clear_interval: 6 * 3600}) // clear interval removes expired sessions every 6h (unit is sec)
}
));
} else {
// when testing we don't want to use the mongo store but in-memory store for sessions
app.use(session(
{
name: config.sessionSettings.name,
secret: config.sessionSettings.secret
}
));
}
//use passport session
app.use(passport.initialize());
app.use(passport.session());
require('./lib/config/passport.js'); // configure passport & set strategy
// Create the server using app
var server = require('http').createServer(app);
// Create a proxy server that redirects WebSocket request directly to the Docker daemon.
var wsLoadBalancer = httpProxy.createProxyServer({target: 'ws://' + config.mantra.ip + ':' + config.mantra.port, changeOrigin: true});
// Intercept upgrade events (from http to WS) and forward the to docker daemon
server.on('upgrade', function(req, socket, head) {
var urlObject = url.parse(req.url, true);
var mantraNode = urlObject.query.mantra;
req.headers['cookie'] = 'CoboMantraId=' + mantraNode;
wsLoadBalancer.ws(req, socket, head);
});
// listen to error
wsLoadBalancer.on('error', function (err, req, res) {
console.log('---\n' + (new Date(Date.now())).toLocaleString('en-US') + ' HTTP-Proxy Error:');
console.log(err);
console.log('---');
});
// load the routes only after setting static etc. Otherwise the files won't be served correctly
require('./lib/routes')(app);
var db = require('./lib/models');
if (env == 'development') {
// Disable the foreign key constraints before trying to sync the models.
// Otherwise, we're likely to violate a constraint while dropping a table.
db.sequelize.query('SET FOREIGN_KEY_CHECKS = 0')
.spread(function(res, meta) {
return db.sequelize.sync({force: false});
})
.then(function() {
return db.sequelize.query('SET FOREIGN_KEY_CHECKS = 1');
})
.spread(function(res, meta) {
if (false) {
throw err[0]
} else {
server.listen(config.port, function () {
// add the template projects to the db; note that this is an async operation
var templateProjects = require('./lib/config/dbTemplateProjects.js');
templateProjects.addAllTemplateProjects();
console.log('Codeboard server listening on port %d in %s mode', config.port, app.get('env'));
});
}
});
}
if(env == 'production') {
// start the server
server.listen(config.port, function () {
console.log('Codeboard server listening on port %d in %s mode', config.port, app.get('env'));
});
}
// Expose app
module.exports = app;