-
Notifications
You must be signed in to change notification settings - Fork 252
Feat 3139 self hosted multi regions support #3150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ibaraki-douji
wants to merge
5
commits into
appwrite:main
Choose a base branch
from
ibaraki-douji:feat-3139-self-hosted-multi-regions-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a82d0e6
feat(regions): implement multi-region support and enhance region hand…
ibaraki-douji 3c2c405
fix(env): update .env.example for self-hosted configuration and clari…
ibaraki-douji ed29aa2
feat(regions): add ensureSelfHostedRegions function to manage region …
ibaraki-douji dbfd407
fix(regions): improve error handling in loadSelfHostedRegions functio…
ibaraki-douji 2447975
chore(env): Revert PUBLIC_APPWRITE_ENDPOINT as greptile complain in b…
ibaraki-douji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| PUBLIC_CONSOLE_MODE=self-hosted | ||
| PUBLIC_CONSOLE_FEATURE_FLAGS= | ||
| # When true: self-hosted region picker + load catalog from GET /console/regions | ||
| PUBLIC_APPWRITE_MULTI_REGION=false | ||
| PUBLIC_APPWRITE_ENDPOINT=http://localhost/v1 | ||
| # Leave empty so the SPA uses the current page hostname + /v1. | ||
| # Set explicitly only when the API is on a different origin (e.g. local bun dev). | ||
| PUBLIC_APPWRITE_ENDPOINT= | ||
| PUBLIC_STRIPE_KEY= | ||
| PUBLIC_GROWTH_ENDPOINT= | ||
| PUBLIC_CONSOLE_MOCK_AI_SUGGESTIONS=true | ||
| PUBLIC_CONSOLE_MOCK_AI_SUGGESTIONS=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { expect, test, beforeEach } from 'vitest'; | ||
| import { | ||
| setRegionHosts, | ||
| resolveRegionV1Endpoint, | ||
| type ConsoleRegionWithHost | ||
| } from '$lib/helpers/regionHosts'; | ||
|
|
||
| function region(partial: Partial<ConsoleRegionWithHost> & { $id: string }): ConsoleRegionWithHost { | ||
| return { | ||
| name: partial.name ?? partial.$id, | ||
| disabled: false, | ||
| available: true, | ||
| ...partial | ||
| } as ConsoleRegionWithHost; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| setRegionHosts([]); | ||
| }); | ||
|
|
||
| test('resolveRegionV1Endpoint returns null when catalog is empty', () => { | ||
| expect(resolveRegionV1Endpoint('http:', 'fra')).toBeNull(); | ||
| }); | ||
|
|
||
| test('resolveRegionV1Endpoint returns null for default or missing region', () => { | ||
| setRegionHosts([region({ $id: 'fra', hostname: 'fra.example.com' })]); | ||
| expect(resolveRegionV1Endpoint('http:', 'default')).toBeNull(); | ||
| expect(resolveRegionV1Endpoint('http:', undefined)).toBeNull(); | ||
| }); | ||
|
|
||
| test('resolveRegionV1Endpoint prefers endpoint over hostname', () => { | ||
| setRegionHosts([ | ||
| region({ | ||
| $id: 'nyc', | ||
| hostname: 'nyc.example.com', | ||
| endpoint: 'https://api.nyc.example.com' | ||
| }) | ||
| ]); | ||
| expect(resolveRegionV1Endpoint('http:', 'nyc')).toBe('https://api.nyc.example.com/v1'); | ||
| }); | ||
|
|
||
| test('resolveRegionV1Endpoint keeps /v1 on endpoint and strips trailing slash', () => { | ||
| setRegionHosts([ | ||
| region({ $id: 'fra', endpoint: 'https://fra.example.com/v1/' }), | ||
| region({ $id: 'syd', endpoint: 'https://syd.example.com/' }) | ||
| ]); | ||
| expect(resolveRegionV1Endpoint('https:', 'fra')).toBe('https://fra.example.com/v1'); | ||
| expect(resolveRegionV1Endpoint('https:', 'syd')).toBe('https://syd.example.com/v1'); | ||
| }); | ||
|
|
||
| test('resolveRegionV1Endpoint builds URL from hostname and page protocol', () => { | ||
| setRegionHosts([region({ $id: 'fra', hostname: 'fra.localhost' })]); | ||
| expect(resolveRegionV1Endpoint('http:', 'fra')).toBe('http://fra.localhost/v1'); | ||
| }); | ||
|
|
||
| test('setRegionHosts replaces previous catalog entries', () => { | ||
| setRegionHosts([region({ $id: 'fra', hostname: 'old.example.com' })]); | ||
| setRegionHosts([region({ $id: 'nyc', hostname: 'nyc.example.com' })]); | ||
| expect(resolveRegionV1Endpoint('http:', 'fra')).toBeNull(); | ||
| expect(resolveRegionV1Endpoint('http:', 'nyc')).toBe('http://nyc.example.com/v1'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import type { Models } from '@appwrite.io/console'; | ||
|
|
||
| export type RegionHostInfo = { | ||
| hostname?: string; | ||
| endpoint?: string; | ||
| }; | ||
|
|
||
| export type ConsoleRegionWithHost = Models.ConsoleRegion & { | ||
| hostname?: string; | ||
| endpoint?: string; | ||
| }; | ||
|
|
||
| const byId = new Map<string, RegionHostInfo>(); | ||
|
|
||
| /** Populate hostname/endpoint overrides from the regions catalog. */ | ||
| export function setRegionHosts(list: ConsoleRegionWithHost[] | null | undefined): void { | ||
| byId.clear(); | ||
| if (!list?.length) return; | ||
|
|
||
| for (const region of list) { | ||
| const id = region?.$id; | ||
| if (!id) continue; | ||
|
|
||
| const { hostname, endpoint } = region; | ||
| if (hostname || endpoint) { | ||
| byId.set(id, { hostname, endpoint }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve an explicit regional API base URL from the catalog. | ||
| * Returns null when the region has no hostname/endpoint override (Cloud subdomain logic applies). | ||
| */ | ||
| export function resolveRegionV1Endpoint( | ||
| protocol: string, | ||
| region: string | undefined | ||
| ): string | null { | ||
| if (!region || region === 'default') return null; | ||
|
|
||
| const info = byId.get(region); | ||
| if (!info) return null; | ||
|
|
||
| if (info.endpoint) { | ||
| const trimmed = info.endpoint.replace(/\/$/, ''); | ||
| return trimmed.endsWith('/v1') ? trimmed : `${trimmed}/v1`; | ||
| } | ||
|
|
||
| if (info.hostname) { | ||
| return `${protocol}//${info.hostname}/v1`; | ||
| } | ||
|
|
||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.