2018-12-26 13:30:02 -05:00
|
|
|
const https = require('https');
|
|
|
|
const fs = require('fs');
|
2018-07-24 19:39:18 -04:00
|
|
|
const express = require('express');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const methodOverride = require('method-override');
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
const morgan = require('morgan');
|
|
|
|
const passport = require('passport');
|
|
|
|
const session = require('express-session');
|
|
|
|
const sanitizer = require('express-sanitizer');
|
|
|
|
const helmet = require('helmet');
|
2018-12-26 20:47:04 -05:00
|
|
|
const rateLimit = require('express-rate-limit');
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-07-24 19:39:18 -04:00
|
|
|
const app = express();
|
|
|
|
const config = require('config');
|
2017-10-12 17:26:35 -04:00
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// MongoDB
|
2018-07-26 17:34:47 -04:00
|
|
|
const dbHost = config.get('Database.host');
|
|
|
|
let db;
|
|
|
|
mongoose.connect(dbHost, {useNewUrlParser: true})
|
|
|
|
.then(() => {
|
2018-07-29 19:35:58 -04:00
|
|
|
console.log('Connected to database ' + dbHost + '\n');
|
2018-07-26 17:34:47 -04:00
|
|
|
db = mongoose.connection;
|
|
|
|
});
|
|
|
|
|
2018-07-24 19:39:18 -04:00
|
|
|
const MongoStore = require('connect-mongo')(session);
|
2018-07-26 17:34:47 -04:00
|
|
|
const mongoStore = new MongoStore({url: dbHost});
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// HTTP Request Logging
|
2018-08-13 06:43:31 -04:00
|
|
|
if (config.get('Log.http'))
|
|
|
|
app.use(morgan(config.get('Log.httpLevel')));
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// Session setup
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use(helmet());
|
|
|
|
app.set('trust proxy', 1);
|
|
|
|
app.use(session({
|
|
|
|
secret: 'secret',
|
|
|
|
name: 'session.id',
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
store: mongoStore,
|
|
|
|
cookie: {
|
|
|
|
//secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
//domain: 'shimapan.rocks',
|
|
|
|
maxAge: 1000 * 60 * 60
|
|
|
|
}
|
|
|
|
}));
|
2018-07-25 10:18:29 -04:00
|
|
|
|
|
|
|
// Middleware
|
2018-07-26 19:01:16 -04:00
|
|
|
app.use(passport.initialize(null));
|
|
|
|
app.use(passport.session(null));
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(bodyParser.json());
|
2018-07-24 19:39:18 -04:00
|
|
|
app.use(bodyParser.json({ type: 'application/*+json' }));
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }));
|
2017-10-12 17:26:35 -04:00
|
|
|
app.use(bodyParser.text());
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use(sanitizer());
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(methodOverride('X-HTTP-Method-Override'));
|
|
|
|
|
2018-12-26 20:47:04 -05:00
|
|
|
// Rate limiter
|
|
|
|
const global_limiter = rateLimit({
|
|
|
|
windowMs: 60 * 1000, // 1 minute
|
|
|
|
max: 60 // limit to 1 request/second
|
|
|
|
});
|
|
|
|
|
|
|
|
if (config.get('RateLimit.enable'))
|
|
|
|
app.use(global_limiter);
|
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// Static directories and favicon
|
2017-10-18 13:31:08 -04:00
|
|
|
//app.use(favicon(__dirname + '/public/img/favicon.ico'));
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use(express.static(__dirname + '/public'));
|
2017-10-18 13:31:08 -04:00
|
|
|
|
2018-07-25 10:18:29 -04:00
|
|
|
// Install routes and configure authentication strategy
|
2017-10-20 16:46:14 -04:00
|
|
|
require('./app/routes/routes.js')(app);
|
2017-10-18 13:31:08 -04:00
|
|
|
require('./config/passport.js');
|
|
|
|
|
2018-07-25 18:43:20 -04:00
|
|
|
// Error handler
|
2018-08-14 05:12:05 -04:00
|
|
|
app.use((err, req, res, next) => {
|
2019-01-02 14:20:46 -05:00
|
|
|
console.error('Unhandled error!');
|
|
|
|
console.error(err);
|
2018-07-25 18:43:20 -04:00
|
|
|
console.error(err.stack);
|
|
|
|
res.status(500).json({'message': 'Internal server error.'});
|
|
|
|
});
|
|
|
|
|
2018-12-26 19:02:59 -05:00
|
|
|
app.set('trust proxy', config.get('Server.trustProxy'));
|
|
|
|
|
2017-10-09 22:01:02 -04:00
|
|
|
// Start app
|
2018-07-26 17:34:47 -04:00
|
|
|
const port = config.get('Server.port');
|
2018-12-26 13:30:02 -05:00
|
|
|
|
|
|
|
let server;
|
|
|
|
if (config.get('Server.tls')) {
|
|
|
|
const options = {
|
|
|
|
cert: fs.readFileSync('./cert.pem'),
|
|
|
|
key: fs.readFileSync('./privkey.pem'),
|
|
|
|
};
|
|
|
|
server = https.createServer(options, app).listen(port, () =>
|
|
|
|
console.log(`Listening on port ${port}...`));
|
|
|
|
} else {
|
|
|
|
server = app.listen(port, () =>
|
|
|
|
console.log(`Listening on port ${port}...`));
|
|
|
|
}
|
2018-07-24 19:39:18 -04:00
|
|
|
|
2017-10-09 22:01:02 -04:00
|
|
|
// Expose app
|
2018-07-26 13:16:42 -04:00
|
|
|
module.exports.app = app;
|
|
|
|
module.exports.server = server;
|