Skip to content

Commit

Permalink
这样写也许更好
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieYu4994 committed Mar 24, 2021
1 parent b3f4cef commit f06c53e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
Bing-API-go
*.sql
config.json
picture.db
6 changes: 1 addition & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ type config struct {
KeyPath string `json:"keypath"`
Port string `json:"port"`
UpdateTime int `json:"updatetime"`
HTML struct {
SiteName string `json:"sitename"`
Footer string `json:"footer"`
}
}

func readConf(path string, conf *config) error {
_, err := os.Stat(path)
if err != nil || os.IsExist(err) {
fmt.Println("Error No Config File")
return errors.New("Error No Config File")
return errors.New("ErrorNoConfigFile")
}
tmp, err := ioutil.ReadFile(path)
if err != nil {
Expand Down
55 changes: 40 additions & 15 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,56 @@ package main
import (
"math/rand"
"net/http"
"strconv"
"time"
)

func redirectToHD(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/HDRES/" {
func redirectToBing(w http.ResponseWriter, r *http.Request) {
var url string
var urls picture

if r.URL.Path != "/bing" {
w.WriteHeader(http.StatusNotFound)
return
}
http.Redirect(w, r, picBuffer[0].HDURL, 302)
}

func redirectToUHD(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/UHDRES/" {
w.WriteHeader(http.StatusNotFound)
parm := r.URL.Query()
res, ok := parm["res"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
datT, ok := parm["dat"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
dat, err := strconv.Atoi(datT[0])
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
http.Redirect(w, r, picBuffer[0].UHDURL, 302)
}

func redirectToRANDOM(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/RANDOM/" {
w.WriteHeader(http.StatusNotFound)
if dat <= len(picBuffer)-1 && dat >= 0 {
urls = picBuffer[dat]
} else if dat == -1 {
rand.Seed(time.Now().Unix())
i := rand.Intn(len(picBuffer) - 1)
urls = picBuffer[i]
} else {
w.WriteHeader(http.StatusBadRequest)
return
}

switch res[0] {
case "hdres":
url = urls.HDURL
case "uhdres":
url = urls.UHDURL
default:
w.WriteHeader(http.StatusBadRequest)
return
}
rand.Seed(time.Now().Unix())
index := rand.Intn(len(picBuffer) - 1)
http.Redirect(w, r, picBuffer[index].HDURL, 302)

http.Redirect(w, r, url, http.StatusFound)
}
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ func init() {
}

func main() {
http.HandleFunc("/HDRES/", redirectToHD)
http.HandleFunc("/UHDRES/", redirectToUHD)
http.HandleFunc("/RANDOM/", redirectToRANDOM)
http.HandleFunc("/bing", redirectToBing)
time.Sleep(time.Second)
if conf.EnableTLS {
http.ListenAndServeTLS("0.0.0.0:"+conf.Port,
Expand Down
Binary file modified picture.db
Binary file not shown.

0 comments on commit f06c53e

Please sign in to comment.