summaryrefslogtreecommitdiff
path: root/src/worker.ts
blob: 5490e2a466314f8f1a8b973d4957b0291aa2f6fd (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
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { handle } from '@astrojs/cloudflare/handler';
import { DurableObject } from 'cloudflare:workers';

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 as any, env as any, ctx);
      }
    } satisfies ExportedHandler<Env>,
    CounterDurableObject: CounterDurableObject,
  };
}