Merged main & websocket server, refactored libs, image uploads fixes
This commit is contained in:
122
server.js
122
server.js
@@ -1,3 +1,4 @@
|
||||
"use strict";
|
||||
// ===========================================
|
||||
// REQUARKS WIKI
|
||||
// 1.0.0
|
||||
@@ -11,52 +12,57 @@ global.PROCNAME = 'SERVER';
|
||||
// Load Winston
|
||||
// ----------------------------------------
|
||||
|
||||
var _isDebug = process.env.NODE_ENV === 'development';
|
||||
global.winston = require('./lib/winston')(_isDebug);
|
||||
const _isDebug = process.env.NODE_ENV === 'development';
|
||||
global.winston = require('./libs/winston')(_isDebug);
|
||||
winston.info('[SERVER] Requarks Wiki is initializing...');
|
||||
|
||||
// ----------------------------------------
|
||||
// Load global modules
|
||||
// ----------------------------------------
|
||||
|
||||
var appconfig = require('./models/config')('./config.yml');
|
||||
global.lcdata = require('./models/server/local').init(appconfig);
|
||||
global.db = require('./models/mongo').init(appconfig);
|
||||
global.git = require('./models/git').init(appconfig, false);
|
||||
global.entries = require('./models/entries').init(appconfig);
|
||||
global.mark = require('./models/markdown');
|
||||
var appconfig = require('./libs/config')('./config.yml');
|
||||
global.lcdata = require('./libs/local').init(appconfig);
|
||||
global.db = require('./libs/mongo').init(appconfig);
|
||||
global.entries = require('./libs/entries').init(appconfig);
|
||||
global.git = require('./libs/git').init(appconfig, false);
|
||||
global.lang = require('i18next');
|
||||
global.mark = require('./libs/markdown');
|
||||
global.upl = require('./libs/uploads').init(appconfig);
|
||||
|
||||
// ----------------------------------------
|
||||
// Load modules
|
||||
// ----------------------------------------
|
||||
|
||||
var _ = require('lodash');
|
||||
var express = require('express');
|
||||
var path = require('path');
|
||||
var favicon = require('serve-favicon');
|
||||
var session = require('express-session');
|
||||
const mongoStore = require('connect-mongo')(session);
|
||||
var cookieParser = require('cookie-parser');
|
||||
var bodyParser = require('body-parser');
|
||||
var flash = require('connect-flash');
|
||||
var compression = require('compression');
|
||||
var passport = require('passport');
|
||||
var autoload = require('auto-load');
|
||||
var expressValidator = require('express-validator');
|
||||
var http = require('http');
|
||||
|
||||
global.lang = require('i18next');
|
||||
var i18next_backend = require('i18next-node-fs-backend');
|
||||
var i18next_mw = require('i18next-express-middleware');
|
||||
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');
|
||||
const path = require('path');
|
||||
const session = require('express-session');
|
||||
const sessionMongoStore = require('connect-mongo')(session);
|
||||
const socketio = require('socket.io');
|
||||
|
||||
var mw = autoload(path.join(ROOTPATH, '/middlewares'));
|
||||
var ctrl = autoload(path.join(ROOTPATH, '/controllers'));
|
||||
var libInternalAuth = require('./libs/internalAuth');
|
||||
|
||||
global.WSInternalKey = libInternalAuth.generateKey();
|
||||
|
||||
// ----------------------------------------
|
||||
// Define Express App
|
||||
// ----------------------------------------
|
||||
|
||||
global.app = express();
|
||||
app.use(compression());
|
||||
|
||||
// ----------------------------------------
|
||||
// Security
|
||||
@@ -65,15 +71,22 @@ global.app = express();
|
||||
app.use(mw.security);
|
||||
|
||||
// ----------------------------------------
|
||||
// Passport Authentication
|
||||
// Public Assets
|
||||
// ----------------------------------------
|
||||
|
||||
var strategy = require('./models/auth')(passport, appconfig);
|
||||
app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')));
|
||||
app.use(express.static(path.join(ROOTPATH, 'assets')));
|
||||
|
||||
// ----------------------------------------
|
||||
// Session
|
||||
// ----------------------------------------
|
||||
|
||||
var strategy = require('./libs/auth')(passport, appconfig);
|
||||
|
||||
app.use(cookieParser());
|
||||
app.use(session({
|
||||
name: 'requarkswiki.sid',
|
||||
store: new mongoStore({
|
||||
store: new sessionMongoStore({
|
||||
mongooseConnection: db.connection,
|
||||
touchAfter: 15
|
||||
}),
|
||||
@@ -109,22 +122,12 @@ lang
|
||||
// View Engine Setup
|
||||
// ----------------------------------------
|
||||
|
||||
app.use(compression());
|
||||
|
||||
app.use(i18next_mw.handle(lang));
|
||||
app.set('views', path.join(ROOTPATH, 'views'));
|
||||
app.set('view engine', 'pug');
|
||||
|
||||
app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')));
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: false }));
|
||||
app.use(expressValidator());
|
||||
|
||||
// ----------------------------------------
|
||||
// Public Assets
|
||||
// ----------------------------------------
|
||||
|
||||
app.use(express.static(path.join(ROOTPATH, 'assets')));
|
||||
|
||||
// ----------------------------------------
|
||||
// View accessible data
|
||||
@@ -149,14 +152,12 @@ app.use('/', ctrl.pages);
|
||||
// Error handling
|
||||
// ----------------------------------------
|
||||
|
||||
// catch 404 and forward to error handler
|
||||
app.use(function(req, res, next) {
|
||||
var err = new Error('Not Found');
|
||||
err.status = 404;
|
||||
next(err);
|
||||
});
|
||||
|
||||
// error handlers
|
||||
app.use(function(err, req, res, next) {
|
||||
res.status(err.status || 500);
|
||||
res.render('error', {
|
||||
@@ -169,10 +170,12 @@ app.use(function(err, req, res, next) {
|
||||
// Start HTTP server
|
||||
// ----------------------------------------
|
||||
|
||||
winston.info('[SERVER] Starting HTTP server on port ' + appconfig.port + '...');
|
||||
winston.info('[SERVER] Starting HTTP/WS server on port ' + appconfig.port + '...');
|
||||
|
||||
app.set('port', appconfig.port);
|
||||
var server = http.createServer(app);
|
||||
var io = socketio(server);
|
||||
|
||||
server.listen(appconfig.port);
|
||||
server.on('error', (error) => {
|
||||
if (error.syscall !== 'listen') {
|
||||
@@ -195,40 +198,21 @@ server.on('error', (error) => {
|
||||
});
|
||||
|
||||
server.on('listening', () => {
|
||||
winston.info('[SERVER] HTTP server started successfully! [RUNNING]');
|
||||
winston.info('[SERVER] HTTP/WS server started successfully! [RUNNING]');
|
||||
});
|
||||
|
||||
// ----------------------------------------
|
||||
// WebSocket handlers
|
||||
// ----------------------------------------
|
||||
|
||||
io.on('connection', ctrl.ws);
|
||||
|
||||
// ----------------------------------------
|
||||
// Start child processes
|
||||
// ----------------------------------------
|
||||
|
||||
var fork = require('child_process').fork,
|
||||
libInternalAuth = require('./lib/internalAuth');
|
||||
|
||||
global.WSInternalKey = libInternalAuth.generateKey();
|
||||
|
||||
var wsSrv = fork('ws-server.js', [WSInternalKey]),
|
||||
bgAgent = fork('agent.js', [WSInternalKey]);
|
||||
var bgAgent = fork('agent.js', [WSInternalKey]);
|
||||
|
||||
process.on('exit', (code) => {
|
||||
wsSrv.disconnect();
|
||||
bgAgent.disconnect();
|
||||
});
|
||||
|
||||
// ----------------------------------------
|
||||
// Connect to local WebSocket server
|
||||
// ----------------------------------------
|
||||
|
||||
var wsClient = require('socket.io-client');
|
||||
global.ws = wsClient('http://localhost:' + appconfig.wsPort, { reconnectionAttempts: 10 });
|
||||
|
||||
ws.on('connect', function () {
|
||||
winston.info('[SERVER] Connected to WebSocket server successfully!');
|
||||
});
|
||||
ws.on('connect_error', function () {
|
||||
winston.warn('[SERVER] Unable to connect to WebSocket server! Retrying...');
|
||||
});
|
||||
ws.on('reconnect_failed', function () {
|
||||
winston.error('[SERVER] Failed to reconnect to WebSocket server too many times! Stopping...');
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user