From a169d21f7b1b75a2ac2855cd0c899f7530c2c93e Mon Sep 17 00:00:00 2001 From: Darshan Date: Sat, 19 Apr 2025 11:59:17 +0530 Subject: [PATCH 1/3] fix: multi-region api access with custom-domains! --- src/lib/stores/sdk.ts | 14 ++++---------- src/lib/system.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index b90f08e3d..fd9204e3f 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -1,4 +1,4 @@ -import { VARS } from '$lib/system'; +import { isCloudHostname, VARS } from '$lib/system'; import { Account, Assistant, @@ -40,7 +40,9 @@ export function getApiEndpoint(region?: string): string { ); const protocol = url.protocol; const hostname = url.hostname; - const subdomain = getSubdomain(region); + + // If on Appwrite cloud, add the region-based subdomain. + const subdomain = isCloudHostname ? getSubdomain(region) : ''; return `${protocol}//${subdomain}${hostname}/v1`; } @@ -95,14 +97,6 @@ export const realtime = { const endpoint = getApiEndpoint(region); if (endpoint !== clientRealtime.config.endpoint) { clientRealtime.setEndpoint(endpoint); - - /** - * Workaround: the SDK doesn't update the realtime in `setEndpoint`. - * Until that's fixed, we manually set the realtime endpoint like this: - */ - clientRealtime.setEndpointRealtime( - endpoint.replace('https://', 'wss://').replace('http://', 'ws://') - ); } return clientRealtime; } diff --git a/src/lib/system.ts b/src/lib/system.ts index 15f745577..13b304ff0 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -27,3 +27,21 @@ export const isDev = ENV.DEV; export const isProd = ENV.PROD; export const hasStripePublicKey = !!VARS.PUBLIC_STRIPE_KEY; export const GRACE_PERIOD_OVERRIDE = false; + +/** + * Determines if the endpoint points to the Appwrite Cloud domain family. + * + * This includes both the default `cloud.appwrite.io` and any region-specific subdomains + * like `fra.cloud.appwrite.io`, `nyc.cloud.appwrite.io`, etc. + * + * This check is required when using custom domains, which are typically `CNAME`'d + * to region-based API endpoints. These custom domains do not include a region subdomain, + * so this logic helps infer cloud hosting context reliably. + */ +export const isCloudHostname = (() => { + try { + return new URL(VARS.APPWRITE_ENDPOINT).hostname.endsWith('cloud.appwrite.io'); + } catch { + return false; + } +})(); From b07732359c910b48a30afdcbc64933f2718f46ea Mon Sep 17 00:00:00 2001 From: Darshan Date: Sat, 19 Apr 2025 12:09:34 +0530 Subject: [PATCH 2/3] update: jsdoc. --- src/lib/system.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/lib/system.ts b/src/lib/system.ts index 13b304ff0..caebc81e2 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -29,14 +29,10 @@ export const hasStripePublicKey = !!VARS.PUBLIC_STRIPE_KEY; export const GRACE_PERIOD_OVERRIDE = false; /** - * Determines if the endpoint points to the Appwrite Cloud domain family. + * Checks if the endpoint belongs to the Appwrite Cloud domain family, + * including `cloud.appwrite.io` and region subdomains like `fra.cloud.appwrite.io`. * - * This includes both the default `cloud.appwrite.io` and any region-specific subdomains - * like `fra.cloud.appwrite.io`, `nyc.cloud.appwrite.io`, etc. - * - * This check is required when using custom domains, which are typically `CNAME`'d - * to region-based API endpoints. These custom domains do not include a region subdomain, - * so this logic helps infer cloud hosting context reliably. + * Useful when building the API endpoint for custom domains `CNAME`'d to regional endpoints. */ export const isCloudHostname = (() => { try { From fe82658bf1a29098f4fabe7092881e079f263581 Mon Sep 17 00:00:00 2001 From: Darshan Date: Wed, 30 Apr 2025 13:11:04 +0530 Subject: [PATCH 3/3] update: use env var. --- .env.example | 4 +++- .github/workflows/publish.yml | 3 +++ Dockerfile | 2 ++ build.js | 1 + compose.yml | 2 ++ playwright.config.ts | 1 + src/lib/stores/sdk.ts | 6 +++--- src/lib/system.ts | 17 ++--------------- 8 files changed, 17 insertions(+), 19 deletions(-) diff --git a/.env.example b/.env.example index 138249fc1..ae9250596 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,6 @@ -PUBLIC_APPWRITE_ENDPOINT=https://localhost/v1 PUBLIC_CONSOLE_MODE=self-hosted +PUBLIC_APPWRITE_MULTI_REGION=false +PUBLIC_APPWRITE_ENDPOINT=http://localhost/v1 + PUBLIC_STRIPE_KEY= PUBLIC_GROWTH_ENDPOINT= \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 91c98b81f..4c761e03e 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -39,6 +39,7 @@ jobs: labels: ${{ steps.meta.outputs.labels }} build-args: | "PUBLIC_CONSOLE_MODE=cloud" + "PUBLIC_APPWRITE_MULTI_REGION=true" "PUBLIC_GROWTH_ENDPOINT=${{ secrets.PUBLIC_GROWTH_ENDPOINT }}" "PUBLIC_STRIPE_KEY=${{ secrets.PUBLIC_STRIPE_KEY }}" "SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }}" @@ -77,6 +78,7 @@ jobs: labels: ${{ steps.meta.outputs.labels }} build-args: | "PUBLIC_CONSOLE_MODE=cloud" + "PUBLIC_APPWRITE_MULTI_REGION=true" "PUBLIC_GROWTH_ENDPOINT=${{ secrets.PUBLIC_GROWTH_ENDPOINT }}" "PUBLIC_STRIPE_KEY=${{ secrets.PUBLIC_STRIPE_KEY_STAGE }}" publish-self-hosted: @@ -113,4 +115,5 @@ jobs: labels: ${{ steps.meta.outputs.labels }} build-args: | "PUBLIC_CONSOLE_MODE=self-hosted" + "PUBLIC_APPWRITE_MULTI_REGION=false" "PUBLIC_GROWTH_ENDPOINT=${{ secrets.PUBLIC_GROWTH_ENDPOINT }}" diff --git a/Dockerfile b/Dockerfile index feb3ae1a6..32fe52726 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,7 @@ ADD ./src /app/src ADD ./static /app/static ARG PUBLIC_CONSOLE_MODE +ARG PUBLIC_APPWRITE_MULTI_REGION ARG PUBLIC_APPWRITE_ENDPOINT ARG PUBLIC_GROWTH_ENDPOINT ARG PUBLIC_STRIPE_KEY @@ -30,6 +31,7 @@ ARG SENTRY_RELEASE ENV PUBLIC_APPWRITE_ENDPOINT=$PUBLIC_APPWRITE_ENDPOINT ENV PUBLIC_GROWTH_ENDPOINT=$PUBLIC_GROWTH_ENDPOINT ENV PUBLIC_CONSOLE_MODE=$PUBLIC_CONSOLE_MODE +ENV PUBLIC_APPWRITE_MULTI_REGION=$PUBLIC_APPWRITE_MULTI_REGION ENV PUBLIC_STRIPE_KEY=$PUBLIC_STRIPE_KEY ENV SENTRY_AUTH_TOKEN=$SENTRY_AUTH_TOKEN ENV SENTRY_RELEASE=$SENTRY_RELEASE diff --git a/build.js b/build.js index 9753d0378..f56506dae 100644 --- a/build.js +++ b/build.js @@ -24,6 +24,7 @@ async function main() { log(bold().magenta('APPWRITE CONSOLE')); log(); logEnv('CONSOLE MODE', env?.PUBLIC_CONSOLE_MODE); + logEnv('MULTI REGION', env?.PUBLIC_APPWRITE_MULTI_REGION); logEnv('APPWRITE ENDPOINT', env?.PUBLIC_APPWRITE_ENDPOINT, 'relative'); logEnv('GROWTH ENDPOINT', env?.PUBLIC_GROWTH_ENDPOINT); log(); diff --git a/compose.yml b/compose.yml index cfbb54934..6166d3776 100644 --- a/compose.yml +++ b/compose.yml @@ -5,6 +5,7 @@ services: context: . args: PUBLIC_CONSOLE_MODE: ${PUBLIC_CONSOLE_MODE} + PUBLIC_APPWRITE_MULTI_REGION: ${PUBLIC_APPWRITE_MULTI_REGION} PUBLIC_APPWRITE_ENDPOINT: ${PUBLIC_APPWRITE_ENDPOINT} PUBLIC_GROWTH_ENDPOINT: ${PUBLIC_GROWTH_ENDPOINT} PUBLIC_STRIPE_KEY: ${PUBLIC_STRIPE_KEY} @@ -20,6 +21,7 @@ services: - build/ environment: - PUBLIC_CONSOLE_MODE + - PUBLIC_APPWRITE_MULTI_REGION - PUBLIC_APPWRITE_ENDPOINT - PUBLIC_GROWTH_ENDPOINT - PUBLIC_STRIPE_KEY diff --git a/playwright.config.ts b/playwright.config.ts index 8e5910e52..eec9d977c 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -14,6 +14,7 @@ const config: PlaywrightTestConfig = { env: { PUBLIC_APPWRITE_ENDPOINT: 'https://stage.cloud.appwrite.io/v1', PUBLIC_CONSOLE_MODE: 'cloud', + PUBLIC_APPWRITE_MULTI_REGION: 'true', PUBLIC_STRIPE_KEY: 'pk_test_51LT5nsGYD1ySxNCyd7b304wPD8Y1XKKWR6hqo6cu3GIRwgvcVNzoZv4vKt5DfYXL1gRGw4JOqE19afwkJYJq1g3K004eVfpdWn' }, diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index fd9204e3f..e590f984b 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -1,4 +1,4 @@ -import { isCloudHostname, VARS } from '$lib/system'; +import { VARS } from '$lib/system'; import { Account, Assistant, @@ -41,8 +41,8 @@ export function getApiEndpoint(region?: string): string { const protocol = url.protocol; const hostname = url.hostname; - // If on Appwrite cloud, add the region-based subdomain. - const subdomain = isCloudHostname ? getSubdomain(region) : ''; + // If instance supports multi-region, add the region subdomain. + const subdomain = VARS.APPWRITE_MULTI_REGION ? getSubdomain(region) : ''; return `${protocol}//${subdomain}${hostname}/v1`; } diff --git a/src/lib/system.ts b/src/lib/system.ts index caebc81e2..72bb2a0f6 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -10,7 +10,8 @@ export const VARS = { CONSOLE_MODE: (env.PUBLIC_CONSOLE_MODE as Mode) ?? undefined, APPWRITE_ENDPOINT: env.PUBLIC_APPWRITE_ENDPOINT ?? undefined, GROWTH_ENDPOINT: env.PUBLIC_GROWTH_ENDPOINT ?? undefined, - PUBLIC_STRIPE_KEY: env.PUBLIC_STRIPE_KEY ?? undefined + PUBLIC_STRIPE_KEY: env.PUBLIC_STRIPE_KEY ?? undefined, + APPWRITE_MULTI_REGION: env.PUBLIC_APPWRITE_MULTI_REGION ?? false }; export const ENV = { @@ -27,17 +28,3 @@ export const isDev = ENV.DEV; export const isProd = ENV.PROD; export const hasStripePublicKey = !!VARS.PUBLIC_STRIPE_KEY; export const GRACE_PERIOD_OVERRIDE = false; - -/** - * Checks if the endpoint belongs to the Appwrite Cloud domain family, - * including `cloud.appwrite.io` and region subdomains like `fra.cloud.appwrite.io`. - * - * Useful when building the API endpoint for custom domains `CNAME`'d to regional endpoints. - */ -export const isCloudHostname = (() => { - try { - return new URL(VARS.APPWRITE_ENDPOINT).hostname.endsWith('cloud.appwrite.io'); - } catch { - return false; - } -})();