From c9496e8d09b44215fae350bb1ce0a1918d284a38 Mon Sep 17 00:00:00 2001 From: CharlieYu4994 Date: Mon, 29 Mar 2021 21:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 1 + config.json.template | 3 ++- handler.go | 6 +++--- main.go | 4 ++-- pic.go | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 0fcff11..f453eea 100644 --- a/config.go +++ b/config.go @@ -14,6 +14,7 @@ type config struct { KeyPath string `json:"keypath"` Port string `json:"port"` DataBase string `json:"database"` + UrlBase string `json:"urlbase"` } func readConf(path string, conf *config) error { diff --git a/config.json.template b/config.json.template index ae7fc89..8dd7ed2 100644 --- a/config.json.template +++ b/config.json.template @@ -2,9 +2,10 @@ "enabletls": true, "certpath": "/path/to/your/crt/file", "keypath": "/path/to/your/key/file", - "updatetime": 1, + "urlbase": "bing", "port": "9090", "database": "./picture.db", + "updatetime": 1, "picnum": 7 } //you can use relative path as well as absolute path diff --git a/handler.go b/handler.go index 1af8d1c..069a683 100644 --- a/handler.go +++ b/handler.go @@ -26,14 +26,14 @@ func getDuration(t int) time.Duration { } func updatePic(i inserter, v validator) error { - pics, err := getPictureInfo("zh-CN") + pics, err := getPicture("zh-CN") if err != nil { return err } for j := len(pics) - 1; j >= 0; j-- { - status, _ := v(pics[j].Date) - if status { + ok, _ := v(pics[j].Date) + if ok { i(pics[j].Date, pics[j].Burl) } } diff --git a/main.go b/main.go index 89d8213..9ec1dff 100644 --- a/main.go +++ b/main.go @@ -32,13 +32,13 @@ func init() { tmp := make([]picture, conf.PicNum) bingHandler.pic = tmp - bingHandler.urlbase = "/bing" + bingHandler.urlbase = "/" + conf.UrlBase go bingHandler.timeToUpdatePic() } func main() { - http.HandleFunc("/bing", bingHandler.redirectToPic) + http.HandleFunc("/"+conf.UrlBase, bingHandler.redirectToPic) time.Sleep(time.Second) if conf.EnableTLS { http.ListenAndServeTLS("0.0.0.0:"+conf.Port, diff --git a/pic.go b/pic.go index d392b30..1595927 100644 --- a/pic.go +++ b/pic.go @@ -12,7 +12,7 @@ type picture struct { Burl string `json:"urlbase"` } -func getPictureInfo(mkt string) (pics []picture, err error) { +func getPicture(mkt string) (pics []picture, err error) { gURL := fmt.Sprintf("%s/HPImageArchive.aspx?format=js&idx=-1&n=9&mkt=%s", domain, mkt) response, err := http.Get(gURL)