mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
fix
This commit is contained in:
parent
7ee5c10ea3
commit
e10049f57c
23
main.go
23
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
@ -39,7 +40,13 @@ func main() {
|
|||||||
log.Fatal("Failed to load CSV paths:", err)
|
log.Fatal("Failed to load CSV paths:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/", handleRequest)
|
// 设置静态文件服务
|
||||||
|
fs := http.FileServer(http.Dir("./public"))
|
||||||
|
http.Handle("/", fs)
|
||||||
|
|
||||||
|
// 设置 API 路由
|
||||||
|
http.HandleFunc("/pic/", handleAPIRequest)
|
||||||
|
http.HandleFunc("/video/", handleAPIRequest)
|
||||||
|
|
||||||
log.Printf("Listening on %s...\n", port)
|
log.Printf("Listening on %s...\n", port)
|
||||||
if err := http.ListenAndServe(port, nil); err != nil {
|
if err := http.ListenAndServe(port, nil); err != nil {
|
||||||
@ -52,7 +59,8 @@ func setupLogging() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.SetOutput(logFile)
|
multiWriter := io.MultiWriter(os.Stdout, logFile)
|
||||||
|
log.SetOutput(multiWriter)
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,9 +140,9 @@ func getCSVContent(path string) ([]string, error) {
|
|||||||
return fileArray, nil
|
return fileArray, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
func handleAPIRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
|
||||||
realIP := getRealIP(r)
|
realIP := getRealIP(r)
|
||||||
|
log.Printf("Handling request from IP: %s\n", realIP)
|
||||||
|
|
||||||
if time.Since(lastFetchTime) > cacheDuration {
|
if time.Since(lastFetchTime) > cacheDuration {
|
||||||
if err := loadCSVPaths(); err != nil {
|
if err := loadCSVPaths(); err != nil {
|
||||||
@ -148,7 +156,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
pathSegments := strings.Split(path, "/")
|
pathSegments := strings.Split(path, "/")
|
||||||
|
|
||||||
if len(pathSegments) < 2 {
|
if len(pathSegments) < 2 {
|
||||||
http.ServeFile(w, r, filepath.Join("public", "index.html"))
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,9 +186,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
randomURL := fileArray[rng.Intn(len(fileArray))]
|
randomURL := fileArray[rng.Intn(len(fileArray))]
|
||||||
|
|
||||||
duration := time.Since(start)
|
log.Printf("Redirecting to %s\n", randomURL)
|
||||||
log.Printf("Request: %s %s from %s - Duration: %v - Redirecting to: %s\n",
|
|
||||||
r.Method, r.URL.Path, realIP, 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