summaryrefslogtreecommitdiff
path: root/README.md
blob: f134769afe91d6239643cebf4be209f9170c4038 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Astro + Cloudflare Durable Objects Demo

A minimal example showing how to use Cloudflare Durable Objects with Astro for persistent storage.

## Features

- **Persistent Counter**: A simple counter that maintains state across deployments
- **Durable Objects**: Demonstrates Cloudflare's stateful serverless objects
- **Minimal Setup**: Clean, simple implementation with minimal code

## Project Structure

```
/
├── src/
│   ├── pages/
│   │   ├── index.astro          # Homepage with counter demo
│   │   └── api/
│   │       └── counter.ts       # API routes for counter operations
│   └── worker.ts                # Durable Object implementation
├── wrangler.jsonc               # Cloudflare Workers configuration
└── astro.config.mjs             # Astro configuration
```

## API Endpoints

- `GET /api/counter` - Get current counter value (returns plain text)
- `POST /api/counter` - Increment counter and return new value (returns plain text)

## Setup & Development

1. **Install dependencies**
   ```bash
   npm install
   ```

2. **Generate TypeScript types**
   ```bash
   npx wrangler@latest types
   ```

3. **Start development server**
   ```bash
   npx wrangler@latest dev
   ```

4. **Build for production**
   ```bash
   npm run build
   ```

## Deployment

Deploy to Cloudflare Workers:

```bash
npx wrangler@latest deploy
```

The Durable Object will be automatically created and bound according to the configuration in `wrangler.jsonc`.

## How It Works

1. **Durable Object**: `CounterDurableObject` stores a persistent counter value
2. **API Routes**: Simple endpoints that interact with the Durable Object
3. **Frontend**: Basic HTML/JS interface that calls the API endpoints

The counter value persists across deployments and is globally consistent across all requests.

## Key Files

- `src/worker.ts` - Durable Object class with storage operations
- `src/pages/api/counter.ts` - API endpoints that proxy to the Durable Object
- `wrangler.jsonc` - Durable Object binding configuration
- `astro.config.mjs` - Astro + Cloudflare adapter configuration