mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
refactor(main): remove unused cache duration and last fetch time
This commit is contained in:
parent
96f41e374f
commit
0537e46ef6
20
main.go
20
main.go
@ -21,14 +21,12 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
port = ":5003"
|
port = ":5003"
|
||||||
cacheDuration = 24 * time.Hour
|
|
||||||
requestTimeout = 10 * time.Second
|
requestTimeout = 10 * time.Second
|
||||||
noRepeatCount = 3 // 在这个次数内不重复选择
|
noRepeatCount = 3 // 在这个次数内不重复选择
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
csvPathsCache map[string]map[string]string
|
csvPathsCache map[string]map[string]string
|
||||||
lastFetchTime time.Time
|
|
||||||
csvCache = make(map[string]*URLSelector)
|
csvCache = make(map[string]*URLSelector)
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
rng *rand.Rand
|
rng *rand.Rand
|
||||||
@ -148,7 +146,6 @@ func loadCSVPaths() error {
|
|||||||
|
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
csvPathsCache = result
|
csvPathsCache = result
|
||||||
lastFetchTime = time.Now()
|
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|
||||||
log.Println("CSV paths loaded from url.json")
|
log.Println("CSV paths loaded from url.json")
|
||||||
@ -196,7 +193,6 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
realIP := utils.GetRealIP(r)
|
realIP := utils.GetRealIP(r)
|
||||||
referer := r.Referer()
|
referer := r.Referer()
|
||||||
|
|
||||||
// 获取来源域名
|
|
||||||
var sourceDomain string
|
var sourceDomain string
|
||||||
if referer != "" {
|
if referer != "" {
|
||||||
if parsedURL, err := url.Parse(referer); err == nil {
|
if parsedURL, err := url.Parse(referer); err == nil {
|
||||||
@ -207,14 +203,6 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
sourceDomain = "direct"
|
sourceDomain = "direct"
|
||||||
}
|
}
|
||||||
|
|
||||||
if time.Since(lastFetchTime) > cacheDuration {
|
|
||||||
if err := loadCSVPaths(); err != nil {
|
|
||||||
http.Error(w, "无法加载 CSV 路径", http.StatusInternalServerError)
|
|
||||||
log.Printf("加载 CSV 路径时出错: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
path := strings.TrimPrefix(r.URL.Path, "/")
|
||||||
pathSegments := strings.Split(path, "/")
|
pathSegments := strings.Split(path, "/")
|
||||||
|
|
||||||
@ -237,13 +225,13 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
selector, err := getCSVContent(csvPath)
|
selector, err := getCSVContent(csvPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "无法获取 CSV 内容", http.StatusInternalServerError)
|
http.Error(w, "Failed to fetch CSV content", http.StatusInternalServerError)
|
||||||
log.Printf("获取 CSV 内容时出错: %v", err)
|
log.Printf("Error fetching CSV content: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(selector.URLs) == 0 {
|
if len(selector.URLs) == 0 {
|
||||||
http.Error(w, "无可用内容", http.StatusNotFound)
|
http.Error(w, "No content available", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,7 +242,7 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
statsManager.IncrementCalls(endpoint)
|
statsManager.IncrementCalls(endpoint)
|
||||||
|
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
log.Printf("请求:%s %s,来自 %s -来源:%s -持续时间:%v -重定向至:%s",
|
log.Printf("请求:%s %s,来自 %s -来源:%s -持续时间: %v - 重定向至: %s",
|
||||||
r.Method, r.URL.Path, realIP, sourceDomain, duration, randomURL)
|
r.Method, r.URL.Path, realIP, sourceDomain, duration, randomURL)
|
||||||
|
|
||||||
http.Redirect(w, r, randomURL, http.StatusFound)
|
http.Redirect(w, r, randomURL, http.StatusFound)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user