diff --git a/config/server.js b/config/server.js index b7c0e71..b033da9 100644 --- a/config/server.js +++ b/config/server.js @@ -1,6 +1,5 @@ ({ host: '127.0.0.1', - transport: 'ws', ports: [8000, 8001, 8002, 8003], timeout: 5000, concurrency: 1000, diff --git a/lib/server.js b/lib/server.js index 9052425..f7a9f98 100644 --- a/lib/server.js +++ b/lib/server.js @@ -5,8 +5,6 @@ const { http, https, path, worker } = require('./dependencies.js'); const WebSocket = require('ws'); const Semaphore = require('./semaphore.js'); -const TRANSPORT = { http, https, ws: http, wss: https }; - const MIME_TYPES = { html: 'text/html; charset=UTF-8', json: 'application/json; charset=UTF-8', @@ -156,23 +154,21 @@ class Server { constructor(config, application) { this.config = config; this.application = application; - const { ports, host, transport, concurrency, queue } = config; + const { ports, host, concurrency, queue } = config; this.semaphore = new Semaphore(concurrency, queue.size, queue.timeout); const { threadId } = worker; const port = ports[threadId - 1]; - const lib = TRANSPORT[transport]; const handler = listener(application); - this.instance = lib.createServer({ ...application.cert }, handler); - if (transport.startsWith('ws')) { - this.ws = new WebSocket.Server({ server: this.instance }); - this.ws.on('connection', (connection, req) => { - const client = new Client(req, null, application, connection); - connection.on('message', message => { - const { method, args } = JSON.parse(message); - client.rpc(method, args); - }); + const transport = threadId === 1 ? http : https; + this.instance = transport.createServer({ ...application.cert }, handler); + this.ws = new WebSocket.Server({ server: this.instance }); + this.ws.on('connection', (connection, req) => { + const client = new Client(req, null, application, connection); + connection.on('message', message => { + const { method, args } = JSON.parse(message); + client.rpc(method, args); }); - } + }); this.instance.listen(port, host); } diff --git a/worker.js b/worker.js index 133dc63..147cea1 100644 --- a/worker.js +++ b/worker.js @@ -22,10 +22,14 @@ const startMonitoring = require('./init/logResources.js'); const logger = await new Logger(logPath, threadId); const application = new Application(); const certPath = path.join(PATH, 'cert'); - const key = await fsp.readFile(path.join(certPath, 'key.pem')); - const cert = await fsp.readFile(path.join(certPath, 'cert.pem')); - Object.assign(application, { config, logger, cert }); - application.cert = { key, cert }; + Object.assign(application, { config, logger }); + try { + const key = await fsp.readFile(path.join(certPath, 'key.pem')); + const cert = await fsp.readFile(path.join(certPath, 'cert.pem')); + application.cert = { key, cert }; + } catch { + if (threadId === 1) logger.log('Can not load TLS certificates'); + } application.db = new Database(config.sections.database, application); application.server = new Server(config.sections.server, application); application.auth = initAuth(application);