Skip to content

Commit 60d0baa

Browse files
committed
Add support for Cloudflare Workers
1 parent f9e8df7 commit 60d0baa

File tree

19 files changed

+304
-164
lines changed

19 files changed

+304
-164
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![JSR Version](https://jsr.io/badges/@rabbit-company/web)](https://jsr.io/@rabbit-company/web)
55
[![License](https://img.shields.io/npm/l/@rabbit-company/web)](LICENSE)
66

7-
A high-performance web framework built for **Bun**, **Deno** and **NodeJS** with trie-based routing, middleware support, and TypeScript-first design. ⚡
7+
A high-performance web framework built for **Bun**, **Deno**, **NodeJS** and **Cloudflare Workers** with trie-based routing, middleware support, and TypeScript-first design. ⚡
88

99
## ✨ Features
1010

@@ -50,9 +50,14 @@ app.get('/users/:id', async (ctx) => {
5050
return ctx.json(user);
5151
});
5252

53-
// Start server
53+
// Start server on Node, Deno or Bun
5454
app.listen({ port: 3000 });
5555

56+
// Start server on Cloudflare Workers
57+
export default {
58+
fetch: app.handleCloudflare,
59+
};
60+
5661
console.log('Server running at http://localhost:3000');
5762
```
5863

examples/cloudflare-workers.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Web } from "@rabbit-company/web";
2+
import { bearerAuth } from "@rabbit-company/web-middleware/bearer-auth";
3+
import { bodyLimit } from "@rabbit-company/web-middleware/body-limit";
4+
import { cors } from "@rabbit-company/web-middleware/cors";
5+
6+
// Cloudflare env Bindings
7+
type Bindings = {
8+
API_KEY: string;
9+
};
10+
11+
const app = new Web<{ userId: string }, Bindings>();
12+
13+
app.use(cors());
14+
app.use(bodyLimit({ maxSize: "10mb" }));
15+
app.use(
16+
"/api/*",
17+
bearerAuth({
18+
validate(token, ctx) {
19+
return token === "validToken";
20+
},
21+
})
22+
);
23+
24+
app.use(async (ctx, next) => {
25+
ctx.set("userId", ctx.clientIp || "unknown");
26+
await next();
27+
});
28+
29+
app.get("/", (ctx) => {
30+
return ctx.html("<h1>Hello from Cloudflare Workers</h1>");
31+
});
32+
33+
app.get("/api/info", (ctx) => {
34+
return ctx.json({ version: "0.0.1", clientIP: ctx.clientIp });
35+
});
36+
37+
export default {
38+
fetch: app.handleCloudflare,
39+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rabbit-company/web-monorepo",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "High-performance web framework monorepo",
55
"private": true,
66
"type": "module",

packages/core/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![JSR Version](https://jsr.io/badges/@rabbit-company/web)](https://jsr.io/@rabbit-company/web)
55
[![License](https://img.shields.io/npm/l/@rabbit-company/web)](LICENSE)
66

7-
A high-performance web framework built for **Bun**, **Deno** and **NodeJS** with trie-based routing, middleware support, and TypeScript-first design. ⚡
7+
A high-performance web framework built for **Bun**, **Deno**, **NodeJS** and **Cloudflare Workers** with trie-based routing, middleware support, and TypeScript-first design. ⚡
88

99
## ✨ Features
1010

@@ -50,9 +50,14 @@ app.get('/users/:id', async (ctx) => {
5050
return ctx.json(user);
5151
});
5252

53-
// Start server
53+
// Start server on Node, Deno or Bun
5454
app.listen({ port: 3000 });
5555

56+
// Start server on Cloudflare Workers
57+
export default {
58+
fetch: app.handleCloudflare,
59+
};
60+
5661
console.log('Server running at http://localhost:3000');
5762
```
5863

packages/core/jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rabbit-company/web",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"license": "MIT",
55
"exports": "./src/index.ts",
66
"publish": {

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rabbit-company/web",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "High-performance web framework",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

0 commit comments

Comments
 (0)