From 09a64a1552b3d3301745d709df75a032ab2c8a14 Mon Sep 17 00:00:00 2001 From: phamhieu Date: Wed, 6 Apr 2022 15:46:25 +0700 Subject: [PATCH] fix: studio endpoint to support lazy project loading --- studio/pages/api/projects/[ref]/index.ts | 63 ++++++++++++++++++++++++ studio/pages/api/projects/index.ts | 32 ------------ 2 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 studio/pages/api/projects/[ref]/index.ts diff --git a/studio/pages/api/projects/[ref]/index.ts b/studio/pages/api/projects/[ref]/index.ts new file mode 100644 index 00000000000..4c525abd79f --- /dev/null +++ b/studio/pages/api/projects/[ref]/index.ts @@ -0,0 +1,63 @@ +import apiWrapper from 'lib/api/apiWrapper' +import { NextApiRequest, NextApiResponse } from 'next' + +export default (req: NextApiRequest, res: NextApiResponse) => apiWrapper(req, res, handler) + +async function handler(req: NextApiRequest, res: NextApiResponse) { + const { method } = req + + switch (method) { + case 'GET': + return handleGet(req, res) + default: + res.setHeader('Allow', ['GET']) + res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } }) + } +} + +const handleGet = async (req: NextApiRequest, res: NextApiResponse) => { + // Platform specific endpoint + const response = { + id: 1, + ref: 'default', + name: 'Default Project', + organization_id: 1, + cloud_provider: 'localhost', + status: 'ACTIVE_HEALTHY', + region: 'local', + connectionString: createDbConnectionString({ + db_user_supabase: 'postgres', + db_host: 'localhost', + db_pass_supabase: process.env.POSTGRES_PASSWORD as string, + db_port: 5432, + db_name: 'postgres', + db_ssl: false, + }), + } + + return res.status(200).json(response) +} + +/** + * Creates a Postgres connection string using the Supabase master login. + * Expects the passwords to be encrypted (straight from the DB) + */ +const createDbConnectionString = ({ + db_user_supabase, + db_pass_supabase, + db_host, + db_port, + db_name, + db_ssl, +}: { + db_user_supabase: string + db_host: string + db_pass_supabase: string + db_port: number + db_name: string + db_ssl: boolean +}) => { + return `postgres://${db_user_supabase}:${db_pass_supabase}@${db_host}:${db_port}/${db_name}?sslmode=${ + db_ssl ? 'require' : 'disable' + }` +} diff --git a/studio/pages/api/projects/index.ts b/studio/pages/api/projects/index.ts index 271862d9b69..f708142b571 100644 --- a/studio/pages/api/projects/index.ts +++ b/studio/pages/api/projects/index.ts @@ -26,39 +26,7 @@ const handleGetAll = async (req: NextApiRequest, res: NextApiResponse) => { cloud_provider: 'localhost', status: 'ACTIVE_HEALTHY', region: 'local', - connectionString: createDbConnectionString({ - db_user_supabase: 'postgres', - db_host: 'localhost', - db_pass_supabase: process.env.POSTGRES_PASSWORD as string, - db_port: 5432, - db_name: 'postgres', - db_ssl: false, - }), }, ] return res.status(200).json(response) } - -/** - * Creates a Postgres connection string using the Supabase master login. - * Expects the passwords to be encrypted (straight from the DB) - */ -const createDbConnectionString = ({ - db_user_supabase, - db_pass_supabase, - db_host, - db_port, - db_name, - db_ssl, -}: { - db_user_supabase: string - db_host: string - db_pass_supabase: string - db_port: number - db_name: string - db_ssl: boolean -}) => { - return `postgres://${db_user_supabase}:${db_pass_supabase}@${db_host}:${db_port}/${db_name}?sslmode=${ - db_ssl ? 'require' : 'disable' - }` -}