-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy path_worker.js
122 lines (109 loc) · 4.49 KB
/
_worker.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
let api_key = '';//你的apikey
let cache_time = 10;//缓存时间(秒)
const url = 'https://api.uptimerobot.com/v2/getMonitors';
export default {
async fetch(request, env) {
api_key = env.api_key ;
cache_time = env.cache_time ? env.cache_time : 10;
console.log(cache_time);
const url = new URL(request.url);
if (url.pathname.startsWith('/api')) {
if (request.method === 'OPTIONS') {
return new Response(null, {
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/json;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Expose-Headers': '*'
}
})
} else if (request.method === 'POST') {
return await handleRequest(request,env);
} else {
return new Response(null, {
status: 405,
statusText: 'Method Not Allowed',
})
}
}
// Otherwise, serve the static assets.
// Without this, the Worker will error and no assets will be served.
return env.ASSETS.fetch(request);
},
}
async function gatherResponse(response) {
const { headers } = response;
const contentType = headers.get('content-type') || '';
if (contentType.includes('application/json')) {
return JSON.stringify(await response.json());
}
return response.text();
}
async function readRequestBody(request) {
const { headers } = request;
const contentType = headers.get('content-type') || '';
if (contentType.includes('application/json')) {
return JSON.stringify(await request.json());
} else if (contentType.includes('application/text')) {
return request.text();
} else if (contentType.includes('text/html')) {
return request.text();
} else if (contentType.includes('form')) {
const formData = await request.formData();
const body = {};
for (const entry of formData.entries()) {
body[entry[0]] = entry[1];
}
return JSON.stringify(body);
} else {
return 'a file';
}
}
async function handleRequest(request,env) {
const timestamp = (Date.parse(new Date()))/1000;
const lasttime = await env.statuslive.get("statuslive_lasttime");
let results = '';
let cache_tag = '';
if (timestamp - lasttime <= cache_time) {
results = await env.statuslive.get("statuslive_json_cache");
cache_tag = 'Cache Hit - Time:'+lasttime;
}else{
const post_data = JSON.parse(await readRequestBody(request));
post_data.api_key = api_key;
const init = {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
method: 'POST',
body: await JSON.stringify(post_data)
};
const response = await fetch(url, init);
results = await JSON.parse(await gatherResponse(response));
for (let index = 0; index < results.monitors.length; index++) {
results.monitors[index].url = "";
results.monitors[index].http_username = "";
results.monitors[index].http_password = "";
results.monitors[index].port = "";
}
results = await JSON.stringify(results);
await env.statuslive.put("statuslive_json_cache", results, {expirationTtl: (cache_time < 60 ? 60 : cache_time)})
await env.statuslive.put("statuslive_lasttime", timestamp.toString(), {expirationTtl: (cache_time < 60 ? 60 : cache_time)})
cache_tag = 'Cache Miss - Time:'+timestamp;
}
let response_init = {
headers: {
'content-type': 'application/json;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Expose-Headers': '*',
'StatusLive-Cache-Tag' : cache_tag
}
}
return new Response(results, response_init);
}