Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Proxy API and supabase via Nextjs backend

Tomas Valenta336ba1f6ec5c
diff --git a/.env.example b/.env.example
index 908eb127b..e7cb8bd70 100644
--- a/.env.example
+++ b/.env.example
@@ -2,6 +2,10 @@
OPENAI_API_KEY=
# Supabase
+# URL for the Supabase DB.
+# The preconfigured value with localhost is for the development with local Supabase.
+SUPABASE_URL=http://localhost:54321
+
# Postgres connection string for the Supabase DB used by Prisma.
# The preconfigured value with localhost is for the development with local Supabase.
# Add `?pgbouncer=true&connection_limit=1` to the connection string if you are deploying to production.
@@ -11,10 +15,10 @@ SUPABASE_POSTGRES_URL=postgresql://postgres:postgres@localhost:54322/postgres
# The following default key is generated by the local Supabase start and doesn't change
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0
-# URL for the Supabase DB.
-# The preconfigured value with localhost is for the development with local Supabase.
-NEXT_PUBLIC_SUPABASE_URL=http://localhost:54321
-
# Anonymous key you can get after starting local Supabase with `npm run db:start` ("anon key: eyJh......")
# The following default key is generated by the local Supabase start and doesn't change
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU
+
+# e2b API
+# URL for the e2b API
+API_URL=http://localhost:49155
diff --git a/api-service/app.py b/api-service/app.py
index 2b865826e..9ce095602 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -15,7 +15,7 @@ from codegen.tools.human.tools import create_human_tools
load_dotenv()
-url = os.environ.get("NEXT_PUBLIC_SUPABASE_URL")
+url = os.environ.get("SUPABASE_URL")
key = os.environ.get("SUPABASE_SERVICE_ROLE_KEY")
if not url or not key:
diff --git a/api-service/playground/src/data/logOutput.ts b/api-service/playground/src/data/logOutput.ts
index 0d0bbb8af..b6f704b27 100644
--- a/api-service/playground/src/data/logOutput.ts
+++ b/api-service/playground/src/data/logOutput.ts
@@ -9,7 +9,7 @@ dotenv.config({
path: '../../.env',
})
-const client = createClient<Database>(process.env.SUPABASE_URL || process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY!)
+const client = createClient<Database>(process.env.SUPABASE_URL!, process.env.SUPABASE_SERVICE_ROLE_KEY!)
export const projectsTable = 'projects'
export const deploymentsTable = 'deployments'
diff --git a/components/Editor/Sidebar/index.tsx b/components/Editor/Sidebar/index.tsx
index e5af598ab..c327f2d9f 100644
--- a/components/Editor/Sidebar/index.tsx
+++ b/components/Editor/Sidebar/index.tsx
@@ -27,10 +27,6 @@ export enum MenuSection {
// Deploy = 'Deploy',
}
-const apiHost = process.env.NODE_ENV === 'development'
- ? 'http://localhost:49155'
- : 'https://ai-api-service-7d2cl2hooq-uc.a.run.app'
-
async function handlePostGenerate(url: string, { arg }: {
arg: {
projectID: string,
@@ -81,7 +77,7 @@ function Sidebar({
const {
trigger: generate,
isMutating: isDeployRequestRunning,
- } = useSWRMutation('/api/generate', handlePostGenerate)
+ } = useSWRMutation('/api/service/generate', handlePostGenerate)
const [selectors] = useStateStore()
const envs = selectors.use.envs()
diff --git a/db/credentials.ts b/db/credentials.ts
new file mode 100644
index 000000000..d09e915fd
--- /dev/null
+++ b/db/credentials.ts
@@ -0,0 +1,6 @@
+const baseURL = typeof window !== 'undefined' && `${window.location.protocol}//${window.location.host}`
+
+export const credentials = {
+ supabaseUrl: process.env.SUPABASE_URL || `${baseURL}/api/supabase`,
+ supabaseKey: process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
+}
diff --git a/docker-compose.yml b/docker-compose.yml
index 690d4410b..80363c507 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -7,7 +7,7 @@ services:
context: ./api-service
environment:
- PORT=49155
- - NEXT_PUBLIC_SUPABASE_URL=http://supabase_kong_workspace:8000
+ - SUPABASE_URL=http://supabase_kong_workspace:8000
- SUPABASE_SERVICE_ROLE_KEY=${SUPABASE_SERVICE_ROLE_KEY}
- OPENAI_API_KEY=${OPENAI_API_KEY}
expose:
diff --git a/hooks/useLatestDeployment.ts b/hooks/useLatestDeployment.ts
index a8257e7e8..fce1316e1 100644
--- a/hooks/useLatestDeployment.ts
+++ b/hooks/useLatestDeployment.ts
@@ -36,6 +36,7 @@ export function useLatestDeployment(project: projects, route?: Route) {
// https://github.com/supabase/realtime/issues/223 mentioned that we may need to check the `old_record` field of the payload for the actual value,
// but so far it seems we don't have to.
useEffect(function subscribe() {
+ console.log('client', client)
if (!route?.id) return
// TODO: SECURITY - Enable row security for all tables and configure access to deployments.
diff --git a/next.config.js b/next.config.js
index 9239c3cb1..2398a09ab 100644
--- a/next.config.js
+++ b/next.config.js
@@ -2,19 +2,7 @@
const nextConfig = {
swcMinify: true,
experimental: {
- // swcPlugins: [['next-superjson-plugin', {}]],
- },
- async rewrites() {
- return [
- {
- source: '/generate',
- destination: 'http://localhost:49155/generate',
- },
- {
- source: '/db',
- destination: 'http://localhost:3000/db',
- },
- ]
+ swcPlugins: [['next-superjson-plugin', {}]],
},
}
diff --git a/package-lock.json b/package-lock.json
index f9da53461..41f870f5f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -26,7 +26,7 @@
"@tiptap/starter-kit": "^2.0.0-beta.220",
"clsx": "^1.2.1",
"fuse.js": "^6.6.2",
- "http-proxy": "^1.18.1",
+ "http-proxy-middleware": "^2.0.6",
"human-id": "^4.0.0",
"immer": "^9.0.19",
"lucide-react": "^0.130.1",
@@ -49,7 +49,6 @@
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
- "@types/http-proxy": "^1.17.10",
"@types/node": "^18.14.6",
"@types/randomcolor": "^0.5.7",
"@types/react": "^18.0.28",
@@ -1026,7 +1025,6 @@
"version": "1.17.10",
"resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.10.tgz",
"integrity": "sha512-Qs5aULi+zV1bwKAg5z1PWnDXWmsn+LxIvUGv6E2+OOMYhclZMO+OXd9pYVf2gLykf2I7IV2u7oTHwChPNsvJ7g==",
- "dev": true,
"dependencies": {
"@types/node": "*"
}
@@ -1053,8 +1051,7 @@
"node_modules/@types/node": {
"version": "18.14.6",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.14.6.tgz",
- "integrity": "sha512-93+VvleD3mXwlLI/xASjw0FzKcwzl3OdTCzm1LaRfqgS21gfFtK3zDXM5Op9TeeMsJVOaJ2VRDpT9q4Y3d0AvA==",
- "dev": true
+ "integrity": "sha512-93+VvleD3mXwlLI/xASjw0FzKcwzl3OdTCzm1LaRfqgS21gfFtK3zDXM5Op9TeeMsJVOaJ2VRDpT9q4Y3d0AvA=="
},
"node_modules/@types/object.omit": {
"version": "3.0.0",
@@ -1548,7 +1545,6 @@
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz",
"integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==",
- "dev": true,
"dependencies": {
"fill-range": "^7.0.1"
},
@@ -3167,7 +3163,6 @@
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
"integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==",
- "dev": true,
"dependencies": {
"to-regex-range": "^5.0.1"
},
@@ -3630,6 +3625,40 @@
"node": ">=8.0.0"
}
},
+ "node_modules/http-proxy-middleware": {
+ "version": "2.0.6",
+ "resolved": "https://registry.npmjs.org/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz",
+ "integrity": "sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==",
+ "dependencies": {
+ "@types/http-proxy": "^1.17.8",
+ "http-proxy": "^1.18.1",
+ "is-glob": "^4.0.1",
+ "is-plain-obj": "^3.0.0",
+ "micromatch": "^4.0.2"
+ },
+ "engines": {
+ "node": ">=12.0.0"
+ },
+ "peerDependencies": {
+ "@types/express": "^4.17.13"
+ },
+ "peerDependenciesMeta": {
+ "@types/express": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/http-proxy-middleware/node_modules/is-plain-obj": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-3.0.0.tgz",
+ "integrity": "sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==",
+ "engines": {
+ "node": ">=10"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/sindresorhus"
+ }
+ },
"node_modules/human-id": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/human-id/-/human-id-4.0.0.tgz",
@@ -3874,7 +3903,6 @@
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
"integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==",
- "dev": true,
"engines": {
"node": ">=0.10.0"
}
@@ -3892,7 +3920,6 @@
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
"integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==",
- "dev": true,
"dependencies": {
"is-extglob": "^2.1.1"
},
@@ -3925,7 +3952,6 @@
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz",
"integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==",
- "dev": true,
"engines": {
"node": ">=0.12.0"
}
@@ -4876,7 +4902,6 @@
"version": "4.0.5",
"resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz",
"integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==",
- "dev": true,
"dependencies": {
"braces": "^3.0.2",
"picomatch": "^2.3.1"
@@ -5486,7 +5511,6 @@
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz",
"integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==",
- "dev": true,
"engines": {
"node": ">=8.6"
},
@@ -7153,7 +7177,6 @@
"version": "5.0.1",
"resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz",
"integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==",
- "dev": true,
"dependencies": {
"is-number": "^7.0.0"
},
diff --git a/package.json b/package.json
index 55ba73e62..60311e240 100644
--- a/package.json
+++ b/package.json
@@ -41,7 +41,7 @@
"@tiptap/starter-kit": "^2.0.0-beta.220",
"clsx": "^1.2.1",
"fuse.js": "^6.6.2",
- "http-proxy": "^1.18.1",
+ "http-proxy-middleware": "^2.0.6",
"human-id": "^4.0.0",
"immer": "^9.0.19",
"lucide-react": "^0.130.1",
@@ -64,7 +64,6 @@
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
- "@types/http-proxy": "^1.17.10",
"@types/node": "^18.14.6",
"@types/randomcolor": "^0.5.7",
"@types/react": "^18.0.28",
diff --git a/pages/[projectID].tsx b/pages/[projectID].tsx
index 5e9be2b30..cb7faa03f 100644
--- a/pages/[projectID].tsx
+++ b/pages/[projectID].tsx
@@ -7,6 +7,7 @@ import { prisma, projects } from 'db/prisma'
import Editor from 'components/Editor'
import { StoreProvider } from 'state/StoreProvider'
import { Database } from 'db/supabase'
+import { credentials } from 'db/credentials'
interface PathProps extends ParsedUrlQuery {
projectID: string
@@ -20,7 +21,7 @@ export const getServerSideProps: GetServerSideProps<Props, PathProps> = async (c
}
}
- const supabase = createServerSupabaseClient(ctx)
+ const supabase = createServerSupabaseClient(ctx, credentials)
const {
data: { session },
} = await supabase.auth.getSession()
diff --git a/pages/_app.tsx b/pages/_app.tsx
index d93182fe5..bdc0b699b 100644
--- a/pages/_app.tsx
+++ b/pages/_app.tsx
@@ -11,6 +11,7 @@ import 'styles/global.css'
import Header from 'components/Header'
import { Database } from 'db/supabase'
+import { credentials } from 'db/credentials'
const inter = Inter({
subsets: ['latin'],
@@ -19,7 +20,7 @@ const inter = Inter({
function App({ Component, pageProps }: AppProps<{ initialSession: Session, project?: projects }>) {
const router = useRouter()
- const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>())
+ const [supabaseClient] = useState(() => createBrowserSupabaseClient<Database>(credentials))
const isSignIn = router.pathname === '/sign' && router.query.signup !== 'true'
const isSignUp = router.pathname === '/sign' && router.query.signup === 'true'
diff --git a/pages/api/[...all].ts b/pages/api/[...all].ts
deleted file mode 100644
index 062ac419c..000000000
--- a/pages/api/[...all].ts
+++ /dev/null
@@ -1,24 +0,0 @@
-// import httpProxy from 'http-proxy'
-// import { NextApiRequest, NextApiResponse } from 'next'
-
-// export const config = {
-// api: {
-// // Enable `externalResolver` option in Next.js
-// externalResolver: true,
-// bodyParser: false,
-// },
-// }
-
-// async function handler(req: NextApiRequest, res: NextApiResponse) {
-// const url = new URL(req.url!, `http://${req.headers.host}`)
-
-// return new Promise((resolve, reject) => {
-// const proxy: httpProxy = httpProxy.createProxy()
-// proxy.once('proxyRes', resolve).once('error', reject).web(req, res, {
-// changeOrigin: true,
-// target: url.pathname === '/api/generate' ? process.env.API_URL : process.env.SUPABASE_URL,
-// })
-// })
-// }
-
-// export default handler
diff --git a/pages/api/project.ts b/pages/api/project.ts
index 1bc56ee2f..d1f3142d8 100644
--- a/pages/api/project.ts
+++ b/pages/api/project.ts
@@ -2,6 +2,7 @@ import { createServerSupabaseClient } from '@supabase/auth-helpers-nextjs'
import { NextApiRequest, NextApiResponse } from 'next'
import { prisma } from 'db/prisma'
+import { credentials } from 'db/credentials'
export interface PostProjectBody {
id: string
@@ -13,7 +14,7 @@ async function postProject(req: NextApiRequest, res: NextApiResponse) {
} = req.body as PostProjectBody
try {
- const supabase = createServerSupabaseClient({ req, res })
+ const supabase = createServerSupabaseClient({ req, res }, credentials)
const { data: { session } } = await supabase.auth.getSession()
if (!session) {
return res.status(401).json({
@@ -74,7 +75,7 @@ async function deleteProject(req: NextApiRequest, res: NextApiResponse) {
const { id } = req.body as PostProjectBody
try {
- const supabase = createServerSupabaseClient({ req, res })
+ const supabase = createServerSupabaseClient({ req, res }, credentials)
const { data: { session } } = await supabase.auth.getSession()
if (!session) {
return res.status(401).json({
diff --git a/pages/api/service/[...all].ts b/pages/api/service/[...all].ts
new file mode 100644
index 000000000..aa26af1ce
--- /dev/null
+++ b/pages/api/service/[...all].ts
@@ -0,0 +1,25 @@
+import { createProxyMiddleware } from 'http-proxy-middleware'
+
+export const config = {
+ api: {
+ // Proxy middleware will handle requests itself, so Next.js should
+ // ignore that our handler doesn't directly return a response
+ externalResolver: true,
+ // Pass request bodies through unmodified so that the origin API server
+ // receives them in the intended format
+ bodyParser: false,
+ },
+}
+
+const pathPrefix = '/api/service'
+
+const target = process.env.API_URL!
+const isSecure = target.startsWith('https://')
+
+export default createProxyMiddleware(`${pathPrefix}/**`, {
+ target,
+ // ws: true,
+ secure: !isSecure,
+ changeOrigin: true,
+ pathRewrite: { [`^${pathPrefix}`]: '' }, // remove prefix
+})
diff --git a/pages/api/supabase/[...all].ts b/pages/api/supabase/[...all].ts
new file mode 100644
index 000000000..1d4897262
--- /dev/null
+++ b/pages/api/supabase/[...all].ts
@@ -0,0 +1,25 @@
+import { createProxyMiddleware } from 'http-proxy-middleware'
+
+export const config = {
+ api: {
+ // Proxy middleware will handle requests itself, so Next.js should
+ // ignore that our handler doesn't directly return a response
+ externalResolver: true,
+ // Pass request bodies through unmodified so that the origin API server
+ // receives them in the intended format
+ bodyParser: false,
+ },
+}
+
+const pathPrefix = '/api/supabase'
+
+const target = process.env.SUPABASE_URL!
+const isSecure = target.startsWith('https://')
+
+export default createProxyMiddleware(`${pathPrefix}/**`, {
+ target,
+ ws: true,
+ secure: !isSecure,
+ changeOrigin: true,
+ pathRewrite: { [`^${pathPrefix}`]: '' }, // remove prefix
+})
diff --git a/pages/index.tsx b/pages/index.tsx
index 8c0b278ad..63c465cd4 100644
--- a/pages/index.tsx
+++ b/pages/index.tsx
@@ -8,9 +8,10 @@ import ItemList from 'components/ItemList'
import Text from 'components/Text'
import { prisma, projects } from 'db/prisma'
import Button from 'components/Button'
+import { credentials } from 'db/credentials'
export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
- const supabase = createServerSupabaseClient(ctx)
+ const supabase = createServerSupabaseClient(ctx, credentials)
const {
data: { session },
} = await supabase.auth.getSession()
diff --git a/pages/settings.tsx b/pages/settings.tsx
index c0cb39fa3..34cc57907 100644
--- a/pages/settings.tsx
+++ b/pages/settings.tsx
@@ -7,9 +7,10 @@ import { useSupabaseClient, useUser } from '@supabase/auth-helpers-react'
import Button from 'components/Button'
import Text from 'components/Text'
import { Database } from 'db/supabase'
+import { credentials } from 'db/credentials'
export const getServerSideProps: GetServerSideProps<Props> = async (ctx) => {
- const supabase = createServerSupabaseClient<Database>(ctx)
+ const supabase = createServerSupabaseClient<Database>(ctx, credentials)
const {
data: { session },
} = await supabase.auth.getSession()