Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test-api.html 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>API测试工具 - svc-code</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  15. line-height: 1.6;
  16. color: #333;
  17. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  18. min-height: 100vh;
  19. padding: 20px;
  20. }
  21. .container {
  22. max-width: 1000px;
  23. margin: 0 auto;
  24. background: white;
  25. border-radius: 12px;
  26. box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  27. overflow: hidden;
  28. }
  29. .header {
  30. background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  31. color: white;
  32. padding: 30px;
  33. text-align: center;
  34. }
  35. .header h1 {
  36. font-size: 2.5rem;
  37. margin-bottom: 10px;
  38. }
  39. .header p {
  40. opacity: 0.9;
  41. font-size: 1.1rem;
  42. }
  43. .content {
  44. padding: 30px;
  45. }
  46. .section {
  47. margin-bottom: 30px;
  48. border: 1px solid #e5e7eb;
  49. border-radius: 8px;
  50. overflow: hidden;
  51. }
  52. .section-header {
  53. background: #f9fafb;
  54. padding: 15px 20px;
  55. border-bottom: 1px solid #e5e7eb;
  56. font-weight: 600;
  57. color: #374151;
  58. }
  59. .section-body {
  60. padding: 20px;
  61. }
  62. .config-info {
  63. background: #f0f9ff;
  64. border: 1px solid #bae6fd;
  65. border-radius: 6px;
  66. padding: 15px;
  67. margin-bottom: 20px;
  68. }
  69. .config-info code {
  70. background: #e0f2fe;
  71. padding: 2px 6px;
  72. border-radius: 4px;
  73. font-family: 'Monaco', 'Menlo', monospace;
  74. font-size: 0.9em;
  75. }
  76. .buttons {
  77. display: flex;
  78. gap: 10px;
  79. flex-wrap: wrap;
  80. margin-bottom: 20px;
  81. }
  82. button {
  83. padding: 12px 24px;
  84. border: none;
  85. border-radius: 6px;
  86. font-size: 1rem;
  87. font-weight: 500;
  88. cursor: pointer;
  89. transition: all 0.2s;
  90. display: flex;
  91. align-items: center;
  92. gap: 8px;
  93. }
  94. button:hover {
  95. transform: translateY(-2px);
  96. box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  97. }
  98. button:active {
  99. transform: translateY(0);
  100. }
  101. .btn-primary {
  102. background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  103. color: white;
  104. }
  105. .btn-secondary {
  106. background: #f3f4f6;
  107. color: #374151;
  108. border: 1px solid #d1d5db;
  109. }
  110. .btn-success {
  111. background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  112. color: white;
  113. }
  114. .btn-danger {
  115. background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  116. color: white;
  117. }
  118. .btn:disabled {
  119. opacity: 0.5;
  120. cursor: not-allowed;
  121. transform: none !important;
  122. box-shadow: none !important;
  123. }
  124. .output {
  125. background: #1e293b;
  126. color: #e2e8f0;
  127. border-radius: 6px;
  128. padding: 15px;
  129. font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  130. font-size: 0.9em;
  131. line-height: 1.5;
  132. white-space: pre-wrap;
  133. max-height: 400px;
  134. overflow-y: auto;
  135. margin-top: 20px;
  136. }
  137. .status {
  138. padding: 10px 15px;
  139. border-radius: 6px;
  140. margin-bottom: 15px;
  141. display: none;
  142. }
  143. .status.success {
  144. background: #d1fae5;
  145. color: #065f46;
  146. border: 1px solid #a7f3d0;
  147. display: block;
  148. }
  149. .status.error {
  150. background: #fee2e2;
  151. color: #991b1b;
  152. border: 1px solid #fecaca;
  153. display: block;
  154. }
  155. .status.info {
  156. background: #dbeafe;
  157. color: #1e40af;
  158. border: 1px solid #bfdbfe;
  159. display: block;
  160. }
  161. .instructions {
  162. background: #fef3c7;
  163. border: 1px solid #fde68a;
  164. border-radius: 6px;
  165. padding: 15px;
  166. margin-top: 20px;
  167. }
  168. .instructions h3 {
  169. color: #92400e;
  170. margin-bottom: 10px;
  171. }
  172. .instructions ul {
  173. padding-left: 20px;
  174. }
  175. .instructions li {
  176. margin-bottom: 5px;
  177. }
  178. .log-item {
  179. padding: 5px 0;
  180. border-bottom: 1px solid #2d3748;
  181. }
  182. .log-item:last-child {
  183. border-bottom: none;
  184. }
  185. .log-time {
  186. color: #94a3b8;
  187. margin-right: 10px;
  188. }
  189. .log-level {
  190. font-weight: bold;
  191. margin-right: 10px;
  192. }
  193. .log-level.info { color: #60a5fa; }
  194. .log-level.success { color: #34d399; }
  195. .log-level.error { color: #f87171; }
  196. .log-level.warn { color: #fbbf24; }
  197. @media (max-width: 768px) {
  198. .container {
  199. margin: 10px;
  200. }
  201. .header h1 {
  202. font-size: 2rem;
  203. }
  204. .buttons {
  205. flex-direction: column;
  206. }
  207. button {
  208. width: 100%;
  209. justify-content: center;
  210. }
  211. }
  212. </style>
  213. </head>
  214. <body>
  215. <div class="container">
  216. <div class="header">
  217. <h1>🔧 API测试工具</h1>
  218. <p>svc-code 后端API独立测试</p>
  219. </div>
  220. <div class="content">
  221. <!-- 配置信息 -->
  222. <div class="section">
  223. <div class="section-header">📋 配置信息</div>
  224. <div class="section-body">
  225. <div class="config-info">
  226. <p><strong>后端服务:</strong> <code id="backend-url">http://localhost:8020</code></p>
  227. <p><strong>API前缀:</strong> <code>/api</code></p>
  228. <p><strong>测试端点:</strong> <code>/api/health</code>, <code>/api/projects</code></p>
  229. <p><strong>状态:</strong> <span id="connection-status">等待测试...</span></p>
  230. </div>
  231. </div>
  232. </div>
  233. <!-- 状态显示 -->
  234. <div id="status-area"></div>
  235. <!-- 测试控制 -->
  236. <div class="section">
  237. <div class="section-header">🚀 测试控制</div>
  238. <div class="section-body">
  239. <div class="buttons">
  240. <button id="btn-test-all" class="btn-primary">
  241. <span>▶️</span> 运行所有测试
  242. </button>
  243. <button id="btn-test-health" class="btn-secondary">
  244. <span>❤️</span> 健康检查
  245. </button>
  246. <button id="btn-test-projects" class="btn-success">
  247. <span>📁</span> 项目列表
  248. </button>
  249. <button id="btn-clear" class="btn-danger">
  250. <span>🗑️</span> 清空日志
  251. </button>
  252. </div>
  253. <div id="output" class="output">
  254. <div id="logs">
  255. <div class="log-item">
  256. <span class="log-time" id="current-time">--:--:--</span>
  257. <span class="log-level info">[INFO]</span>
  258. <span>API测试工具已就绪!</span>
  259. </div>
  260. <div class="log-item">
  261. <span class="log-time">--:--:--</span>
  262. <span class="log-level info">[INFO]</span>
  263. <span>点击上方按钮开始测试</span>
  264. </div>
  265. </div>
  266. </div>
  267. </div>
  268. </div>
  269. <!-- 使用说明 -->
  270. <div class="section">
  271. <div class="section-header">📖 使用说明</div>
  272. <div class="section-body">
  273. <div class="instructions">
  274. <h3>如何运行测试:</h3>
  275. <ul>
  276. <li><strong>运行所有测试</strong>:点击"运行所有测试"按钮,依次测试健康检查和项目列表API</li>
  277. <li><strong>单独测试</strong>:点击"健康检查"或"项目列表"按钮测试特定API</li>
  278. <li><strong>控制台测试</strong>:在浏览器控制台中输入 <code>window.testAPI.runAllTests()</code></li>
  279. </ul>
  280. <h3>前提条件:</h3>
  281. <ul>
  282. <li>后端服务必须正在运行(端口8020)</li>
  283. <li>确保没有跨域问题(CORS已配置)</li>
  284. <li>如果测试失败,请检查后端日志</li>
  285. </ul>
  286. <h3>快速命令:</h3>
  287. <pre style="background:#1e293b;color:#e2e8f0;padding:10px;border-radius:4px;margin-top:10px;">
  288. # 启动后端服务
  289. cd svc-code && go run main.go
  290. # 测试API(命令行)
  291. curl http://localhost:8020/api/health
  292. curl http://localhost:8020/api/projects</pre>
  293. </div>
  294. </div>
  295. </div>
  296. </div>
  297. </div>
  298. <script type="module">
  299. // 更新时间显示
  300. function updateTime() {
  301. const now = new Date();
  302. const timeStr = now.toTimeString().split(' ')[0];
  303. document.querySelectorAll('#current-time').forEach(el => {
  304. el.textContent = timeStr;
  305. });
  306. }
  307. setInterval(updateTime, 1000);
  308. updateTime();
  309. // 添加日志
  310. function addLog(level, message) {
  311. const now = new Date();
  312. const timeStr = now.toTimeString().split(' ')[0];
  313. const logItem = document.createElement('div');
  314. logItem.className = 'log-item';
  315. logItem.innerHTML = `
  316. <span class="log-time">${timeStr}</span>
  317. <span class="log-level ${level}">[${level.toUpperCase()}]</span>
  318. <span>${message}</span>
  319. `;
  320. document.getElementById('logs').prepend(logItem);
  321. // 限制日志数量
  322. const logs = document.getElementById('logs');
  323. if (logs.children.length > 50) {
  324. logs.removeChild(logs.lastChild);
  325. }
  326. }
  327. // 显示状态
  328. function showStatus(type, message) {
  329. const statusArea = document.getElementById('status-area');
  330. const statusDiv = document.createElement('div');
  331. statusDiv.className = `status ${type}`;
  332. statusDiv.textContent = message;
  333. statusArea.innerHTML = '';
  334. statusArea.appendChild(statusDiv);
  335. }
  336. // API请求函数
  337. async function apiRequest(endpoint) {
  338. const backendUrl = document.getElementById('backend-url').textContent;
  339. const url = `${backendUrl}/api${endpoint}`;
  340. const startTime = Date.now();
  341. try {
  342. const response = await fetch(url);
  343. const duration = Date.now() - startTime;
  344. if (!response.ok) {
  345. throw new Error(`HTTP ${response.status}`);
  346. }
  347. const data = await response.json();
  348. return { success: true, data, duration };
  349. } catch (error) {
  350. const duration = Date.now() - startTime;
  351. return { success: false, error: error.message, duration };
  352. }
  353. }
  354. // 测试函数
  355. async function testHealth() {
  356. showStatus('info', '正在测试健康检查API...');
  357. addLog('info', '开始健康检查测试');
  358. const result = await apiRequest('/health');
  359. if (result.success) {
  360. const isHealthy = result.data?.data?.healthy || result.data?.healthy;
  361. const message = `健康检查成功!状态: ${isHealthy ? '健康' : '不健康'}, 耗时: ${result.duration}ms`;
  362. showStatus('success', message);
  363. addLog('success', message);
  364. return true;
  365. } else {
  366. const message = `健康检查失败: ${result.error}`;
  367. showStatus('error', message);
  368. addLog('error', message);
  369. return false;
  370. }
  371. }
  372. async function testProjects() {
  373. showStatus('info', '正在测试项目列表API...');
  374. addLog('info', '开始项目列表测试');
  375. const result = await apiRequest('/projects');
  376. if (result.success) {
  377. const projects = result.data?.data || result.data || [];
  378. const message = `获取到 ${projects.length} 个项目, 耗时: ${result.duration}ms`;
  379. showStatus('success', message);
  380. addLog('success', message);
  381. // 显示项目详情
  382. if (projects.length > 0) {
  383. projects.forEach((project, index) => {
  384. addLog('info', `项目 ${index + 1}: ${project.name} (${project.id})`);
  385. });
  386. }
  387. return true;
  388. } else {
  389. const message = `项目列表测试失败: ${result.error}`;
  390. showStatus('error', message);
  391. addLog('error', message);
  392. return false;
  393. }
  394. }
  395. async function runAllTests() {
  396. showStatus('info', '开始运行所有测试...');
  397. addLog('info', '='.repeat(50));
  398. addLog('info', '开始运行所有API测试');
  399. addLog('info', '='.repeat(50));
  400. // 测试健康检查
  401. const healthOk = await testHealth();
  402. // 只有健康检查通过才测试项目列表
  403. let projectsOk = false;
  404. if (healthOk) {
  405. projectsOk = await testProjects();
  406. } else {
  407. addLog('warn', '跳过项目列表测试(健康检查失败)');
  408. }
  409. // 显示结果
  410. addLog('info', '='.repeat(50));
  411. addLog('info', '测试结果汇总:');
  412. addLog('info', `健康检查: ${healthOk ? '✅ 通过' : '❌ 失败'}`);
  413. addLog('info', `项目列表: ${projectsOk ? '✅ 通过' : healthOk ? '❌ 失败' : '⏭️ 跳过'}`);
  414. const allPassed = healthOk && projectsOk;
  415. addLog(allPassed ? 'success' : 'warn',
  416. allPassed ? '🎉 所有测试通过!' : '⚠️ 部分测试失败');
  417. showStatus(allPassed ? 'success' : 'error',
  418. allPassed ? '所有测试通过!' : '部分测试失败');
  419. }
  420. // 按钮事件
  421. document.getElementById('btn-test-all').addEventListener('click', runAllTests);
  422. document.getElementById('btn-test-health').addEventListener('click', testHealth);
  423. document.getElementById('btn-test-projects').addEventListener('click', testProjects);
  424. document.getElementById('btn-clear').addEventListener('click', () => {
  425. document.getElementById('logs').innerHTML = `
  426. <div class="log-item">
  427. <span class="log-time" id="current-time">--:--:--</span>
  428. <span class="log-level info">[INFO]</span>
  429. <span>日志已清空</span>
  430. </div>
  431. `;
  432. addLog('info', '日志已清空');
  433. document.getElementById('status-area').innerHTML = '';
  434. });
  435. // 更新连接状态
  436. async function checkConnection() {
  437. const backendUrl = document.getElementById('backend-url').textContent;
  438. const statusEl = document.getElementById('connection-status');
  439. try {
  440. const response = await fetch(`${backendUrl}/api/health`, { signal: AbortSignal.timeout(3000) });
  441. if (response.ok) {
  442. statusEl.textContent = '✅ 连接正常';
  443. statusEl.style.color = '#10b981';
  444. } else {
  445. statusEl.textContent = '⚠️ 连接异常';
  446. statusEl.style.color = '#f59e0b';
  447. }
  448. } catch {
  449. statusEl.textContent = '❌ 无法连接';
  450. statusEl.style.color = '#ef4444';
  451. }
  452. }
  453. // 初始连接检查
  454. setTimeout(checkConnection, 1000);
  455. // 在控制台中注册测试函数
  456. window.testAPI = {
  457. runAllTests,
  458. testHealth,
  459. testProjects,
  460. checkConnection
  461. };
  462. addLog('success', '测试工具已初始化,在控制台输入 window.testAPI.runAllTests() 开始测试');
  463. </script>
  464. </body>
  465. </html>