From ff2163b207a5acd773d85e92a53c8272fdad8485 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 26 Oct 2024 14:28:04 +0800 Subject: [PATCH] feat: update random-api-go service and UI with stats display --- docker-compose.yml | 4 +- main.go | 16 +++---- public/index.html | 103 ++++++++++++++++++++++++++++++++++++++++++++- public/index.md | 6 +++ 4 files changed, 119 insertions(+), 10 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c3534d9..b2adf92 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,13 @@ services: - random-api: + random-api-go: + container_name: random-api-go image: woodchen/random-api-go:latest ports: - "5003:5003" volumes: - ./public:/root/public - ./logs:/var/log/random-api + - ./data:/root/data environment: - TZ=Asia/Shanghai restart: unless-stopped \ No newline at end of file diff --git a/main.go b/main.go index 6ba70c0..79db0aa 100644 --- a/main.go +++ b/main.go @@ -180,11 +180,11 @@ func getCSVContent(path string) (*URLSelector, error) { } fullPath := filepath.Join("public", path) - log.Printf("Attempting to read file: %s", fullPath) + log.Printf("尝试读取文件: %s", fullPath) fileContent, err := os.ReadFile(fullPath) if err != nil { - return nil, fmt.Errorf("error reading CSV content: %w", err) + return nil, fmt.Errorf("读取 CSV 内容时出错: %w", err) } lines := strings.Split(string(fileContent), "\n") @@ -225,8 +225,8 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) { if time.Since(lastFetchTime) > cacheDuration { if err := loadCSVPaths(); err != nil { - http.Error(w, "Failed to load CSV paths", http.StatusInternalServerError) - log.Printf("Error loading CSV paths: %v", err) + http.Error(w, "无法加载 CSV 路径", http.StatusInternalServerError) + log.Printf("加载 CSV 路径时出错: %v", err) return } } @@ -253,13 +253,13 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) { selector, err := getCSVContent(csvPath) if err != nil { - http.Error(w, "Failed to fetch CSV content", http.StatusInternalServerError) - log.Printf("Error fetching CSV content: %v", err) + http.Error(w, "无法获取 CSV 内容", http.StatusInternalServerError) + log.Printf("获取 CSV 内容时出错: %v", err) return } if len(selector.URLs) == 0 { - http.Error(w, "No content available", http.StatusNotFound) + http.Error(w, "无可用内容", http.StatusNotFound) return } @@ -270,7 +270,7 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) { statsManager.IncrementCalls(endpoint) duration := time.Since(start) - log.Printf("Request: %s %s from %s - Source: %s - Duration: %v - Redirecting to: %s", + log.Printf("请求:%s %s,来自 %s -来源:%s -持续时间:%v -重定向至:%s", r.Method, r.URL.Path, realIP, sourceDomain, duration, randomURL) http.Redirect(w, r, randomURL, http.StatusFound) diff --git a/public/index.html b/public/index.html index dff1639..4446dbb 100644 --- a/public/index.html +++ b/public/index.html @@ -54,6 +54,32 @@ max-width: 100%; height: auto; } + + .stats-summary { + background-color: #f5f5f5; + padding: 15px; + border-radius: 8px; + margin: 20px 0; + } + + .stats-grid { + display: grid; + grid-template-columns: repeat(2, 1fr); + /* 两列布局 */ + gap: 10px; + /* 项目之间的间距 */ + } + + .stats-item { + padding: 5px; + } + + /* 确保在小屏幕上切换为单列 */ + @media (max-width: 600px) { + .stats-grid { + grid-template-columns: 1fr; + } + } @@ -72,6 +98,81 @@ html: true }); + // 加载统计数据 + async function loadStats() { + try { + const response = await fetch('/stats'); + const stats = await response.json(); + updateStats(stats); + } catch (error) { + console.error('Error loading stats:', error); + } + } + + // 处理统计数据 + function updateStats(stats) { + const startDate = new Date('2024-10-26'); + const today = new Date(); + const daysSinceStart = Math.ceil((today - startDate) / (1000 * 60 * 60 * 24)); + + let totalCalls = 0; + let todayCalls = 0; + + Object.values(stats).forEach(stat => { + totalCalls += stat.total_calls; + todayCalls += stat.today_calls; + }); + + const avgCallsPerDay = Math.round(totalCalls / daysSinceStart); + + // 更新总体统计 + const summaryHtml = ` +
+

📊 接口调用统计

+
+
今日总调用:${todayCalls} 次
+
平均每天调用:${avgCallsPerDay} 次
+
总调用次数:${totalCalls} 次
+
统计开始日期:2024-10-26
+
+
+ `; + + // 更新详细统计 + let detailHtml = ` +
+ + + + + + + + + + `; + + Object.entries(stats).forEach(([endpoint, stat]) => { + detailHtml += ` + + + + + + `; + }); + + detailHtml += ` + +
接口今日调用总调用
${endpoint}${stat.today_calls}${stat.total_calls}
+
+ `; + + // 插入统计数据 + document.getElementById('stats-summary').innerHTML = summaryHtml; + document.getElementById('stats-detail').innerHTML = detailHtml; + } + // 异步加载 index.md 文件内容 fetch('./index.md') .then(response => response.text()) @@ -83,4 +184,4 @@ - + \ No newline at end of file diff --git a/public/index.md b/public/index.md index 1a5b978..1afe638 100644 --- a/public/index.md +++ b/public/index.md @@ -1,5 +1,11 @@ # Random-Api 随机文件API +## 接口统计 +
+ +## 详细统计 +
+ ## 图片接口 | 种类 | 请求地址 |