From ba25817111888b5a03325774eed206449b47a3a6 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 27 Oct 2024 02:02:15 +0800 Subject: [PATCH] feat: add support for CSV_BASE_URL environment variable and update file reading logic --- docker-compose.yml | 1 + main.go | 53 +++++++++++++++++++++++++++++++++++++++++----- readme.md | 35 +----------------------------- utils/utils.go | 18 ++++++++++++++++ 4 files changed, 68 insertions(+), 39 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b2adf92..d04aaa3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,4 +10,5 @@ services: - ./data:/root/data environment: - TZ=Asia/Shanghai + - CSV_BASE_URL=https://example.com/csvfile restart: unless-stopped \ No newline at end of file diff --git a/main.go b/main.go index 5570145..2ddb9de 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "io" "log" "math/rand" "net/http" @@ -23,6 +24,7 @@ const ( port = ":5003" requestTimeout = 10 * time.Second noRepeatCount = 3 // 在这个次数内不重复选择 + envCSVBaseURL = "CSV_BASE_URL" ) var ( @@ -160,12 +162,53 @@ func getCSVContent(path string) (*URLSelector, error) { return selector, nil } - fullPath := filepath.Join("public", path) - log.Printf("尝试读取文件: %s", fullPath) + var fileContent []byte + var err error - fileContent, err := os.ReadFile(fullPath) - if err != nil { - return nil, fmt.Errorf("读取 CSV 内容时出错: %w", err) + // 获取环境变量中的基础URL + baseURL := os.Getenv(envCSVBaseURL) + + if baseURL != "" { + // 如果设置了基础URL,构建完整的URL + var fullURL string + if strings.HasPrefix(baseURL, "http://") || strings.HasPrefix(baseURL, "https://") { + // 如果baseURL已经包含协议,直接使用 + fullURL = utils.JoinURLPath(baseURL, path) + } else { + // 如果没有协议,添加https:// + fullURL = "https://" + utils.JoinURLPath(baseURL, path) + } + + log.Printf("尝试从URL获取: %s", fullURL) + + // 创建HTTP客户端 + client := &http.Client{ + Timeout: requestTimeout, + } + + resp, err := client.Get(fullURL) + if err != nil { + return nil, fmt.Errorf("HTTP请求失败: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("HTTP请求返回非200状态码: %d", resp.StatusCode) + } + + fileContent, err = io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("读取响应内容失败: %w", err) + } + } else { + // 如果没有设置基础URL,从本地文件读取 + fullPath := filepath.Join("public", path) + log.Printf("尝试读取本地文件: %s", fullPath) + + fileContent, err = os.ReadFile(fullPath) + if err != nil { + return nil, fmt.Errorf("读取CSV内容时出错: %w", err) + } } lines := strings.Split(string(fileContent), "\n") diff --git a/readme.md b/readme.md index 4570582..c666274 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Random API -一些介绍: https://q58.org/t/topic/127 +介绍,使用方法和更新记录: https://q58.org/t/topic/127 Random API 是一个用 Go 语言编写的简单而强大的随机图片/视频 API 服务。它允许用户通过配置文件轻松管理和提供随机媒体内容。 @@ -12,39 +12,6 @@ Random API 是一个用 Go 语言编写的简单而强大的随机图片/视频 - Docker 支持,便于部署和扩展 - 详细的日志记录 -## 快速开始 - -### 使用 Docker Compose - -1. 克隆仓库: - ``` - git clone https://github.com/yourusername/random-api.git - cd random-api - ``` - -2. 创建并编辑 `public/url.json` 文件来配置你的 URL 路径。 - -3. 启动服务: - ``` - docker-compose up -d - ``` - -4. 访问 `http://localhost:5003` 来使用 API。 - -### 手动运行 - -1. 确保你已安装 Go 1.21 或更高版本。 - -2. 克隆仓库并进入项目目录。 - -3. 运行以下命令: - ``` - go mod download - go run main.go - ``` - -4. 服务将在 `http://localhost:5003` 上运行。 - ## 配置 ### url.json diff --git a/utils/utils.go b/utils/utils.go index 920adda..0c92268 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -26,3 +26,21 @@ func GetRealIP(r *http.Request) string { } return ip } + +// 添加一个处理URL路径的工具函数 +func JoinURLPath(parts ...string) string { + // 过滤空字符串 + var nonEmptyParts []string + for _, part := range parts { + if part != "" { + // 去除首尾的"/" + part = strings.Trim(part, "/") + if part != "" { + nonEmptyParts = append(nonEmptyParts, part) + } + } + } + + // 用"/"连接所有部分 + return strings.Join(nonEmptyParts, "/") +}