blob: 52e9f5e088022d9e201d64c4d95e78d5ae35fc6b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { handle } from '@astrojs/cloudflare/handler';
import { DurableObject } from 'cloudflare:workers';
interface Env {
COUNTER_DO: DurableObjectNamespace;
}
class CounterDurableObject extends DurableObject<Env> {
async fetch(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === '/get') {
const count = (await this.ctx.storage.get<number>('count')) || 0;
return new Response(count.toString());
}
if (url.pathname === '/increment') {
const count = (await this.ctx.storage.get<number>('count')) || 0;
const newCount = count + 1;
await this.ctx.storage.put('count', newCount);
return new Response(newCount.toString());
}
return new Response('Not found', { status: 404 });
}
}
export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
return {
default: {
async fetch(request, env, ctx) {
return handle(manifest, app, request, env, ctx);
}
} satisfies ExportedHandler<Env>,
CounterDurableObject: CounterDurableObject,
};
}
|