0
0
Fork 0

more cleanup

This commit is contained in:
ston1th 2025-04-13 00:30:41 +02:00
commit 21c0d33b5c

75
main.go
View file

@ -44,15 +44,7 @@ func main() {
http.Redirect(w, r, "/", http.StatusMovedPermanently)
return
}
c := &http.Cookie{
Name: "loc",
Path: "/",
Value: loc,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
http.SetCookie(w, c)
setCookie(w, loc)
http.Redirect(w, r, "/", http.StatusMovedPermanently)
return
}
@ -61,55 +53,26 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
cc := r.CookiesNamed("loc")
var c *http.Cookie
if len(cc) == 0 {
c = &http.Cookie{
Name: "loc",
Path: "/",
Value: DefaultCity,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
}
http.SetCookie(w, c)
} else {
if len(cc) > 0 {
c = cc[0]
} else {
c = setCookie(w, DefaultCity)
}
num, ok := locFromString(c.Value)
if !ok {
fmt.Fprintf(w, indexPage,
"000000",
"Error",
"Error",
time.Now().Format(time.RFC1123),
"Error",
version,
)
writeError(w, "Error", "Error")
return
}
img, mod, err := ic.getImage(num)
mod = mod.Local()
location := cityList[num]
if err != nil {
fmt.Fprintf(w, indexPage,
"000000",
"Error",
mod.Format(time.RFC1123),
time.Now().Format(time.RFC1123),
location,
version,
)
writeError(w, mod.Format(time.RFC1123), location)
return
}
vals := getUV(img)
if len(vals) == 0 {
fmt.Fprintf(w, indexPage,
"000000",
"Error",
mod.Format(time.RFC1123),
time.Now().Format(time.RFC1123),
location,
version,
)
writeError(w, mod.Format(time.RFC1123), location)
return
}
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) {
if err != nil {
fmt.Println(err)