From 5af0c8ee08cfb86b7f662df9fc052ae396509548 Mon Sep 17 00:00:00 2001 From: CharlieYu4994 Date: Fri, 29 Jan 2021 13:29:40 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9A=8F=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http.go | 8 ++++++++ main.go | 4 +--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/http.go b/http.go index 67198a4..d49845b 100644 --- a/http.go +++ b/http.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "math/rand" "net/http" + "time" ) func redirectToHD(w http.ResponseWriter, r *http.Request) { @@ -13,6 +15,12 @@ func redirectToUHD(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, picBuffer[0].UHDURL, 302) } +func redirectToRANDOM(w http.ResponseWriter, r *http.Request) { + rand.Seed(time.Now().Unix()) + index := rand.Intn(len(picBuffer) - 1) + http.Redirect(w, r, picBuffer[index].HDURL, 302) +} + func homePage(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `Here is LassiCat's BingAPI. diff --git a/main.go b/main.go index b9f2695..d80b5e2 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "database/sql" - "fmt" "net/http" "time" ) @@ -85,9 +84,8 @@ func main() { http.HandleFunc("/", homePage) http.HandleFunc("/HDRES/", redirectToHD) http.HandleFunc("/UHDRES/", redirectToUHD) + http.HandleFunc("/RANDOM/", redirectToRANDOM) time.Sleep(time.Second) - fmt.Println("Done") - fmt.Println(picBuffer) if conf.EnableTLS { http.ListenAndServeTLS("0.0.0.0:"+conf.Port, conf.CertPath, conf.KeyPath, nil)