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.

142 lines
3.6 KiB

  1. // ===========================================
  2. // REQUARKS WIKI - WebSocket Server
  3. // 1.0.0
  4. // Licensed under AGPLv3
  5. // ===========================================
  6. global.ROOTPATH = __dirname;
  7. // ----------------------------------------
  8. // Load Winston
  9. // ----------------------------------------
  10. var _isDebug = process.env.NODE_ENV === 'development';
  11. global.winston = require('winston');
  12. winston.remove(winston.transports.Console)
  13. winston.add(winston.transports.Console, {
  14. level: (_isDebug) ? 'info' : 'warn',
  15. prettyPrint: true,
  16. colorize: true,
  17. silent: false,
  18. timestamp: true
  19. });
  20. // ----------------------------------------
  21. // Fetch internal handshake key
  22. // ----------------------------------------
  23. if(!process.argv[2] || process.argv[2].length !== 40) {
  24. winston.error('[WS] Illegal process start. Missing handshake key.');
  25. process.exit(1);
  26. }
  27. global.internalAuth = require('./lib/internalAuth').init(process.argv[2]);;
  28. // ----------------------------------------
  29. // Load modules
  30. // ----------------------------------------
  31. winston.info('[WS] WS Server is initializing...');
  32. var appconfig = require('./models/config')('./config.yml');
  33. global.entries = require('./models/entries').init(appconfig);
  34. global.mark = require('./models/markdown');
  35. global.search = require('./models/search').init(appconfig);
  36. // ----------------------------------------
  37. // Load modules
  38. // ----------------------------------------
  39. var _ = require('lodash');
  40. var express = require('express');
  41. var path = require('path');
  42. var http = require('http');
  43. var socketio = require('socket.io');
  44. var moment = require('moment');
  45. // ----------------------------------------
  46. // Define Express App
  47. // ----------------------------------------
  48. global.app = express();
  49. // ----------------------------------------
  50. // Controllers
  51. // ----------------------------------------
  52. app.get('/', function(req, res){
  53. res.send('Requarks Wiki WebSocket server');
  54. });
  55. // ----------------------------------------
  56. // Start WebSocket server
  57. // ----------------------------------------
  58. winston.info('[SERVER] Starting WebSocket server on port ' + appconfig.wsPort + '...');
  59. app.set('port', appconfig.wsPort);
  60. var server = http.Server(app);
  61. var io = socketio(server);
  62. server.on('error', (error) => {
  63. if (error.syscall !== 'listen') {
  64. throw error;
  65. }
  66. switch (error.code) {
  67. case 'EACCES':
  68. console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
  69. process.exit(1);
  70. break;
  71. case 'EADDRINUSE':
  72. console.error('Port ' + appconfig.port + ' is already in use!');
  73. process.exit(1);
  74. break;
  75. default:
  76. throw error;
  77. }
  78. });
  79. server.listen(appconfig.wsPort, () => {
  80. winston.info('[WS] WebSocket server started successfully! [RUNNING]');
  81. });
  82. io.on('connection', (socket) => {
  83. socket.on('searchAdd', (data) => {
  84. if(internalAuth.validateKey(data.auth)) {
  85. search.add(data.content);
  86. }
  87. });
  88. socket.on('searchDel', (data, cb) => {
  89. if(internalAuth.validateKey(data.auth)) {
  90. search.delete(data.entryPath);
  91. }
  92. });
  93. socket.on('search', (data, cb) => {
  94. search.find(data.terms).then((results) => {
  95. cb(results);
  96. });
  97. });
  98. });
  99. /*setTimeout(() => {
  100. search._si.searchAsync({ query: { AND: [{'*': ['unit']}] }}).then((stuff) => { console.log(stuff.hits); });
  101. }, 8000);*/
  102. // ----------------------------------------
  103. // Shutdown gracefully
  104. // ----------------------------------------
  105. process.on('disconnect', () => {
  106. winston.warn('[WS] Lost connection to main server. Exiting... [' + moment().toISOString() + ']');
  107. server.close();
  108. process.exit();
  109. });
  110. process.on('exit', () => {
  111. server.stop();
  112. });