Skip to content

Commit 29bf37c

Browse files
committed
Remove config parameter: server.transport
Closes: #113 PR-URL: #115
1 parent b61e619 commit 29bf37c

3 files changed

Lines changed: 18 additions & 19 deletions

File tree

config/server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
({
22
host: '127.0.0.1',
3-
transport: 'ws',
43
ports: [8000, 8001, 8002, 8003],
54
timeout: 5000,
65
concurrency: 1000,

lib/server.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const { http, https, path, worker } = require('./dependencies.js');
55
const WebSocket = require('ws');
66
const Semaphore = require('./semaphore.js');
77

8-
const TRANSPORT = { http, https, ws: http, wss: https };
9-
108
const MIME_TYPES = {
119
html: 'text/html; charset=UTF-8',
1210
json: 'application/json; charset=UTF-8',
@@ -156,23 +154,21 @@ class Server {
156154
constructor(config, application) {
157155
this.config = config;
158156
this.application = application;
159-
const { ports, host, transport, concurrency, queue } = config;
157+
const { ports, host, concurrency, queue } = config;
160158
this.semaphore = new Semaphore(concurrency, queue.size, queue.timeout);
161159
const { threadId } = worker;
162160
const port = ports[threadId - 1];
163-
const lib = TRANSPORT[transport];
164161
const handler = listener(application);
165-
this.instance = lib.createServer({ ...application.cert }, handler);
166-
if (transport.startsWith('ws')) {
167-
this.ws = new WebSocket.Server({ server: this.instance });
168-
this.ws.on('connection', (connection, req) => {
169-
const client = new Client(req, null, application, connection);
170-
connection.on('message', message => {
171-
const { method, args } = JSON.parse(message);
172-
client.rpc(method, args);
173-
});
162+
const transport = threadId === 1 ? http : https;
163+
this.instance = transport.createServer({ ...application.cert }, handler);
164+
this.ws = new WebSocket.Server({ server: this.instance });
165+
this.ws.on('connection', (connection, req) => {
166+
const client = new Client(req, null, application, connection);
167+
connection.on('message', message => {
168+
const { method, args } = JSON.parse(message);
169+
client.rpc(method, args);
174170
});
175-
}
171+
});
176172
this.instance.listen(port, host);
177173
}
178174

worker.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ const startMonitoring = require('./init/logResources.js');
2222
const logger = await new Logger(logPath, threadId);
2323
const application = new Application();
2424
const certPath = path.join(PATH, 'cert');
25-
const key = await fsp.readFile(path.join(certPath, 'key.pem'));
26-
const cert = await fsp.readFile(path.join(certPath, 'cert.pem'));
27-
Object.assign(application, { config, logger, cert });
28-
application.cert = { key, cert };
25+
Object.assign(application, { config, logger });
26+
try {
27+
const key = await fsp.readFile(path.join(certPath, 'key.pem'));
28+
const cert = await fsp.readFile(path.join(certPath, 'cert.pem'));
29+
application.cert = { key, cert };
30+
} catch {
31+
if (threadId === 1) logger.log('Can not load TLS certificates');
32+
}
2933
application.db = new Database(config.sections.database, application);
3034
application.server = new Server(config.sections.server, application);
3135
application.auth = initAuth(application);

0 commit comments

Comments
 (0)