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.

193 lines
5.2 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. var lcdata = require('./models/localdata')(appconfig);
  14. global.db = require('./models/loki')(appconfig);
  15. global.git = require('./models/git').init(appconfig);
  16. global.mark = require('./models/markdown');
  17. // ----------------------------------------
  18. // Load modules
  19. // ----------------------------------------
  20. var _ = require('lodash');
  21. var express = require('express');
  22. var path = require('path');
  23. var favicon = require('serve-favicon');
  24. var session = require('express-session');
  25. var lokiStore = require('connect-loki')(session);
  26. var cookieParser = require('cookie-parser');
  27. var bodyParser = require('body-parser');
  28. var flash = require('connect-flash');
  29. var compression = require('compression');
  30. var passport = require('passport');
  31. var autoload = require('auto-load');
  32. var expressValidator = require('express-validator');
  33. var http = require('http');
  34. global.lang = require('i18next');
  35. var i18next_backend = require('i18next-node-fs-backend');
  36. var i18next_mw = require('i18next-express-middleware');
  37. var mw = autoload(path.join(ROOTPATH, '/middlewares'));
  38. var ctrl = autoload(path.join(ROOTPATH, '/controllers'));
  39. // ----------------------------------------
  40. // Define Express App
  41. // ----------------------------------------
  42. global.app = express();
  43. global.ROOTPATH = __dirname;
  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.locals.appdata = require('./data.json');
  104. app.use(mw.flash);
  105. // ----------------------------------------
  106. // Controllers
  107. // ----------------------------------------
  108. app.use('/', ctrl.auth);
  109. app.use('/', ctrl.pages);
  110. app.use('/admin', mw.auth, ctrl.admin);
  111. // ----------------------------------------
  112. // Error handling
  113. // ----------------------------------------
  114. // catch 404 and forward to error handler
  115. app.use(function(req, res, next) {
  116. var err = new Error('Not Found');
  117. err.status = 404;
  118. next(err);
  119. });
  120. // error handlers
  121. app.use(function(err, req, res, next) {
  122. res.status(err.status || 500);
  123. res.render('error', {
  124. message: err.message,
  125. error: _isDebug ? err : {}
  126. });
  127. });
  128. // ----------------------------------------
  129. // Start HTTP server
  130. // ----------------------------------------
  131. winston.info('[SERVER] Requarks Wiki has initialized successfully.');
  132. winston.info('[SERVER] Starting HTTP server on port ' + appconfig.port + '...');
  133. app.set('port', appconfig.port);
  134. var server = http.createServer(app);
  135. server.listen(appconfig.port);
  136. server.on('error', (error) => {
  137. if (error.syscall !== 'listen') {
  138. throw error;
  139. }
  140. // handle specific listen errors with friendly messages
  141. switch (error.code) {
  142. case 'EACCES':
  143. console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
  144. process.exit(1);
  145. break;
  146. case 'EADDRINUSE':
  147. console.error('Port ' + appconfig.port + ' is already in use!');
  148. process.exit(1);
  149. break;
  150. default:
  151. throw error;
  152. }
  153. });
  154. server.on('listening', () => {
  155. winston.info('[SERVER] HTTP server started successfully! [RUNNING]');
  156. });