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.

194 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. let lcdata = require('./models/localdata');
  14. global.db = require('./models/db')(appconfig);
  15. global.git = require('./models/git').init(appconfig);
  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. global.ROOTPATH = __dirname;
  45. var _isDebug = (app.get('env') === 'development');
  46. // ----------------------------------------
  47. // Security
  48. // ----------------------------------------
  49. app.use(mw.security);
  50. // ----------------------------------------
  51. // Passport Authentication
  52. // ----------------------------------------
  53. var strategy = require('./models/auth')(passport, appconfig);
  54. app.use(cookieParser());
  55. app.use(session({
  56. name: 'requarkswiki.sid',
  57. store: new lokiStore({ path: path.join(appconfig.datadir.db, 'sessions.db') }),
  58. secret: appconfig.sessionSecret,
  59. resave: false,
  60. saveUninitialized: false
  61. }));
  62. app.use(flash());
  63. app.use(passport.initialize());
  64. app.use(passport.session());
  65. // ----------------------------------------
  66. // Localization Engine
  67. // ----------------------------------------
  68. lang
  69. .use(i18next_backend)
  70. .use(i18next_mw.LanguageDetector)
  71. .init({
  72. load: 'languageOnly',
  73. ns: ['common'],
  74. defaultNS: 'common',
  75. saveMissing: false,
  76. supportedLngs: ['en', 'fr'],
  77. preload: ['en', 'fr'],
  78. fallbackLng : 'en',
  79. backend: {
  80. loadPath: './locales/{{lng}}/{{ns}}.json'
  81. }
  82. });
  83. // ----------------------------------------
  84. // View Engine Setup
  85. // ----------------------------------------
  86. app.use(compression());
  87. app.use(i18next_mw.handle(lang));
  88. app.set('views', path.join(ROOTPATH, 'views'));
  89. app.set('view engine', 'pug');
  90. app.use(favicon(path.join(ROOTPATH, 'assets', 'favicon.ico')));
  91. app.use(bodyParser.json());
  92. app.use(bodyParser.urlencoded({ extended: false }));
  93. app.use(expressValidator());
  94. // ----------------------------------------
  95. // Public Assets
  96. // ----------------------------------------
  97. app.use(express.static(path.join(ROOTPATH, 'assets')));
  98. // ----------------------------------------
  99. // View accessible data
  100. // ----------------------------------------
  101. app.locals._ = require('lodash');
  102. app.locals.moment = require('moment');
  103. app.locals.appconfig = appconfig;
  104. //app.locals.appdata = require('./data.json');
  105. app.use(mw.flash);
  106. // ----------------------------------------
  107. // Controllers
  108. // ----------------------------------------
  109. app.use('/', ctrl.auth);
  110. app.use('/', ctrl.pages);
  111. app.use('/admin', mw.auth, ctrl.admin);
  112. // ----------------------------------------
  113. // Error handling
  114. // ----------------------------------------
  115. // catch 404 and forward to error handler
  116. app.use(function(req, res, next) {
  117. var err = new Error('Not Found');
  118. err.status = 404;
  119. next(err);
  120. });
  121. // error handlers
  122. app.use(function(err, req, res, next) {
  123. res.status(err.status || 500);
  124. res.render('error', {
  125. message: err.message,
  126. error: _isDebug ? err : {}
  127. });
  128. });
  129. // ----------------------------------------
  130. // Start HTTP server
  131. // ----------------------------------------
  132. winston.info('[SERVER] Requarks Wiki has initialized successfully.');
  133. winston.info('[SERVER] Starting HTTP server on port ' + appconfig.port + '...');
  134. app.set('port', appconfig.port);
  135. var server = http.createServer(app);
  136. server.listen(appconfig.port);
  137. server.on('error', (error) => {
  138. if (error.syscall !== 'listen') {
  139. throw error;
  140. }
  141. // handle specific listen errors with friendly messages
  142. switch (error.code) {
  143. case 'EACCES':
  144. console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
  145. process.exit(1);
  146. break;
  147. case 'EADDRINUSE':
  148. console.error('Port ' + appconfig.port + ' is already in use!');
  149. process.exit(1);
  150. break;
  151. default:
  152. throw error;
  153. }
  154. });
  155. server.on('listening', () => {
  156. winston.info('[SERVER] HTTP server started successfully! [RUNNING]');
  157. });