Skip to content

Commit e13a8c2

Browse files
committed
feat: update main entry point to src/worker.ts and add UPSTREAM_ORIGIN variable
1 parent 598b51b commit e13a8c2

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/worker.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
type WorkerEnv = {
2+
UPSTREAM_ORIGIN?: string;
3+
};
4+
5+
function jsonResponse(body: unknown, status: number): Response {
6+
return new Response(JSON.stringify(body, null, 2), {
7+
status,
8+
headers: {
9+
'content-type': 'application/json; charset=utf-8',
10+
'cache-control': 'no-store',
11+
},
12+
});
13+
}
14+
15+
export default {
16+
async fetch(request: Request, env: WorkerEnv): Promise<Response> {
17+
const configuredOrigin = (env.UPSTREAM_ORIGIN || '').trim();
18+
19+
if (!configuredOrigin) {
20+
return jsonResponse(
21+
{
22+
error: 'UPSTREAM_ORIGIN is not configured',
23+
message:
24+
'Set UPSTREAM_ORIGIN in your Wrangler env vars to the Node API origin, for example https://api.example.com',
25+
},
26+
500,
27+
);
28+
}
29+
30+
const incomingUrl = new URL(request.url);
31+
const upstreamBase = new URL(configuredOrigin.endsWith('/') ? configuredOrigin : `${configuredOrigin}/`);
32+
const upstreamUrl = new URL(`${incomingUrl.pathname}${incomingUrl.search}`, upstreamBase);
33+
34+
const headers = new Headers(request.headers);
35+
headers.set('x-forwarded-host', incomingUrl.host);
36+
headers.delete('host');
37+
38+
const init: RequestInit = {
39+
method: request.method,
40+
headers,
41+
redirect: 'manual',
42+
body: request.method === 'GET' || request.method === 'HEAD' ? undefined : request.body,
43+
};
44+
45+
return fetch(upstreamUrl.toString(), init);
46+
},
47+
};

wrangler.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "github-stats"
2-
main = "dist/index.js"
2+
main = "src/worker.ts"
33
compatibility_date = "2024-12-16"
44
compatibility_flags = ["nodejs_compat"]
55

@@ -12,6 +12,7 @@ cwd = "."
1212
[vars]
1313
ENVIRONMENT = "cloudflare"
1414
PORT = "3000"
15+
UPSTREAM_ORIGIN = ""
1516

1617
# D1 Database Binding - SQLite for Cloudflare
1718
[[d1_databases]]
@@ -32,12 +33,13 @@ preview_id = "YOUR_BADGE_CACHE_PREVIEW_NAMESPACE_ID"
3233
# Development environment
3334
[env.development]
3435
name = "github-stats-dev"
35-
main = "dist/index.js"
36+
main = "src/worker.ts"
3637
compatibility_date = "2024-12-16"
3738

3839
[env.development.vars]
3940
ENVIRONMENT = "cloudflare-dev"
4041
DEBUG = "true"
42+
UPSTREAM_ORIGIN = ""
4143

4244
[[env.development.d1_databases]]
4345
binding = "DB"
@@ -57,12 +59,13 @@ preview_id = "YOUR_DEV_BADGE_CACHE_PREVIEW_NAMESPACE_ID"
5759
# Production environment
5860
[env.production]
5961
name = "github-stats-prod"
60-
main = "dist/index.js"
62+
main = "src/worker.ts"
6163
compatibility_date = "2024-12-16"
6264

6365
[env.production.vars]
6466
ENVIRONMENT = "cloudflare-production"
6567
DEBUG = "false"
68+
UPSTREAM_ORIGIN = ""
6669

6770
[[env.production.d1_databases]]
6871
binding = "DB"

0 commit comments

Comments
 (0)