forked from tinyslash-tech/tinyslash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare-worker-proxy.js
More file actions
76 lines (67 loc) · 2.25 KB
/
cloudflare-worker-proxy.js
File metadata and controls
76 lines (67 loc) · 2.25 KB
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
76
// Cloudflare Worker for Universal Custom Domain Proxy
// This handles ALL custom domains pointing to your service
export default {
async fetch(request, env) {
const url = new URL(request.url);
const hostname = url.hostname;
const pathname = url.pathname;
// Skip if it's your main domain
if (hostname === 'pebly.vercel.app') {
return fetch(request);
}
// For all custom domains, proxy to your backend
const backendUrl = 'https://urlshortner-1-hpyu.onrender.com';
// Create new request to your backend
const backendRequest = new Request(`${backendUrl}${pathname}${url.search}`, {
method: request.method,
headers: {
...request.headers,
'Host': hostname, // Pass original hostname
'X-Forwarded-Host': hostname,
'X-Original-Host': hostname
},
body: request.body
});
try {
const response = await fetch(backendRequest);
// If it's a redirect response, return it directly
if (response.status >= 300 && response.status < 400) {
return response;
}
// For other responses, return with CORS headers
const newResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
...response.headers,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': '*'
}
});
return newResponse;
} catch (error) {
// Fallback error page
return new Response(`
<!DOCTYPE html>
<html>
<head>
<title>Link Not Found</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
.error { color: #e74c3c; }
</style>
</head>
<body>
<h1 class="error">Link Not Found</h1>
<p>The short link you're looking for doesn't exist or has expired.</p>
<p><a href="https://pebly.vercel.app">Create your own short links</a></p>
</body>
</html>
`, {
status: 404,
headers: { 'Content-Type': 'text/html' }
});
}
}
};