Skip to content

Commit

Permalink
refactor(main): remove unused cache duration and last fetch time
Browse files Browse the repository at this point in the history
  • Loading branch information
woodchen-ink committed Oct 26, 2024
1 parent 96f41e3 commit 0537e46
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import (

const (
port = ":5003"
cacheDuration = 24 * time.Hour
requestTimeout = 10 * time.Second
noRepeatCount = 3 // 在这个次数内不重复选择
)

var (
csvPathsCache map[string]map[string]string
lastFetchTime time.Time
csvCache = make(map[string]*URLSelector)
mu sync.RWMutex
rng *rand.Rand
Expand Down Expand Up @@ -148,7 +146,6 @@ func loadCSVPaths() error {

mu.Lock()
csvPathsCache = result
lastFetchTime = time.Now()
mu.Unlock()

log.Println("CSV paths loaded from url.json")
Expand Down Expand Up @@ -196,7 +193,6 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
realIP := utils.GetRealIP(r)
referer := r.Referer()

// 获取来源域名
var sourceDomain string
if referer != "" {
if parsedURL, err := url.Parse(referer); err == nil {
Expand All @@ -207,14 +203,6 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
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, "/")
pathSegments := strings.Split(path, "/")

Expand All @@ -237,13 +225,13 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {

selector, err := getCSVContent(csvPath)
if err != nil {
http.Error(w, "无法获取 CSV 内容", http.StatusInternalServerError)
log.Printf("获取 CSV 内容时出错: %v", err)
http.Error(w, "Failed to fetch CSV content", http.StatusInternalServerError)
log.Printf("Error fetching CSV content: %v", err)
return
}

if len(selector.URLs) == 0 {
http.Error(w, "无可用内容", http.StatusNotFound)
http.Error(w, "No content available", http.StatusNotFound)
return
}

Expand All @@ -254,7 +242,7 @@ func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
statsManager.IncrementCalls(endpoint)

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)

http.Redirect(w, r, randomURL, http.StatusFound)
Expand Down

0 comments on commit 0537e46

Please sign in to comment.