mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
feat: add support for CSV_BASE_URL environment variable and update file reading logic
This commit is contained in:
parent
0537e46ef6
commit
ba25817111
@ -10,4 +10,5 @@ services:
|
||||
- ./data:/root/data
|
||||
environment:
|
||||
- TZ=Asia/Shanghai
|
||||
- CSV_BASE_URL=https://example.com/csvfile
|
||||
restart: unless-stopped
|
53
main.go
53
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")
|
||||
|
35
readme.md
35
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
|
||||
|
@ -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, "/")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user