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.

161 lines
4.0 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. let lcdata = require('./models/localdata').init(appconfig, true);
  34. global.entries = require('./models/entries').init(appconfig);
  35. global.mark = require('./models/markdown');
  36. global.search = require('./models/search').init(appconfig);
  37. // ----------------------------------------
  38. // Load modules
  39. // ----------------------------------------
  40. var _ = require('lodash');
  41. var express = require('express');
  42. var path = require('path');
  43. var http = require('http');
  44. var socketio = require('socket.io');
  45. var moment = require('moment');
  46. // ----------------------------------------
  47. // Define Express App
  48. // ----------------------------------------
  49. global.app = express();
  50. // ----------------------------------------
  51. // Controllers
  52. // ----------------------------------------
  53. app.get('/', function(req, res){
  54. res.send('Requarks Wiki WebSocket server');
  55. });
  56. // ----------------------------------------
  57. // Start WebSocket server
  58. // ----------------------------------------
  59. winston.info('[SERVER] Starting WebSocket server on port ' + appconfig.wsPort + '...');
  60. app.set('port', appconfig.wsPort);
  61. var server = http.Server(app);
  62. var io = socketio(server);
  63. server.on('error', (error) => {
  64. if (error.syscall !== 'listen') {
  65. throw error;
  66. }
  67. switch (error.code) {
  68. case 'EACCES':
  69. console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
  70. process.exit(1);
  71. break;
  72. case 'EADDRINUSE':
  73. console.error('Port ' + appconfig.port + ' is already in use!');
  74. process.exit(1);
  75. break;
  76. default:
  77. throw error;
  78. }
  79. });
  80. server.listen(appconfig.wsPort, () => {
  81. winston.info('[WS] WebSocket server started successfully! [RUNNING]');
  82. });
  83. io.on('connection', (socket) => {
  84. //-----------------------------------------
  85. // SEARCH
  86. //-----------------------------------------
  87. socket.on('searchAdd', (data) => {
  88. if(internalAuth.validateKey(data.auth)) {
  89. search.add(data.content);
  90. }
  91. });
  92. socket.on('searchDel', (data, cb) => {
  93. if(internalAuth.validateKey(data.auth)) {
  94. search.delete(data.entryPath);
  95. }
  96. });
  97. socket.on('search', (data, cb) => {
  98. search.find(data.terms).then((results) => {
  99. cb(results);
  100. });
  101. });
  102. //-----------------------------------------
  103. // UPLOADS
  104. //-----------------------------------------
  105. socket.on('uploadsSetFolders', (data, cb) => {
  106. if(internalAuth.validateKey(data.auth)) {
  107. lcdata.setUploadsFolders(data.content);
  108. }
  109. });
  110. socket.on('uploadsGetFolders', (data, cb) => {
  111. cb(lcdata.getUploadsFolders());
  112. });
  113. socket.on('uploadsGetImages', (data, cb) => {
  114. cb([]);
  115. });
  116. });
  117. // ----------------------------------------
  118. // Shutdown gracefully
  119. // ----------------------------------------
  120. process.on('disconnect', () => {
  121. winston.warn('[WS] Lost connection to main server. Exiting... [' + moment().toISOString() + ']');
  122. server.close();
  123. process.exit();
  124. });
  125. process.on('exit', () => {
  126. server.stop();
  127. });