Skip to content
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

feat: set project endpoint based on the region #1508

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
8 changes: 8 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ export const PAGE_LIMIT = 12; // default page limit
export const CARD_LIMIT = 6; // default card limit
export const INTERVAL = 5 * 60000; // default interval to check for feedback

export const REGION_FRA = 'fra';
export const REGION_SYD = 'syd';
export const REGION_NYC = 'nyc';

export const SUBDOMAIN_FRA = 'fra-';
export const SUBDOMAIN_SYD = 'syd-';
export const SUBDOMAIN_NYC = 'nyc-';

export enum Dependencies {
FACTORS = 'dependency:factors',
IDENTITIES = 'dependency:identities',
Expand Down
28 changes: 26 additions & 2 deletions src/lib/stores/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,36 @@ import {
import { Billing } from '../sdk/billing';
import { Backups } from '../sdk/backups';
import { Sources } from '$lib/sdk/sources';
import {
REGION_FRA,
REGION_NYC,
REGION_SYD,
SUBDOMAIN_FRA,
SUBDOMAIN_NYC,
SUBDOMAIN_SYD
} from '$lib/constants';

export function getApiEndpoint(): string {
export function getApiEndpoint(region?: string): string {
if (VARS.APPWRITE_ENDPOINT) return VARS.APPWRITE_ENDPOINT;
return globalThis?.location?.origin + '/v1';
const protocol = globalThis?.location?.protocol;
const hostname = globalThis?.location?.hostname;
const subdomain = getSubdomain(region);
return `${protocol}//${subdomain}${hostname}/v1`;
}

const getSubdomain = (region?: string) => {
switch (region) {
case REGION_FRA:
return SUBDOMAIN_FRA;
case REGION_SYD:
return SUBDOMAIN_SYD;
case REGION_NYC:
return SUBDOMAIN_NYC;
default:
return '';
}
};
Comment on lines +44 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const getSubdomain = (region?: string) => {
switch (region) {
case REGION_FRA:
return SUBDOMAIN_FRA;
case REGION_SYD:
return SUBDOMAIN_SYD;
case REGION_NYC:
return SUBDOMAIN_NYC;
default:
return '';
}
};
const getSubdomain = (region?: string) => {
return region ? `${region}.` : '';
};

we should probably avoid hardcoding regions for this in the console.


const endpoint = getApiEndpoint();

const clientConsole = new Client();
Expand Down
5 changes: 4 additions & 1 deletion src/routes/(console)/project-[project]/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dependencies } from '$lib/constants';
import { sdk } from '$lib/stores/sdk';
import { getApiEndpoint, sdk } from '$lib/stores/sdk';
import { error } from '@sveltejs/kit';
import type { LayoutLoad } from './$types';
import { preferences } from '$lib/stores/preferences';
Expand Down Expand Up @@ -35,6 +35,9 @@ export const load: LayoutLoad = async ({ params, depends }) => {
}
}

console.log(getApiEndpoint(project.region));
sdk.forProject.client.setEndpoint(getApiEndpoint(project.region));

return {
project,
organization,
Expand Down
5 changes: 4 additions & 1 deletion src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@appwrite.io/pink';
import '@appwrite.io/pink-icons';
import 'tippy.js/dist/tippy.css';
import { sdk } from '$lib/stores/sdk';
import { getApiEndpoint, sdk } from '$lib/stores/sdk';
import { redirect } from '@sveltejs/kit';
import { Dependencies } from '$lib/constants';
import type { LayoutLoad } from './$types';
Expand All @@ -15,6 +15,9 @@ export const ssr = false;
export const load: LayoutLoad = async ({ depends, url, route }) => {
depends(Dependencies.ACCOUNT);

console.log(getApiEndpoint());
sdk.forProject.client.setEndpoint(getApiEndpoint());

const [account, error] = (await sdk.forConsole.account
.get()
.then((response) => [response, null])
Expand Down
Loading