Skip to content

Commit f06c53e

Browse files
committed
这样写也许更好
1 parent b3f4cef commit f06c53e

5 files changed

Lines changed: 43 additions & 23 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
Bing-API-go
44
*.sql
55
config.json
6+
picture.db

config.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ type config struct {
1414
KeyPath string `json:"keypath"`
1515
Port string `json:"port"`
1616
UpdateTime int `json:"updatetime"`
17-
HTML struct {
18-
SiteName string `json:"sitename"`
19-
Footer string `json:"footer"`
20-
}
2117
}
2218

2319
func readConf(path string, conf *config) error {
2420
_, err := os.Stat(path)
2521
if err != nil || os.IsExist(err) {
2622
fmt.Println("Error No Config File")
27-
return errors.New("Error No Config File")
23+
return errors.New("ErrorNoConfigFile")
2824
}
2925
tmp, err := ioutil.ReadFile(path)
3026
if err != nil {

http.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,56 @@ package main
33
import (
44
"math/rand"
55
"net/http"
6+
"strconv"
67
"time"
78
)
89

9-
func redirectToHD(w http.ResponseWriter, r *http.Request) {
10-
if r.URL.Path != "/HDRES/" {
10+
func redirectToBing(w http.ResponseWriter, r *http.Request) {
11+
var url string
12+
var urls picture
13+
14+
if r.URL.Path != "/bing" {
1115
w.WriteHeader(http.StatusNotFound)
1216
return
1317
}
14-
http.Redirect(w, r, picBuffer[0].HDURL, 302)
15-
}
1618

17-
func redirectToUHD(w http.ResponseWriter, r *http.Request) {
18-
if r.URL.Path != "/UHDRES/" {
19-
w.WriteHeader(http.StatusNotFound)
19+
parm := r.URL.Query()
20+
res, ok := parm["res"]
21+
if !ok {
22+
w.WriteHeader(http.StatusBadRequest)
23+
return
24+
}
25+
datT, ok := parm["dat"]
26+
if !ok {
27+
w.WriteHeader(http.StatusBadRequest)
28+
return
29+
}
30+
dat, err := strconv.Atoi(datT[0])
31+
if err != nil {
32+
w.WriteHeader(http.StatusBadRequest)
2033
return
2134
}
22-
http.Redirect(w, r, picBuffer[0].UHDURL, 302)
23-
}
2435

25-
func redirectToRANDOM(w http.ResponseWriter, r *http.Request) {
26-
if r.URL.Path != "/RANDOM/" {
27-
w.WriteHeader(http.StatusNotFound)
36+
if dat <= len(picBuffer)-1 && dat >= 0 {
37+
urls = picBuffer[dat]
38+
} else if dat == -1 {
39+
rand.Seed(time.Now().Unix())
40+
i := rand.Intn(len(picBuffer) - 1)
41+
urls = picBuffer[i]
42+
} else {
43+
w.WriteHeader(http.StatusBadRequest)
44+
return
45+
}
46+
47+
switch res[0] {
48+
case "hdres":
49+
url = urls.HDURL
50+
case "uhdres":
51+
url = urls.UHDURL
52+
default:
53+
w.WriteHeader(http.StatusBadRequest)
2854
return
2955
}
30-
rand.Seed(time.Now().Unix())
31-
index := rand.Intn(len(picBuffer) - 1)
32-
http.Redirect(w, r, picBuffer[index].HDURL, 302)
56+
57+
http.Redirect(w, r, url, http.StatusFound)
3358
}

main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ func init() {
8181
}
8282

8383
func main() {
84-
http.HandleFunc("/HDRES/", redirectToHD)
85-
http.HandleFunc("/UHDRES/", redirectToUHD)
86-
http.HandleFunc("/RANDOM/", redirectToRANDOM)
84+
http.HandleFunc("/bing", redirectToBing)
8785
time.Sleep(time.Second)
8886
if conf.EnableTLS {
8987
http.ListenAndServeTLS("0.0.0.0:"+conf.Port,

picture.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)