Skip to content

Commit 43df57c

Browse files
committed
wip
1 parent 0c92415 commit 43df57c

File tree

19 files changed

+969
-33
lines changed

19 files changed

+969
-33
lines changed

apps/deploy-worker/.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# dev
2+
.yarn/
3+
!.yarn/releases
4+
.vscode/*
5+
!.vscode/launch.json
6+
!.vscode/*.code-snippets
7+
.idea/workspace.xml
8+
.idea/usage.statistics.xml
9+
.idea/shelf
10+
11+
# deps
12+
node_modules/
13+
14+
# env
15+
.env
16+
.env.production
17+
18+
# logs
19+
logs/
20+
*.log
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
pnpm-debug.log*
25+
lerna-debug.log*
26+
27+
# misc
28+
.DS_Store

apps/deploy-worker/Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM node:22-alpine
2+
3+
RUN apk add --no-cache postgresql-client
4+
5+
WORKDIR /app
6+
7+
COPY --link package.json ./
8+
COPY --link src/ ./src/
9+
10+
RUN npm install
11+
12+
EXPOSE 443
13+
EXPOSE 5432
14+
15+
CMD ["node", "--experimental-strip-types", "src/index.ts"]

apps/deploy-worker/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```
2+
npm install
3+
npm run dev
4+
```
5+
6+
```
7+
open http://localhost:3000
8+
```

apps/deploy-worker/fly.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
primary_region = 'iad'
2+
3+
[[services]]
4+
internal_port = 5432
5+
protocol = "tcp"
6+
[[services.ports]]
7+
handlers = ["proxy_proto"]
8+
port = 5432
9+
10+
[[services]]
11+
internal_port = 443
12+
protocol = "tcp"
13+
[[services.ports]]
14+
port = 443
15+
16+
[[restart]]
17+
policy = "always"
18+
retries = 10
19+
20+
[[vm]]
21+
memory = '512mb'
22+
cpu_kind = 'shared'
23+
cpus = 1

apps/deploy-worker/package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@database.build/deploy-worker",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node --env-file=.env --experimental-strip-types src/index.ts",
6+
"dev": "node --watch --env-file=.env --experimental-strip-types src/index.ts",
7+
"type-check": "tsc",
8+
"generate:types": "npx supabase gen types --lang=typescript --local > src/supabase/db-types.ts"
9+
},
10+
"dependencies": {
11+
"@hono/node-server": "^1.13.2",
12+
"@hono/zod-validator": "^0.4.1",
13+
"@supabase/supabase-js": "^2.45.4",
14+
"debug": "^4.3.7",
15+
"hono": "^4.6.5",
16+
"neverthrow": "^8.0.0",
17+
"zod": "^3.23.8"
18+
},
19+
"devDependencies": {
20+
"@total-typescript/tsconfig": "^1.0.4",
21+
"@types/debug": "^4.1.12",
22+
"@types/node": "^22.5.4",
23+
"typescript": "^5.5.4"
24+
}
25+
}

apps/deploy-worker/src/debug.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import createDebug from 'debug'
2+
3+
export const debug = createDebug('deploy-worker')

apps/deploy-worker/src/index.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { serve } from '@hono/node-server'
2+
import { Hono } from 'hono'
3+
import { z } from 'zod'
4+
import { zValidator } from '@hono/zod-validator'
5+
import { createClient } from './supabase/client.ts'
6+
import { HTTPException } from 'hono/http-exception'
7+
import { deployOnSupabase } from './supabase/deploy.ts'
8+
9+
const app = new Hono()
10+
11+
app.get(
12+
'/',
13+
zValidator(
14+
'json',
15+
z.object({
16+
databaseId: z.string(),
17+
integrationId: z.string(),
18+
databaseUrl: z.string(),
19+
})
20+
),
21+
async (c) => {
22+
const { databaseId, integrationId, databaseUrl: localDatabaseUrl } = c.req.valid('json')
23+
24+
const token = c.req.header('Authorization')?.replace('Bearer ', '')
25+
26+
if (!token) {
27+
throw new HTTPException(401, { message: 'Unauthorized' })
28+
}
29+
30+
const supabase = createClient()
31+
32+
const { error } = await supabase.auth.getUser(token)
33+
34+
if (error) {
35+
throw new HTTPException(401, { message: 'Unauthorized' })
36+
}
37+
38+
// TODO: create a lock in postgres to prevent multiple deployments
39+
// await supabase.from('deployment_locks').insert({
40+
// local_database_id: databaseId,
41+
// })
42+
try {
43+
const { databaseUrl } = await deployOnSupabase(
44+
{ supabase },
45+
{ databaseId, integrationId, localDatabaseUrl }
46+
)
47+
return c.json({ databaseUrl })
48+
} catch (error: unknown) {
49+
if (error instanceof Error) {
50+
throw new HTTPException(500, { message: error.message })
51+
}
52+
throw new HTTPException(500, { message: 'Internal server error' })
53+
} finally {
54+
// TODO: remove the lock
55+
// await supabase.from('deployment_locks').delete().eq('local_database_id', databaseId)
56+
}
57+
}
58+
)
59+
60+
const port = 4000
61+
console.log(`Server is running on port ${port}`)
62+
63+
serve({
64+
fetch: app.fetch,
65+
port,
66+
})
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createClient as createSupabaseClient } from '@supabase/supabase-js'
2+
import type { Database } from './db-types.ts'
3+
4+
export const supabaseAdmin = createSupabaseClient<Database>(
5+
process.env.SUPABASE_URL!,
6+
process.env.SUPABASE_SERVICE_ROLE_KEY!
7+
)
8+
9+
export function createClient() {
10+
return createSupabaseClient<Database>(process.env.SUPABASE_URL!, process.env.SUPABASE_ANON_KEY!)
11+
}

0 commit comments

Comments
 (0)