2016-10-16 05:34:34 +00:00
|
|
|
"use strict";
|
2016-08-17 00:56:55 +00:00
|
|
|
// ===========================================
|
2016-12-09 03:44:31 +00:00
|
|
|
// Wiki.js
|
2016-08-17 00:56:55 +00:00
|
|
|
// 1.0.0
|
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
global.PROCNAME = 'SERVER';
|
2016-11-21 01:09:50 +00:00
|
|
|
global.ROOTPATH = __dirname;
|
|
|
|
global.IS_DEBUG = process.env.NODE_ENV === 'development';
|
2016-11-23 03:06:59 +00:00
|
|
|
if(IS_DEBUG) {
|
|
|
|
global.CORE_PATH = ROOTPATH + '/../core/';
|
|
|
|
} else {
|
2016-11-23 03:21:03 +00:00
|
|
|
global.CORE_PATH = ROOTPATH + '/node_modules/requarks-core/';
|
2016-11-23 03:06:59 +00:00
|
|
|
}
|
2016-08-20 21:20:53 +00:00
|
|
|
|
2016-08-17 00:56:55 +00:00
|
|
|
// ----------------------------------------
|
2016-09-25 04:08:49 +00:00
|
|
|
// Load Winston
|
2016-08-17 00:56:55 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-11-21 01:09:50 +00:00
|
|
|
global.winston = require(CORE_PATH + 'core-libs/winston')(IS_DEBUG);
|
2016-12-09 03:44:31 +00:00
|
|
|
winston.info('[SERVER] Wiki.js is initializing...');
|
2016-08-17 00:56:55 +00:00
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
// Load global modules
|
|
|
|
// ----------------------------------------
|
2016-08-22 03:18:31 +00:00
|
|
|
|
2017-01-10 03:45:56 +00:00
|
|
|
let appconf = require(CORE_PATH + 'core-libs/config')();
|
|
|
|
global.appconfig = appconf.config;
|
|
|
|
global.appdata = appconf.data;
|
|
|
|
global.lcdata = require('./libs/local').init();
|
|
|
|
global.db = require(CORE_PATH + 'core-libs/mongodb').init();
|
|
|
|
global.entries = require('./libs/entries').init();
|
|
|
|
global.git = require('./libs/git').init(false);
|
2016-10-16 05:34:34 +00:00
|
|
|
global.lang = require('i18next');
|
|
|
|
global.mark = require('./libs/markdown');
|
2017-01-10 03:45:56 +00:00
|
|
|
global.upl = require('./libs/uploads').init();
|
2016-08-20 21:20:53 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Load modules
|
|
|
|
// ----------------------------------------
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2016-10-16 05:34:34 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const autoload = require('auto-load');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const compression = require('compression');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const express = require('express');
|
|
|
|
const favicon = require('serve-favicon');
|
|
|
|
const flash = require('connect-flash');
|
|
|
|
const fork = require('child_process').fork;
|
|
|
|
const http = require('http');
|
|
|
|
const i18next_backend = require('i18next-node-fs-backend');
|
|
|
|
const i18next_mw = require('i18next-express-middleware');
|
|
|
|
const passport = require('passport');
|
2016-10-30 22:41:10 +00:00
|
|
|
const passportSocketIo = require('passport.socketio');
|
2016-10-16 05:34:34 +00:00
|
|
|
const path = require('path');
|
|
|
|
const session = require('express-session');
|
|
|
|
const sessionMongoStore = require('connect-mongo')(session);
|
|
|
|
const socketio = require('socket.io');
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2016-11-21 01:09:50 +00:00
|
|
|
var mw = autoload(CORE_PATH + '/core-middlewares');
|
2016-08-17 03:56:08 +00:00
|
|
|
var ctrl = autoload(path.join(ROOTPATH, '/controllers'));
|
2016-10-16 05:34:34 +00:00
|
|
|
var libInternalAuth = require('./libs/internalAuth');
|
|
|
|
|
|
|
|
global.WSInternalKey = libInternalAuth.generateKey();
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
global.app = express();
|
2016-10-16 05:34:34 +00:00
|
|
|
app.use(compression());
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Security
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(mw.security);
|
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-16 05:34:34 +00:00
|
|
|
// Public Assets
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')));
|
|
|
|
app.use(express.static(path.join(ROOTPATH, 'assets')));
|
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-11-21 01:09:50 +00:00
|
|
|
// Passport Authentication
|
2016-08-17 03:56:08 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2017-01-10 03:45:56 +00:00
|
|
|
var strategy = require(CORE_PATH + 'core-libs/auth')(passport);
|
2016-11-21 01:09:50 +00:00
|
|
|
global.rights = require(CORE_PATH + 'core-libs/rights');
|
2017-01-03 04:32:16 +00:00
|
|
|
rights.init();
|
2016-11-21 01:09:50 +00:00
|
|
|
|
2016-10-30 22:41:10 +00:00
|
|
|
var sessionStore = new sessionMongoStore({
|
|
|
|
mongooseConnection: db.connection,
|
|
|
|
touchAfter: 15
|
|
|
|
});
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(session({
|
|
|
|
name: 'requarkswiki.sid',
|
2016-10-30 22:41:10 +00:00
|
|
|
store: sessionStore,
|
2016-08-17 03:56:08 +00:00
|
|
|
secret: appconfig.sessionSecret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false
|
|
|
|
}));
|
|
|
|
app.use(flash());
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Localization Engine
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
lang
|
|
|
|
.use(i18next_backend)
|
|
|
|
.use(i18next_mw.LanguageDetector)
|
|
|
|
.init({
|
|
|
|
load: 'languageOnly',
|
2016-11-02 03:02:11 +00:00
|
|
|
ns: ['common', 'auth'],
|
2016-08-17 03:56:08 +00:00
|
|
|
defaultNS: 'common',
|
|
|
|
saveMissing: false,
|
|
|
|
supportedLngs: ['en', 'fr'],
|
|
|
|
preload: ['en', 'fr'],
|
|
|
|
fallbackLng : 'en',
|
|
|
|
backend: {
|
|
|
|
loadPath: './locales/{{lng}}/{{ns}}.json'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View Engine Setup
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(i18next_mw.handle(lang));
|
|
|
|
app.set('views', path.join(ROOTPATH, 'views'));
|
|
|
|
app.set('view engine', 'pug');
|
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// View accessible data
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.locals._ = require('lodash');
|
|
|
|
app.locals.moment = require('moment');
|
|
|
|
app.locals.appconfig = appconfig;
|
|
|
|
app.use(mw.flash);
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Controllers
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use('/', ctrl.auth);
|
|
|
|
|
2016-10-30 22:41:10 +00:00
|
|
|
app.use('/uploads', mw.auth, ctrl.uploads);
|
2016-08-17 03:56:08 +00:00
|
|
|
app.use('/admin', mw.auth, ctrl.admin);
|
2016-10-30 22:41:10 +00:00
|
|
|
app.use('/', mw.auth, ctrl.pages);
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Error handling
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
var err = new Error('Not Found');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
2016-11-21 01:09:50 +00:00
|
|
|
error: IS_DEBUG ? err : {}
|
2016-08-17 03:56:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Start HTTP server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-10-16 05:34:34 +00:00
|
|
|
winston.info('[SERVER] Starting HTTP/WS server on port ' + appconfig.port + '...');
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
app.set('port', appconfig.port);
|
|
|
|
var server = http.createServer(app);
|
2016-10-16 05:34:34 +00:00
|
|
|
var io = socketio(server);
|
|
|
|
|
2016-08-17 03:56:08 +00:00
|
|
|
server.listen(appconfig.port);
|
|
|
|
server.on('error', (error) => {
|
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle specific listen errors with friendly messages
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
|
|
|
console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
case 'EADDRINUSE':
|
|
|
|
console.error('Port ' + appconfig.port + ' is already in use!');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.on('listening', () => {
|
2016-10-16 05:34:34 +00:00
|
|
|
winston.info('[SERVER] HTTP/WS server started successfully! [RUNNING]');
|
2016-09-01 02:45:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-30 22:41:10 +00:00
|
|
|
// WebSocket
|
2016-09-01 02:45:28 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-10-30 22:41:10 +00:00
|
|
|
io.use(passportSocketIo.authorize({
|
|
|
|
key: 'requarkswiki.sid',
|
|
|
|
store: sessionStore,
|
|
|
|
secret: appconfig.sessionSecret,
|
|
|
|
passport,
|
|
|
|
cookieParser,
|
|
|
|
success: (data, accept) => {
|
|
|
|
accept();
|
|
|
|
},
|
|
|
|
fail: (data, message, error, accept) => {
|
|
|
|
return accept(new Error(message));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2016-10-16 05:34:34 +00:00
|
|
|
io.on('connection', ctrl.ws);
|
2016-09-05 23:23:49 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-10-16 05:34:34 +00:00
|
|
|
// Start child processes
|
2016-09-05 23:23:49 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
2016-10-16 23:09:43 +00:00
|
|
|
global.bgAgent = fork('agent.js');
|
2016-09-05 23:23:49 +00:00
|
|
|
|
2016-10-16 05:34:34 +00:00
|
|
|
process.on('exit', (code) => {
|
|
|
|
bgAgent.disconnect();
|
2016-08-17 03:56:08 +00:00
|
|
|
});
|