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.

203 lines
5.4 KiB

  1. // ===========================================
  2. // REQUARKS WIKI
  3. // 1.0.0
  4. // Licensed under AGPLv3
  5. // ===========================================
  6. global.ROOTPATH = __dirname;
  7. // ----------------------------------------
  8. // Load global modules
  9. // ----------------------------------------
  10. global.winston = require('winston');
  11. winston.info('[SERVER] Requarks Wiki is initializing...');
  12. var appconfig = require('./models/config')('./config.yml');
  13. let lcdata = require('./models/localdata');
  14. global.db = require('./models/db')(appconfig);
  15. global.git = require('./models/git').init(appconfig, false);
  16. global.entries = require('./models/entries').init(appconfig);
  17. global.mark = require('./models/markdown');
  18. // ----------------------------------------
  19. // Load modules
  20. // ----------------------------------------
  21. var _ = require('lodash');
  22. var express = require('express');
  23. var path = require('path');
  24. var favicon = require('serve-favicon');
  25. var session = require('express-session');
  26. var lokiStore = require('connect-loki')(session);
  27. var cookieParser = require('cookie-parser');
  28. var bodyParser = require('body-parser');
  29. var flash = require('connect-flash');
  30. var compression = require('compression');
  31. var passport = require('passport');
  32. var autoload = require('auto-load');
  33. var expressValidator = require('express-validator');
  34. var http = require('http');
  35. global.lang = require('i18next');
  36. var i18next_backend = require('i18next-node-fs-backend');
  37. var i18next_mw = require('i18next-express-middleware');
  38. var mw = autoload(path.join(ROOTPATH, '/middlewares'));
  39. var ctrl = autoload(path.join(ROOTPATH, '/controllers'));
  40. // ----------------------------------------
  41. // Define Express App
  42. // ----------------------------------------
  43. global.app = express();
  44. var _isDebug = (app.get('env') === 'development');
  45. // ----------------------------------------
  46. // Security
  47. // ----------------------------------------
  48. app.use(mw.security);
  49. // ----------------------------------------
  50. // Passport Authentication
  51. // ----------------------------------------
  52. var strategy = require('./models/auth')(passport, appconfig);
  53. app.use(cookieParser());
  54. app.use(session({
  55. name: 'requarkswiki.sid',
  56. store: new lokiStore({ path: path.join(appconfig.datadir.db, 'sessions.db') }),
  57. secret: appconfig.sessionSecret,
  58. resave: false,
  59. saveUninitialized: false
  60. }));
  61. app.use(flash());
  62. app.use(passport.initialize());
  63. app.use(passport.session());
  64. // ----------------------------------------
  65. // Localization Engine
  66. // ----------------------------------------
  67. lang
  68. .use(i18next_backend)
  69. .use(i18next_mw.LanguageDetector)
  70. .init({
  71. load: 'languageOnly',
  72. ns: ['common'],
  73. defaultNS: 'common',
  74. saveMissing: false,
  75. supportedLngs: ['en', 'fr'],
  76. preload: ['en', 'fr'],
  77. fallbackLng : 'en',
  78. backend: {
  79. loadPath: './locales/{{lng}}/{{ns}}.json'
  80. }
  81. });
  82. // ----------------------------------------
  83. // View Engine Setup
  84. // ----------------------------------------
  85. app.use(compression());
  86. app.use(i18next_mw.handle(lang));
  87. app.set('views', path.join(ROOTPATH, 'views'));
  88. app.set('view engine', 'pug');
  89. app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')));
  90. app.use(bodyParser.json());
  91. app.use(bodyParser.urlencoded({ extended: false }));
  92. app.use(expressValidator());
  93. // ----------------------------------------
  94. // Public Assets
  95. // ----------------------------------------
  96. app.use(express.static(path.join(ROOTPATH, 'assets')));
  97. // ----------------------------------------
  98. // View accessible data
  99. // ----------------------------------------
  100. app.locals._ = require('lodash');
  101. app.locals.moment = require('moment');
  102. app.locals.appconfig = appconfig;
  103. app.use(mw.flash);
  104. // ----------------------------------------
  105. // Controllers
  106. // ----------------------------------------
  107. app.use('/', ctrl.auth);
  108. app.use('/', ctrl.pages);
  109. app.use('/admin', mw.auth, ctrl.admin);
  110. // ----------------------------------------
  111. // Error handling
  112. // ----------------------------------------
  113. // catch 404 and forward to error handler
  114. app.use(function(req, res, next) {
  115. var err = new Error('Not Found');
  116. err.status = 404;
  117. next(err);
  118. });
  119. // error handlers
  120. app.use(function(err, req, res, next) {
  121. res.status(err.status || 500);
  122. res.render('error', {
  123. message: err.message,
  124. error: _isDebug ? err : {}
  125. });
  126. });
  127. // ----------------------------------------
  128. // Start HTTP server
  129. // ----------------------------------------
  130. winston.info('[SERVER] Requarks Wiki has initialized successfully.');
  131. winston.info('[SERVER] Starting HTTP server on port ' + appconfig.port + '...');
  132. app.set('port', appconfig.port);
  133. var server = http.createServer(app);
  134. server.listen(appconfig.port);
  135. server.on('error', (error) => {
  136. if (error.syscall !== 'listen') {
  137. throw error;
  138. }
  139. // handle specific listen errors with friendly messages
  140. switch (error.code) {
  141. case 'EACCES':
  142. console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
  143. process.exit(1);
  144. break;
  145. case 'EADDRINUSE':
  146. console.error('Port ' + appconfig.port + ' is already in use!');
  147. process.exit(1);
  148. break;
  149. default:
  150. throw error;
  151. }
  152. });
  153. server.on('listening', () => {
  154. winston.info('[SERVER] HTTP server started successfully! [RUNNING]');
  155. });
  156. // ----------------------------------------
  157. // Start Agents
  158. // ----------------------------------------
  159. var fork = require('child_process').fork;
  160. var bgAgent = fork('agent.js');
  161. process.on('exit', (code) => {
  162. bgAgent.disconnect();
  163. });