From 55e7c86b3bff6da0ae0169c77ebef070a7febdce Mon Sep 17 00:00:00 2001 From: Henry Tsai Date: Fri, 28 Jun 2024 11:06:01 -0700 Subject: [PATCH] Introduced generalized base URL --- README.md | 1 + src/config.ts | 13 +++++++------ src/http-api.ts | 5 +++-- src/web5-connect/web5-connect-server.ts | 3 ++- tests/http-api.spec.ts | 1 + 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cf5eca0..bb8d80d 100644 --- a/README.md +++ b/README.md @@ -281,6 +281,7 @@ Configuration can be set using environment variables | `DS_PORT` | Port that the server listens on | `3000` | | `DS_MAX_RECORD_DATA_SIZE` | Maximum size for `RecordsWrite` data. use `b`, `kb`, `mb`, `gb` for value | `1gb` | | `DS_WEBSOCKET_SERVER` | Whether to enable listening over `ws:`. values: `on`,`off` | `on` | +| `DWN_BASE_URL` | Base external URL of this DWN. Used to construct URL paths such as the `Request URI` for the Web5 Connect flow. | http://localhost | | `DWN_REGISTRATION_STORE_URL` | URL to use for storage of registered DIDs. Leave unset to if DWN does not require registration (ie. open for all) | unset | | `DWN_REGISTRATION_PROOF_OF_WORK_SEED` | Seed to generate the challenge nonce from, this allows all DWN instances in a cluster to generate the same challenge. | unset | | `DWN_REGISTRATION_PROOF_OF_WORK_ENABLED` | Require new users to complete a proof-of-work challenge | `false` | diff --git a/src/config.ts b/src/config.ts index 6aa87b2..320c5e1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -10,6 +10,13 @@ export const config = { * otherwise we fall back on the use defined `DWN_SERVER_PACKAGE_NAME` or `@web5/dwn-server`. */ serverName: process.env.npm_package_name || process.env.DWN_SERVER_PACKAGE_NAME || '@web5/dwn-server', + + /** + * The base external URL of this DWN. + * This is used to construct URL paths such as the `Request URI` in the Web5 Connect flow. + */ + baseUrl: process.env.DWN_BASE_URL || 'http://localhost', + /** * Used to populate the `version` and `sdkVersion` properties returned by the `/info` endpoint. * @@ -39,10 +46,4 @@ export const config = { // log level - trace/debug/info/warn/error logLevel: process.env.DWN_SERVER_LOG_LEVEL || 'INFO', - - /** - * The base URL of the connect server excluding the port (port will be appended by using the `port` param in this config), - * this is used to construct the full Web5 Connect Request URI for the Identity Provider (wallet) to use to fetch the Web5 Connect Request object. - */ - web5ConnectServerBaseUrl: process.env.WEB5_CONNECT_SERVER_BASE_URL || 'http://localhost', }; diff --git a/src/http-api.ts b/src/http-api.ts index c19cd8c..9a4a533 100644 --- a/src/http-api.ts +++ b/src/http-api.ts @@ -57,9 +57,9 @@ export class HttpApi { this.registrationManager = registrationManager; } - // setup the Web5 Connect Server + // create the Web5 Connect Server this.web5ConnectServer = new Web5ConnectServer({ - baseUrl: `${config.web5ConnectServerBaseUrl}:${config.port}`, + baseUrl: `${config.baseUrl}:${config.port}`, }); this.#setupMiddleware(); @@ -320,6 +320,7 @@ export class HttpApi { } res.json({ + url : config.baseUrl, server : this.#packageInfo.server, maxFileSize : config.maxRecordDataSize, registrationRequirements : registrationRequirements, diff --git a/src/web5-connect/web5-connect-server.ts b/src/web5-connect/web5-connect-server.ts index 18119ce..a0b7405 100644 --- a/src/web5-connect/web5-connect-server.ts +++ b/src/web5-connect/web5-connect-server.ts @@ -35,7 +35,8 @@ export class Web5ConnectServer { /** * Creates a new instance of the Web5 Connect Server. - * @param params.baseUrl The the base URL of the connect server including the port, this is used to construct the full request URI. + * @param params.baseUrl The the base URL of the connect server including the port. + * This is given to the Identity Provider (wallet) to fetch the Web5 Connect Request object. */ public constructor({ baseUrl }: { baseUrl: string; diff --git a/tests/http-api.spec.ts b/tests/http-api.spec.ts index 5d59638..164ec06 100644 --- a/tests/http-api.spec.ts +++ b/tests/http-api.spec.ts @@ -1042,6 +1042,7 @@ describe('http api', function () { expect(resp.status).to.equal(200); const info = await resp.json(); + expect(info['url']).to.equal('http://localhost'); expect(info['server']).to.equal('@web5/dwn-server'); expect(info['registrationRequirements']).to.include('terms-of-service'); expect(info['registrationRequirements']).to.include(