1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-15 09:08:06 -05:00
shimapan/server.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-12-26 13:30:02 -05:00
const https = require('https');
const fs = require('fs');
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
const app = express();
const config = require('config');
2017-10-12 17:26:35 -04:00
// MongoDB
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');
db = mongoose.connection;
});
const MongoStore = require('connect-mongo')(session);
const mongoStore = new MongoStore({url: dbHost});
2017-10-09 22:01:02 -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
// 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
}
}));
// 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());
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);
// 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
// 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) => {
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
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}...`));
}
2017-10-09 22:01:02 -04:00
// Expose app
module.exports.app = app;
module.exports.server = server;