more cleanup
This commit is contained in:
parent
5f0a27d041
commit
21c0d33b5c
1 changed files with 31 additions and 44 deletions
75
main.go
75
main.go
|
|
@ -44,15 +44,7 @@ func main() {
|
||||||
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c := &http.Cookie{
|
setCookie(w, loc)
|
||||||
Name: "loc",
|
|
||||||
Path: "/",
|
|
||||||
Value: loc,
|
|
||||||
Secure: true,
|
|
||||||
HttpOnly: true,
|
|
||||||
SameSite: http.SameSiteStrictMode,
|
|
||||||
}
|
|
||||||
http.SetCookie(w, c)
|
|
||||||
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -61,55 +53,26 @@ func main() {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
cc := r.CookiesNamed("loc")
|
cc := r.CookiesNamed("loc")
|
||||||
var c *http.Cookie
|
var c *http.Cookie
|
||||||
if len(cc) == 0 {
|
if len(cc) > 0 {
|
||||||
c = &http.Cookie{
|
|
||||||
Name: "loc",
|
|
||||||
Path: "/",
|
|
||||||
Value: DefaultCity,
|
|
||||||
Secure: true,
|
|
||||||
HttpOnly: true,
|
|
||||||
SameSite: http.SameSiteStrictMode,
|
|
||||||
}
|
|
||||||
http.SetCookie(w, c)
|
|
||||||
} else {
|
|
||||||
c = cc[0]
|
c = cc[0]
|
||||||
|
} else {
|
||||||
|
c = setCookie(w, DefaultCity)
|
||||||
}
|
}
|
||||||
num, ok := locFromString(c.Value)
|
num, ok := locFromString(c.Value)
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Fprintf(w, indexPage,
|
writeError(w, "Error", "Error")
|
||||||
"000000",
|
|
||||||
"Error",
|
|
||||||
"Error",
|
|
||||||
time.Now().Format(time.RFC1123),
|
|
||||||
"Error",
|
|
||||||
version,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
img, mod, err := ic.getImage(num)
|
img, mod, err := ic.getImage(num)
|
||||||
mod = mod.Local()
|
mod = mod.Local()
|
||||||
location := cityList[num]
|
location := cityList[num]
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, indexPage,
|
writeError(w, mod.Format(time.RFC1123), location)
|
||||||
"000000",
|
|
||||||
"Error",
|
|
||||||
mod.Format(time.RFC1123),
|
|
||||||
time.Now().Format(time.RFC1123),
|
|
||||||
location,
|
|
||||||
version,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
vals := getUV(img)
|
vals := getUV(img)
|
||||||
if len(vals) == 0 {
|
if len(vals) == 0 {
|
||||||
fmt.Fprintf(w, indexPage,
|
writeError(w, mod.Format(time.RFC1123), location)
|
||||||
"000000",
|
|
||||||
"Error",
|
|
||||||
mod.Format(time.RFC1123),
|
|
||||||
time.Now().Format(time.RFC1123),
|
|
||||||
location,
|
|
||||||
version,
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
last := vals[len(vals)-1]
|
last := vals[len(vals)-1]
|
||||||
|
|
@ -128,6 +91,30 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setCookie(w http.ResponseWriter, loc string) *http.Cookie {
|
||||||
|
c := &http.Cookie{
|
||||||
|
Name: "loc",
|
||||||
|
Path: "/",
|
||||||
|
Value: loc,
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteStrictMode,
|
||||||
|
}
|
||||||
|
http.SetCookie(w, c)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, mod, location string) {
|
||||||
|
fmt.Fprintf(w, indexPage,
|
||||||
|
"000000",
|
||||||
|
"Error",
|
||||||
|
mod,
|
||||||
|
time.Now().Format(time.RFC1123),
|
||||||
|
location,
|
||||||
|
version,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func fatal(err error) {
|
func fatal(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue