-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
142 lines (123 loc) · 5.01 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
// server.js
//TODO: consider setting up logging via winston (https://github.com/flatiron/winston)
// set up ======================================================================
// if this is a c9 project using argv rather than environment variable
// then run configurations configure appropriately
if(process.env.C9_PROJECT) {
require('./config/c9');
}
// if configured to use newrelic connect to it
if(process.env.NEW_RELIC_LICENSE_KEY) {
require('newrelic');
}
var port = process.env.PORT || 8080;
// if we're behind an http proxy set up all requests to go through it
if(process.env.HTTP_PROXY) {
var parsedURL = require('url').parse(process.env.HTTP_PROXY);
var host = parsedURL && parsedURL.hostname ? parsedURL.hostname : '127.0.0.1';
port = parsedURL && parsedURL.port ? parseInt(parsedURL.port, 10) : 8080;
require('./config/proxy')(host, port);
}
// get all the tools we need
var express = require('express');
var helmet = require('helmet');
var app = express();
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var compression = require('compression');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var configDB = require('./config/database');
var MongoStore = require('connect-mongo')(session);
// add Sentry handler first if configured
if(process.env.SENTRY_DSN) {
var raven = require('raven');
var client = new raven.Client(process.env.SENTRY_DSN);
client.patchGlobal();
app.use(raven.middleware.express.requestHandler(process.env.SENTRY_DSN));
}
// configuration ===============================================================
if(process.env.MONGOOSE_DEBUG) {
mongoose.set('debug', process.env.MONGOOSE_DEBUG);
}
//TODO: having issues if web connections come in before this is established
// https://github.com/kcbanner/connect-mongo/issues/80
var sessionStore = new MongoStore({
url: configDB.url,
autoReconnect: true
});
mongoose.plugin(schema => { schema.options.usePushEach = true }); // MongoDB 3.6+ deprecates $pushAll, set flag for mongoose to avoid that operator
// connect to our database
const connectWithRetry = () => {
mongoose.connect(configDB.url)
.then(() => console.log('connected to mongo'))
.catch((err) => {
console.error('mongo failed to connect', err);
setTimeout(connectWithRetry, 2500);
});
}
connectWithRetry();
require('./config/passport')(passport); // pass passport for configuration
// set up our express application
app.use(helmet()); // set HTTP headers via Helmet
app.set('view engine', 'ejs'); // set up ejs for templating
if(!process.env.C9_PROJECT) {
// redirect to https if the request isn't secured
app.set('trust proxy', true);
// disabled as dns redirect + nginx-proxy is handling https
if(process.env.ENFORCE_HTTPS) {
app.use(function(req, res, next) {
if((!req.secure || !req.host.startsWith('www.')) && !req.url.startsWith('.well-known/acme-challenge/')) {
return res.redirect(process.env.SERVER_URL);
}
next();
});
}
} else {
// temporary for C9 beta which does not correctly set the working directory
// TODO: remove once bug is fixed
app.set('views', '/home/ubuntu/workspace/views');
}
app.use(compression());
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // get info from html forms
//TODO: consider using static cache (https://github.com/isaacs/st)
// set one week caching on static content
app.use('/content', express.static(__dirname + '/content', { maxAge: 604800000 }));
// required for passport
process.env.EXPRESS_SESSION_SECRET = process.env.EXPRESS_SESSION_SECRET ||
'ilovescotchscotchyscotchscotch';
app.use(session({
secret: process.env.EXPRESS_SESSION_SECRET,
cookie: { maxAge: 31536000 },
key: 'express.sid',
store: sessionStore,
resave: false,
saveUninitialized: false
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// error handling ==============================================================
process.on('uncaughtException', function (err) {
console.log(err);
});
// routes ======================================================================
// load our routes and pass in our app and fully configured passport
require('./app/routes')(app, passport, client);
if(process.env.SENTRY_DSN) {
app.use(raven.middleware.express.errorHandler(process.env.SENTRY_DSN));
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry+'\n');
});
}
// launch ======================================================================
var server = app.listen(port);
console.log('Server listening on port ' + port);
require('./app/gameserver')(server, sessionStore);