Advertisement
Guest User

Untitled

a guest
Jul 4th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. //
  2. // Load dependencies
  3. //
  4. const express = require('express'),
  5. server = express(),
  6. mysql = require('mysql'),
  7. fs = require('fs'),
  8. http = require('http'),
  9. https = require('https'),
  10. ssl = {key: fs.readFileSync('./ssl/ssl.pk', 'utf8'), cert: fs.readFileSync('./ssl/ssl.crt', 'utf8')},
  11. ws = require('ws'),
  12. dgram = require('dgram'),
  13. udp = dgram.createSocket('udp4'),
  14. chalk = require('chalk');
  15.  
  16. //
  17. // Initialize servers
  18. //
  19. module.exports = function(app){
  20. // Static resources directory
  21. app.express = server;
  22. app.express.use(express.static('web'));
  23. // Database setup
  24. app.db = mysql.createConnection({
  25. host: app.config.database.host || 'localhost',
  26. user: app.config.database.user || 'root',
  27. password: app.config.database.password || '',
  28. database: app.config.database.name || 'angstrom',
  29. port : app.config.database.port || false
  30. });
  31. // Servers setup
  32. /// HTTPS server initialization
  33. app.https = https.createServer(ssl,app.express);
  34. app.https.on('listening',_=>{
  35. /// WebSocket server initialization
  36. app.ws = new ws.Server({server:app.https});
  37. console.log(chalk.green(":445\tHTTPS\tUP"));
  38. console.log(chalk.green(":445\tWSS\tUP"));
  39. });
  40. app.https.on('restart', _ => setTimeout( _ => app.https.listen(445) , 1200 ));
  41. app.https.on('error',error=>{
  42. app.https.close().emit('restart');
  43. })
  44. /// HTTP server initialization
  45. app.http = http.createServer(app.express);
  46. app.http.on('listening',_=>{
  47. console.log(chalk.green(":80\tHTTP\tUP"));
  48. });
  49. app.http.on('restart', _ => setTimeout( _ => app.http.listen(80), 1200 ) );
  50. app.http.on('error',error=>{
  51. console.log(chalk.red(error))
  52. app.http.close().emit('restart');
  53. })
  54. /// UDP server initialization
  55. app.udp = udp.on('listening', _ => {
  56. console.log(chalk.green(`:${udp.address().port}\tUDP\tUP`));
  57. });
  58. app.udp.on('restart', _ => setTimeout( _ => app.udp.bind(0) ) );
  59. app.udp.on('error',error=>{
  60. console.log(chalk.red(error));
  61. app.udp.close().emit('restart');
  62. })
  63. /// Run servers
  64. app.http.emit('restart');
  65. app.https.emit('restart');
  66. app.udp.emit('restart');
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement