-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_worker.js
More file actions
36 lines (30 loc) · 873 Bytes
/
Copy path_worker.js
File metadata and controls
36 lines (30 loc) · 873 Bytes
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
const CARS = [
"site1.example.com",
"site2.example.com",
"site3.example.com"
];
function selectHost(strategy = "random") {
if (strategy === "random") {
return CARS[Math.trunc(Math.random() * CARS.length)];
} else if (strategy === "daily") {
return CARS[new Date().getDate() % CARS.length];
}
return CARS[0]; // 默认策略
}
export default {
async fetch(request, env) {
try {
const host = selectHost("random"); // 选择策略
console.log(`Request routed to: ${host}`);
let url = new URL(request.url);
if (url.pathname.startsWith('/')) {
url.hostname = host;
let new_request = new Request(url, request);
return fetch(new_request);
}
return env.ASSETS.fetch(request);
} catch (err) {
return new Response("Error occurred: " + err.message, { status: 500 });
}
}
};