Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Load dependencies
- //
- const express = require('express'),
- server = express(),
- mysql = require('mysql'),
- fs = require('fs'),
- http = require('http'),
- https = require('https'),
- ssl = {key: fs.readFileSync('./ssl/ssl.pk', 'utf8'), cert: fs.readFileSync('./ssl/ssl.crt', 'utf8')},
- ws = require('ws'),
- dgram = require('dgram'),
- udp = dgram.createSocket('udp4'),
- chalk = require('chalk');
- //
- // Initialize servers
- //
- module.exports = function(app){
- // Static resources directory
- app.express = server;
- app.express.use(express.static('web'));
- // Database setup
- app.db = mysql.createConnection({
- host: app.config.database.host || 'localhost',
- user: app.config.database.user || 'root',
- password: app.config.database.password || '',
- database: app.config.database.name || 'angstrom',
- port : app.config.database.port || false
- });
- // Servers setup
- /// HTTPS server initialization
- app.https = https.createServer(ssl,app.express);
- app.https.on('listening',_=>{
- /// WebSocket server initialization
- app.ws = new ws.Server({server:app.https});
- console.log(chalk.green(":445\tHTTPS\tUP"));
- console.log(chalk.green(":445\tWSS\tUP"));
- });
- app.https.on('restart', _ => setTimeout( _ => app.https.listen(445) , 1200 ));
- app.https.on('error',error=>{
- app.https.close().emit('restart');
- })
- /// HTTP server initialization
- app.http = http.createServer(app.express);
- app.http.on('listening',_=>{
- console.log(chalk.green(":80\tHTTP\tUP"));
- });
- app.http.on('restart', _ => setTimeout( _ => app.http.listen(80), 1200 ) );
- app.http.on('error',error=>{
- console.log(chalk.red(error))
- app.http.close().emit('restart');
- })
- /// UDP server initialization
- app.udp = udp.on('listening', _ => {
- console.log(chalk.green(`:${udp.address().port}\tUDP\tUP`));
- });
- app.udp.on('restart', _ => setTimeout( _ => app.udp.bind(0) ) );
- app.udp.on('error',error=>{
- console.log(chalk.red(error));
- app.udp.close().emit('restart');
- })
- /// Run servers
- app.http.emit('restart');
- app.https.emit('restart');
- app.udp.emit('restart');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement