Skip to content

Commit a26ad42

Browse files
tianzhouclaude
andcommitted
fix: use Durable Object binding for Cloudflare Containers
Containers require a class extending Container, a DO binding, and a migration. Updated wrangler.toml with class_name, DO binding, and migration config. Worker now exports PgConsoleContainer class. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5b3c0eb commit a26ad42

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

.github/workflows/deploy-demo.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ jobs:
2020
id: version
2121
run: echo "VERSION=$(jq -r .version package.json)" >> $GITHUB_OUTPUT
2222

23-
- name: Update image tag in wrangler.toml
24-
run: sed -i "s|pgplex/pgconsole:[^ \"]*|pgplex/pgconsole:${{ steps.version.outputs.VERSION }}|" worker/demo/wrangler.toml
23+
- name: Update image tag
24+
run: |
25+
sed -i "s|pgplex/pgconsole:[^ \"]*|pgplex/pgconsole:${{ steps.version.outputs.VERSION }}|" worker/demo/wrangler.toml
26+
sed -i "s|pgplex/pgconsole:[^ \"]*|pgplex/pgconsole:${{ steps.version.outputs.VERSION }}|" worker/demo/src/index.ts
2527
2628
- uses: pnpm/action-setup@v4
2729
with:

worker/demo/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { Container } from "@cloudflare/containers";
22

33
interface Env {
4-
PGCONSOLE_CONTAINER: Container;
4+
PGCONSOLE_CONTAINER: DurableObjectNamespace<PgConsoleContainer>;
5+
}
6+
7+
export class PgConsoleContainer extends Container {
8+
defaultPort = 9876;
9+
10+
override getContainerImage(): string {
11+
return "docker.io/pgplex/pgconsole:1.1.1";
12+
}
513
}
614

715
export default {
816
async fetch(request: Request, env: Env): Promise<Response> {
9-
return env.PGCONSOLE_CONTAINER.fetch(request);
17+
const id = env.PGCONSOLE_CONTAINER.idFromName("default");
18+
const stub = env.PGCONSOLE_CONTAINER.get(id);
19+
return stub.fetch(request);
1020
},
1121
};

worker/demo/wrangler.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@ routes = [
77
]
88

99
[[containers]]
10-
name = "PGCONSOLE_CONTAINER"
10+
class_name = "PgConsoleContainer"
1111
image = "pgplex/pgconsole:1.1.1"
1212
max_instances = 1
1313

14+
[[durable_objects.bindings]]
15+
class_name = "PgConsoleContainer"
16+
name = "PGCONSOLE_CONTAINER"
17+
18+
[[migrations]]
19+
tag = "v1"
20+
new_sqlite_classes = ["PgConsoleContainer"]
21+
1422
[observability]
1523
enabled = true
1624
head_sampling_rate = 1

0 commit comments

Comments
 (0)