Repository navigation
Expand file tree
/
Copy pathcors-test.html
More file actions
109 lines (100 loc) · 4.72 KB
/
Copy pathcors-test.html
File metadata and controls
109 lines (100 loc) · 4.72 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
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CORS 和 Redis VPC 连接测试</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
}
button {
background: #4285f4;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
margin: 10px 5px;
}
button:hover {
background: #357ae8;
}
.result {
background: #f5f5f5;
padding: 15px;
border-radius: 5px;
margin: 10px 0;
white-space: pre-wrap;
word-break: break-all;
}
.success {
border-left: 4px solid #4caf50;
}
.error {
border-left: 4px solid #f44336;
}
</style>
</head>
<body>
<h1>Firebase Functions - 连接测试</h1>
<button onclick="testHttpsEndpoint()">测试旧的 testEndpoint (HTTPS)</button>
<button onclick="testCallableFunction()">测试 compareLatency (Callable)</button>
<button onclick="testRedisVPCDirectly()">测试 Redis VPC 连接 (直接 HTTP)</button>
<div id="results"></div>
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.13.0/firebase-app.js';
import { getFunctions, httpsCallable } from 'https://www.gstatic.com/firebasejs/10.13.0/firebase-functions.js';
const firebaseConfig = {
apiKey: "AIzaSyCMT4t0NMSjXgI9clqALB6uguqY76ul8Iw",
authDomain: "taptap-ugc.firebaseapp.com", // 确保项目 ID 正确
databaseURL: "https://taptap-ugc-default-rtdb.asia-southeast1.firebasedatabase.app", // 可能需要根据项目调整
projectId: "taptap-ugc",
storageBucket: "taptap-ugc.appspot.com", // 确保项目 ID 正确
messagingSenderId: "519191676565", // 确保是 taptap-ugc 的发送者 ID
appId: "1:519191676565:web:YOUR_APP_ID", // !! 请替换为 taptap-ugc 项目的 Web App ID
measurementId: "G-YOUR_MEASUREMENT_ID" // 可选,taptap-ugc 的分析 ID
};
const app = initializeApp(firebaseConfig);
const functions = getFunctions(app, 'asia-southeast1');
const resultsDiv = document.getElementById('results');
function displayResult(data, type = 'success', header = '成功') {
const content = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
resultsDiv.innerHTML = `<div class="result ${type}"><strong>${header}:</strong>\n${content}</div>` + resultsDiv.innerHTML;
}
window.testHttpsEndpoint = async function() {
// 这个函数可能不再相关或需要更新 URL
displayResult('testEndpoint 已被移除或更改。', 'error', '旧 testEndpoint');
};
window.testCallableFunction = async function() {
try {
const compareLatency = httpsCallable(functions, 'compareLatency');
const result = await compareLatency({ testSize: 1, rounds: 1, enableRedis: false }); // 用最小参数测试
displayResult(result.data, 'success', 'compareLatency (Callable)');
} catch (error) {
console.error('Callable function error:', error);
displayResult(`${error.message}\nCode: ${error.code}\nDetails: ${JSON.stringify(error.details)}`, 'error', 'compareLatency (Callable) 失败');
}
};
window.testRedisVPCDirectly = async function() {
const redisTestUrl = 'https://testredisvpcconnection-4srt7w2jta-as.a.run.app'; // 使用 Cloud Run URL
// 或者 Firebase Functions URL: 'https://asia-southeast1-taptap-ugc.cloudfunctions.net/testRedisVPCConnection';
displayResult(`正在调用: ${redisTestUrl}`, 'info', '测试 Redis VPC 连接');
try {
const response = await fetch(redisTestUrl, { method: 'GET' }); // 使用 GET
const responseText = await response.text(); // 获取文本响应
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status} - ${responseText}`);
}
displayResult(responseText, 'success', 'Redis VPC 连接测试 (直接 HTTP)');
} catch (error) {
console.error('Redis VPC direct HTTP error:', error);
displayResult(error.message, 'error', 'Redis VPC 连接测试 (直接 HTTP) 失败');
}
};
</script>
</body>
</html>