From 5f0a27d0416773ad4e618f76d001c5d1ad02540f Mon Sep 17 00:00:00 2001 From: ston1th Date: Sun, 13 Apr 2025 00:18:17 +0200 Subject: [PATCH] more cleanup --- main.go | 289 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 144 insertions(+), 145 deletions(-) diff --git a/main.go b/main.go index 9717f32..e621faa 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,9 @@ import ( "image" "image/color" "image/png" - "iter" "net/http" "os" - "slices" + "strconv" "sync" "time" ) @@ -27,28 +26,35 @@ func main() { flag.StringVar(&listen, "l", ":7878", "listen addr") flag.Parse() + if len(cityList) != len(fileList) { + fatal(errors.New("city and file list length differ")) + } + ic := &ImageCrawler{ url: "https://uvi.bfs.de/Tagesgrafiken", c: &http.Client{}, - m: make(map[string]*Image), + m: make(map[int]*Image), } http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { r.ParseForm() loc := r.PostForm.Get("loc") - if _, ok := locations[loc]; ok { - c := &http.Cookie{ - Name: "loc", - Path: "/", - Value: loc, - Secure: true, - HttpOnly: true, - SameSite: http.SameSiteStrictMode, - } - http.SetCookie(w, c) + _, ok := locFromString(loc) + if !ok { 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) + http.Redirect(w, r, "/", http.StatusMovedPermanently) + return } w.Write([]byte(locPage)) }) @@ -68,14 +74,25 @@ func main() { } else { c = cc[0] } - loc := c.Value - img, mod, err := ic.getImage(loc) + num, ok := locFromString(c.Value) + if !ok { + fmt.Fprintf(w, indexPage, + "000000", + "Error", + "Error", + time.Now().Format(time.RFC1123), + "Error", + version, + ) + return + } + img, mod, err := ic.getImage(num) mod = mod.Local() - location := selectListReverse[loc] + location := cityList[num] if err != nil { fmt.Fprintf(w, indexPage, "000000", - "Err", + "Error", mod.Format(time.RFC1123), time.Now().Format(time.RFC1123), location, @@ -87,7 +104,7 @@ func main() { if len(vals) == 0 { fmt.Fprintf(w, indexPage, "000000", - "Err", + "Error", mod.Format(time.RFC1123), time.Now().Format(time.RFC1123), location, @@ -111,11 +128,18 @@ func main() { } } +func fatal(err error) { + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} + type ImageCrawler struct { mu sync.RWMutex url string c *http.Client - m map[string]*Image + m map[int]*Image } type Image struct { @@ -128,11 +152,8 @@ type Image struct { lastGet time.Time } -func (ic *ImageCrawler) getImage(loc string) (img image.Image, mod time.Time, err error) { - file, ok := locations[loc] - if !ok { - return nil, time.Time{}, errors.New("invalid location") - } +func (ic *ImageCrawler) getImage(loc int) (img image.Image, mod time.Time, err error) { + file := fileList[loc] ic.mu.RLock() if i, ok := ic.m[loc]; ok { ic.mu.RUnlock() @@ -362,134 +383,112 @@ func isStart(col color.Color) bool { return false } -func fatal(err error) { - if err != nil { - fmt.Println(err) - os.Exit(1) - } -} - // DefaultCity is Muenchen +// Array index 24 const DefaultCity = "24" -var locations = map[string]string{ - "0": "EEr_Andernach_today.png", - "1": "EEr_Berlin_Potsdamer_Platz_today.png", - "2": "EEr_Bonn_today.png", - "3": "EEr_Boesel_B_today.png", - "4": "EEr_Chieming_today.png", - "5": "EEr_Cuxhaven_B_today.png", - "6": "EEr_Dortmund_today.png", - "7": "EEr_Duderstadt_B_today.png", - "8": "EEr_Eckernfoerde_today.png", - "9": "EEr_Fichtelberg_today.png", - "10": "EEr_Friedrichshafen_today.png", - "11": "EEr_Genthin_today.png", - "12": "EEr_Giessen-Wettenberg_today.png", - "13": "EEr_Goerlitz_today.png", - "14": "EEr_Groemitz_today.png", - "15": "EEr_Hamburg_today.png", - "16": "EEr_Hohenpeissenberg_today.png", - "17": "EEr_Kassel_today.png", - "18": "EEr_Klippeneck_today.png", - "19": "EEr_Kulmbach_today.png", - "20": "EEr_Langen_today.png", - "21": "EEr_Lindenberg_today.png", - "22": "EEr_Lueneburg_today.png", - "23": "EEr_Melpitz_today.png", - "24": "EEr_Muenchen_today.png", - "25": "EEr_Norderney_today.png", - "26": "EEr_Belm-Osnabrueck_today.png", - "27": "EEr_Salzgitter_today.png", - "28": "EEr_Sankt Augustin_today.png", - "29": "EEr_Schauinsland_today.png", - "30": "EEr_Schweinfurt_Wasserlosen_today.png", - "31": "EEr_Stuttgart_today.png", - "32": "EEr_Sylt_Tinnum_today.png", - "33": "EEr_Tholey_today.png", - "34": "EEr_Todendorf_today.png", - "35": "EEr_Waldhof_Falkenstein_today.png", - "36": "EEr_Waldmuenchen_today.png", - "37": "EEr_Wasserkuppe bei Fulda_today.png", - "38": "EEr_Weissenburg_today.png", - "39": "EEr_Wurmberg_B_today.png", - "40": "EEr_Zingst_today.png", - "41": "EEr_Zirchow_today.png", - "42": "EEr_Schneefernerhaus_today.png", +var fileList = []string{ + "EEr_Andernach_today.png", + "EEr_Berlin_Potsdamer_Platz_today.png", + "EEr_Bonn_today.png", + "EEr_Boesel_B_today.png", + "EEr_Chieming_today.png", + "EEr_Cuxhaven_B_today.png", + "EEr_Dortmund_today.png", + "EEr_Duderstadt_B_today.png", + "EEr_Eckernfoerde_today.png", + "EEr_Fichtelberg_today.png", + "EEr_Friedrichshafen_today.png", + "EEr_Genthin_today.png", + "EEr_Giessen-Wettenberg_today.png", + "EEr_Goerlitz_today.png", + "EEr_Groemitz_today.png", + "EEr_Hamburg_today.png", + "EEr_Hohenpeissenberg_today.png", + "EEr_Kassel_today.png", + "EEr_Klippeneck_today.png", + "EEr_Kulmbach_today.png", + "EEr_Langen_today.png", + "EEr_Lindenberg_today.png", + "EEr_Lueneburg_today.png", + "EEr_Melpitz_today.png", + "EEr_Muenchen_today.png", + "EEr_Norderney_today.png", + "EEr_Belm-Osnabrueck_today.png", + "EEr_Salzgitter_today.png", + "EEr_Sankt Augustin_today.png", + "EEr_Schauinsland_today.png", + "EEr_Schweinfurt_Wasserlosen_today.png", + "EEr_Stuttgart_today.png", + "EEr_Sylt_Tinnum_today.png", + "EEr_Tholey_today.png", + "EEr_Todendorf_today.png", + "EEr_Waldhof_Falkenstein_today.png", + "EEr_Waldmuenchen_today.png", + "EEr_Wasserkuppe bei Fulda_today.png", + "EEr_Weissenburg_today.png", + "EEr_Wurmberg_B_today.png", + "EEr_Zingst_today.png", + "EEr_Zirchow_today.png", + "EEr_Schneefernerhaus_today.png", } -var selectList = map[string]string{ - "Andernach": "0", - "Berlin": "1", - "Bonn": "2", - "Bösel": "3", - "Chieming": "4", - "Cuxhaven": "5", - "Dortmund": "6", - "Duderstadt": "7", - "Eckernförde": "8", - "Fichtelberg": "9", - "Friedrichshafen": "10", - "Genthin": "11", - "Gießen": "12", - "Görlitz": "13", - "Grömitz": "14", - "Hamburg": "15", - "Hohenpeißenberg": "16", - "Kassel": "17", - "Klippeneck": "18", - "Kulmbach": "19", - "Langen": "20", - "Lindenberg": "21", - "Lüneburg": "22", - "Melpitz": "23", - "München": "24", - "Norderney": "25", - "Osnabrück": "26", - "Salzgitter": "27", - "Sankt Augustin": "28", - "Schauinsland": "29", - "Schweinfurt": "30", - "Stuttgart": "31", - "Sylt (Westerland)": "32", - "Tholey": "33", - "Todendorf": "34", - "Waldhof": "35", - "Waldmünchen": "36", - "Wasserkuppe": "37", - "Weißenburg": "38", - "Wurmberg": "39", - "Zingst": "40", - "Zirchow": "41", - "Zugspitze (Schneefernerhaus)": "42", +func locFromString(s string) (i int, ok bool) { + i, err := strconv.Atoi(s) + if err != nil { + return + } + return i, validLoc(i) } -var selectListReverse = reverseMap(selectList) - -func reverseMap[K, V comparable](v map[K]V) map[V]K { - m := make(map[V]K) - for k, v := range v { - m[v] = k - } - return m +func validLoc(loc int) bool { + return loc >= 0 && loc < len(cityList) } -func sortedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] { - a := make([]K, len(m)) - i := 0 - for k := range m { - a[i] = k - i++ - } - slices.Sort(a) - return func(yield func(K, V) bool) { - for _, k := range a { - v := m[k] - if !yield(k, v) { - return - } - } - } +var cityList = []string{ + "Andernach", + "Berlin", + "Bonn", + "Bösel", + "Chieming", + "Cuxhaven", + "Dortmund", + "Duderstadt", + "Eckernförde", + "Fichtelberg", + "Friedrichshafen", + "Genthin", + "Gießen", + "Görlitz", + "Grömitz", + "Hamburg", + "Hohenpeißenberg", + "Kassel", + "Klippeneck", + "Kulmbach", + "Langen", + "Lindenberg", + "Lüneburg", + "Melpitz", + "München", + "Norderney", + "Osnabrück", + "Salzgitter", + "Sankt Augustin", + "Schauinsland", + "Schweinfurt", + "Stuttgart", + "Sylt (Westerland)", + "Tholey", + "Todendorf", + "Waldhof", + "Waldmünchen", + "Wasserkuppe", + "Weißenburg", + "Wurmberg", + "Zingst", + "Zirchow", + "Zugspitze (Schneefernerhaus)", } func generateLocPage() string { @@ -503,8 +502,8 @@ func generateLocPage() string {
Change Location

` return loc