jschan - Anonymous imageboard software. Classic look, modern features and feel. Works without JavaScript and supports Tor, I2P, Lokinet, etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.9 KiB

5 years ago
'use strict';
process
.on('uncaughtException', console.error)
.on('unhandledRejection', console.error);
5 years ago
const express = require('express')
, path = require('path')
, app = express()
, server = require('http').createServer(app)
5 years ago
, cookieParser = require('cookie-parser')
, { cacheTemplates, boardDefaults, globalLimits, captchaOptions,
enableUserBoardCreation, enableUserAccountCreation, cookieSecret,
debugLogs, ipHashPermLevel, meta, port, enableWebring } = require(__dirname+'/configs/main.js')
, referrerCheck = require(__dirname+'/helpers/referrercheck.js')
, { themes, codeThemes } = require(__dirname+'/helpers/themes.js')
, Mongo = require(__dirname+'/db/db.js')
, Socketio = require(__dirname+'/socketio.js')
, commit = require(__dirname+'/helpers/commit.js')
, dynamicResponse = require(__dirname+'/helpers/dynamic.js')
, formatSize = require(__dirname+'/helpers/files/formatsize.js')
, CachePugTemplates = require('cache-pug-templates');
5 years ago
(async () => {
const env = process.env.NODE_ENV;
const production = env === 'production';
debugLogs && console.log('STARTING IN MODE:', env);
// connect to mongodb
debugLogs && console.log('CONNECTING TO MONGODB');
5 years ago
await Mongo.connect();
await Mongo.checkVersion();
// connect to redis
debugLogs && console.log('CONNECTING TO REDIS');
const { redisClient } = require(__dirname+'/redis.js');
5 years ago
// disable useless express header
app.disable('x-powered-by');
// parse forms
app.use(express.urlencoded({extended: false}));
// parse cookies
app.use(cookieParser(cookieSecret));
5 years ago
// session store
const sessionMiddleware = require(__dirname+'/helpers/usesession.js');
// connect socketio
debugLogs && console.log('STARTING WEBSOCKET');
Socketio.connect(server, sessionMiddleware);
5 years ago
//trust proxy for nginx
app.set('trust proxy', 1);
5 years ago
//self explanatory middlewares
app.use(referrerCheck);
5 years ago
// use pug view engine
const views = path.join(__dirname, 'views/pages');
5 years ago
app.set('view engine', 'pug');
app.set('views', views);
//cache loaded templates
if (cacheTemplates === true) {
app.enable('view cache');
}
5 years ago
//default settings
app.locals.enableUserAccountCreation = enableUserAccountCreation;
app.locals.enableUserBoardCreation = enableUserBoardCreation;
app.locals.defaultTheme = boardDefaults.theme;
app.locals.defaultCodeTheme = boardDefaults.codeTheme;
app.locals.globalLimits = globalLimits;
app.locals.ipHashPermLevel = ipHashPermLevel;
app.locals.enableWebring = enableWebring;
app.locals.commit = commit;
app.locals.meta = meta;
app.locals.captchaType = captchaOptions.type;
app.locals.postFilesSize = formatSize(globalLimits.postFilesSize.max);
switch (captchaOptions.type) {
case 'google':
app.locals.googleRecaptchaSiteKey = captchaOptions.google.siteKey;
break;
case 'grid':
app.locals.captchaGridSize = captchaOptions.grid.size;
break;
default:
break;
}
5 years ago
// routes
if (!production) {
app.use(express.static(__dirname+'/static', { redirect: false }));
app.use(express.static(__dirname+'/static/html', { redirect: false }));
app.use(express.static(__dirname+'/static/json', { redirect: false }));
}
app.use('/forms', require(__dirname+'/controllers/forms.js'));
app.use('/', require(__dirname+'/controllers/pages.js'));
5 years ago
//404 catchall
5 years ago
app.get('*', (req, res) => {
res.status(404).render('404');
5 years ago
})
// catch any unhandled errors
5 years ago
app.use((err, req, res, next) => {
if (err.code === 'EBADCSRFTOKEN') {
return dynamicResponse(req, res, 403, 'message', {
'title': 'Forbidden',
'message': 'Invalid CSRF token'
});
5 years ago
}
console.error(err);
return dynamicResponse(req, res, 500, 'message', {
'title': 'Internal Server Error',
'error': 'Internal Server Error', //what to put here?
'redirect': req.headers.referer || '/'
});
5 years ago
})
//listen
server.listen(port, '127.0.0.1', () => {
new CachePugTemplates({ app, views }).start();
debugLogs && console.log(`LISTENING ON :${port}`);
//let PM2 know that this is ready for graceful reloads and to serialise startup
if (typeof process.send === 'function') {
//make sure we are a child process of PM2 i.e. not in dev
debugLogs && console.log('SENT READY SIGNAL TO PM2');
process.send('ready');
}
});
const gracefulStop = () => {
debugLogs && console.log('SIGINT SIGNAL RECEIVED');
// Stops the server from accepting new connections and finishes existing connections.
Socketio.io.close((err) => {
// if error, log and exit with error (1 code)
debugLogs && console.log('CLOSING SERVER');
if (err) {
console.error(err);
process.exit(1);
}
// close database connection
debugLogs && console.log('DISCONNECTING MONGODB');
Mongo.client.close();
//close redis connection
debugLogs && console.log('DISCONNECTING REDIS')
redisClient.quit();
// now close without error
process.exit(0);
});
};
//graceful stop
process.on('SIGINT', gracefulStop);
process.on('message', (message) => {
if (message === 'shutdown') {
gracefulStop();
}
});
5 years ago
})();