From 3f0837d920ba67496c5fc7f50485aed84b37d05e Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 10 Jul 2025 13:55:01 +0200 Subject: Simplify code' --- .gitignore | 1 + src/pages/api/counter.ts | 52 ++------- src/pages/api/hello.ts | 54 --------- src/pages/index.astro | 277 +++++++---------------------------------------- src/worker.ts | 43 ++------ 5 files changed, 58 insertions(+), 369 deletions(-) delete mode 100644 src/pages/api/hello.ts diff --git a/.gitignore b/.gitignore index 16d54bb..b7f47be 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ dist/ # generated types .astro/ +.wrangler/ # dependencies node_modules/ diff --git a/src/pages/api/counter.ts b/src/pages/api/counter.ts index 1d3d0b7..4f42236 100644 --- a/src/pages/api/counter.ts +++ b/src/pages/api/counter.ts @@ -6,54 +6,16 @@ interface Env { export const GET: APIRoute = async ({ locals }) => { const env = locals.runtime.env as Env; - if (!env?.COUNTER_DO) { - return new Response(JSON.stringify({ error: 'Durable Object not available' }), { - status: 500, - headers: { 'Content-Type': 'application/json' } - }); - } - - const id = env.COUNTER_DO.idFromName('global-counter'); + const id = env.COUNTER_DO.idFromName('counter'); const stub = env.COUNTER_DO.get(id); - - const response = await stub.fetch(new Request('https://counter.do/get')); - const data = await response.json(); - - return new Response(JSON.stringify(data), { - headers: { 'Content-Type': 'application/json' } - }); + const response = await stub.fetch('https://counter.do/get'); + return response; }; -export const POST: APIRoute = async ({ request, locals }) => { +export const POST: APIRoute = async ({ locals }) => { const env = locals.runtime.env as Env; - if (!env?.COUNTER_DO) { - return new Response(JSON.stringify({ error: 'Durable Object not available' }), { - status: 500, - headers: { 'Content-Type': 'application/json' } - }); - } - - const url = new URL(request.url); - const action = url.searchParams.get('action') || 'increment'; - - const id = env.COUNTER_DO.idFromName('global-counter'); + const id = env.COUNTER_DO.idFromName('counter'); const stub = env.COUNTER_DO.get(id); - - let doRequest: Request; - switch (action) { - case 'reset': - doRequest = new Request('https://counter.do/reset'); - break; - case 'increment': - default: - doRequest = new Request('https://counter.do/increment'); - break; - } - - const response = await stub.fetch(doRequest); - const data = await response.json(); - - return new Response(JSON.stringify(data), { - headers: { 'Content-Type': 'application/json' } - }); + const response = await stub.fetch('https://counter.do/increment'); + return response; }; \ No newline at end of file diff --git a/src/pages/api/hello.ts b/src/pages/api/hello.ts deleted file mode 100644 index eba198d..0000000 --- a/src/pages/api/hello.ts +++ /dev/null @@ -1,54 +0,0 @@ -import type { APIRoute } from 'astro'; - -export const GET: APIRoute = async ({ request }) => { - const url = new URL(request.url); - const name = url.searchParams.get('name') || 'World'; - - return new Response( - JSON.stringify({ - message: `Hello, ${name}!`, - timestamp: new Date().toISOString(), - method: 'GET' - }), - { - status: 200, - headers: { - 'Content-Type': 'application/json', - }, - } - ); -}; - -export const POST: APIRoute = async ({ request }) => { - try { - const body = await request.json(); - - return new Response( - JSON.stringify({ - message: 'Data received successfully', - received: body, - timestamp: new Date().toISOString(), - method: 'POST' - }), - { - status: 200, - headers: { - 'Content-Type': 'application/json', - }, - } - ); - } catch (error) { - return new Response( - JSON.stringify({ - error: 'Invalid JSON', - timestamp: new Date().toISOString() - }), - { - status: 400, - headers: { - 'Content-Type': 'application/json', - }, - } - ); - } -}; \ No newline at end of file diff --git a/src/pages/index.astro b/src/pages/index.astro index 363e843..ae53624 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -7,267 +7,74 @@ - - Astro + Cloudflare Durable Objects Demo + Astro + Cloudflare Durable Objects -
-

Astro + Cloudflare Durable Objects

-

A persistent counter demonstrating Durable Objects with Astro and Cloudflare Workers

- -
-

Global Counter

-
-
-
- - - -
- -
- -
-

How it works

-

This demo showcases a Cloudflare Durable Object that maintains a persistent counter. The counter state survives across requests, deployments, and server restarts.

- -

API Endpoints:

- - -

Key Features:

- -
+

Astro + Cloudflare Durable Objects

+

A simple persistent counter using Durable Objects.

+ +
+
-
+ +
- + +

How it works: The counter value is stored in a Durable Object and persists across deployments.

+ - + \ No newline at end of file diff --git a/src/worker.ts b/src/worker.ts index 5eb9d5d..52e9f5e 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -8,46 +8,19 @@ interface Env { } class CounterDurableObject extends DurableObject { - constructor(ctx: DurableObjectState, env: Env) { - super(ctx, env); - } - async fetch(request: Request): Promise { const url = new URL(request.url); - const pathname = url.pathname; - - if (pathname === '/increment') { - const currentValue = (await this.ctx.storage.get('counter')) || 0; - const newValue = currentValue + 1; - await this.ctx.storage.put('counter', newValue); - - return new Response(JSON.stringify({ - counter: newValue, - timestamp: new Date().toISOString() - }), { - headers: { 'Content-Type': 'application/json' } - }); - } - if (pathname === '/get') { - const currentValue = (await this.ctx.storage.get('counter')) || 0; - return new Response(JSON.stringify({ - counter: currentValue, - timestamp: new Date().toISOString() - }), { - headers: { 'Content-Type': 'application/json' } - }); + if (url.pathname === '/get') { + const count = (await this.ctx.storage.get('count')) || 0; + return new Response(count.toString()); } - if (pathname === '/reset') { - await this.ctx.storage.put('counter', 0); - return new Response(JSON.stringify({ - counter: 0, - message: 'Counter reset', - timestamp: new Date().toISOString() - }), { - headers: { 'Content-Type': 'application/json' } - }); + if (url.pathname === '/increment') { + const count = (await this.ctx.storage.get('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 }); -- cgit v1.3.1