Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
({
host: '127.0.0.1',
transport: 'ws',
ports: [8000, 8001, 8002, 8003],
timeout: 5000,
concurrency: 1000,
Expand Down
24 changes: 10 additions & 14 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
}

Expand Down
12 changes: 8 additions & 4 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down