Skip to content

Commit

Permalink
feat: enabled configurable server host for the Relay server (#3073) (#…
Browse files Browse the repository at this point in the history
…3074)

* feat: enabled configurable server host for the Relay server (#3073)

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
Signed-off-by: ebadiere <ebadiere@gmail.com>

* fix: reverted to relayServer before cherry pick

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>

---------

Signed-off-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
Signed-off-by: ebadiere <ebadiere@gmail.com>
Co-authored-by: Logan Nguyen <logan.nguyen@swirldslabs.com>
  • Loading branch information
ebadiere and quiet-node authored Oct 7, 2024
1 parent 85952b2 commit 91c2a2d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 39 deletions.
72 changes: 37 additions & 35 deletions docs/configuration.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/relay/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export default {
WEB_SOCKET_HTTP_PORT: process.env.WEB_SOCKET_HTTP_PORT || 8547,

RELAY_PORT: process.env.SERVER_PORT || 7546,
RELAY_HOST: process.env.SERVER_HOST || 'localhost',

FUNCTION_SELECTOR_CHAR_LENGTH: 10,
MIRROR_NODE_RETRY_DELAY_DEFAULT: 2000,
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import app from './server';
import { setServerTimeout } from './koaJsonRpc/lib/utils'; // Import the 'setServerTimeout' function from the correct location

async function main() {
const server = await app.listen({ port: process.env.SERVER_PORT || 7546 });
const server = app.listen({ port: process.env.SERVER_PORT || 7546, host: process.env.SERVER_HOST });

// set request timeout to ensure sockets are closed after specified time of inactivity
setServerTimeout(server);
Expand Down
2 changes: 2 additions & 0 deletions packages/server/tests/acceptance/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ describe('RPC Server Acceptance Tests', function () {
// start local relay, relay instance in local should not be running

logger.info(`Start relay on port ${constants.RELAY_PORT}`);
logger.info(`Start relay on host ${constants.RELAY_HOST}`);
relayServer = app.listen({ port: constants.RELAY_PORT });

setServerTimeout(relayServer);

if (process.env.TEST_WS_SERVER === 'true') {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/tests/acceptance/serverConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('@server-config Server Configuration Options Coverage', function () {
describe('Koa Server Timeout', () => {
it('should timeout a request after the specified time', async () => {
const requestTimeoutMs: number = parseInt(process.env.SERVER_REQUEST_TIMEOUT_MS || '3000');
const host = 'localhost';
const host = process.env.SERVER_HOST || 'localhost';
const port = parseInt(process.env.SERVER_PORT || '7546');
const method = 'eth_blockNumber';
const params: any[] = [];
Expand Down
29 changes: 29 additions & 0 deletions packages/server/tests/integration/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ describe('RPC Server', function () {

this.timeout(5000);

it('should verify that the server is running with the correct host and port', async function () {
const CUSTOMIZE_PORT = '7545';
const CUSTOMIZE_HOST = '127.0.0.1';
const configuredServer = app.listen({ port: CUSTOMIZE_PORT, host: CUSTOMIZE_HOST });

return new Promise<void>((resolve, reject) => {
configuredServer.on('listening', () => {
const address = configuredServer.address();

try {
expect(address).to.not.be.null;
if (address && typeof address === 'object') {
expect(address.address).to.equal(CUSTOMIZE_HOST);
expect(address.port.toString()).to.equal(CUSTOMIZE_PORT);
} else {
throw new Error('Server address is not an object');
}
configuredServer.close(() => resolve());
} catch (error) {
configuredServer.close(() => reject(error));
}
});

configuredServer.on('error', (error) => {
reject(error);
});
});
});

it('should execute "eth_chainId"', async function () {
const res = await testClient.post('/', {
id: '2',
Expand Down
5 changes: 3 additions & 2 deletions packages/ws-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { app, httpApp } from './webSocketServer';
import constants from '@hashgraph/json-rpc-relay/dist/lib/constants';

async function main() {
app.listen({ port: constants.WEB_SOCKET_PORT });
httpApp.listen({ port: constants.WEB_SOCKET_HTTP_PORT });
const host = process.env.SERVER_HOST;
app.listen({ port: constants.WEB_SOCKET_PORT, host });
httpApp.listen({ port: constants.WEB_SOCKET_HTTP_PORT, host });
}

(async () => {
Expand Down

0 comments on commit 91c2a2d

Please sign in to comment.