From 1ae67bc9f8382c6da7047e88af6648d71c3c73f3 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sun, 22 May 2022 19:11:16 -0400 Subject: peach: implement lat,lng routing --- main.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 00ff8d8..151aebe 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "html/template" "log" "net/http" + "regexp" + "strconv" "time" "ricketyspace.net/peach/nws" @@ -28,6 +30,9 @@ var peachFS embed.FS // HTML templates. var peachTemplates = template.Must(template.ParseFS(peachFS, "templates/*.tmpl")) +// lat,long regex. +var latLngRegex = regexp.MustCompile(`/(-?[0-9]+\.?[0-9]+?),(-?[0-9]+\.?[0-9]+)`) + type Weather struct { Location string Now WeatherNow @@ -63,13 +68,30 @@ func init() { } func main() { + // static files handler. http.Handle("/static/", http.FileServer(http.FS(peachFS))) + + // default handler. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if len(r.URL.Path[1:]) != 0 { + if r.URL.Path == "/" { + http.Redirect(w, r, "/41.115,-83.177", 302) + return + } + + m := latLngRegex.FindStringSubmatch(r.URL.Path) + if len(m) != 3 { http.NotFound(w, r) return } - showWeather(w, 41.115, -83.177) + lat, err := strconv.ParseFloat(m[1], 32) + if err != nil { + http.Error(w, err.Error(), 400) + } + lng, err := strconv.ParseFloat(m[2], 32) + if err != nil { + http.Error(w, err.Error(), 400) + } + showWeather(w, float32(lat), float32(lng)) }) log.Fatal(http.ListenAndServe(peachAddr, nil)) } -- cgit v1.2.3