diff options
author | siddharth <s@ricketyspace.net> | 2022-05-22 19:11:16 -0400 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2022-05-22 19:11:16 -0400 |
commit | 1ae67bc9f8382c6da7047e88af6648d71c3c73f3 (patch) | |
tree | dc7fa57d94583814e03db959dea12efbd6afd844 | |
parent | 81d119d5112b4c3fd40d28b80e5af3305cb90c90 (diff) |
peach: implement lat,lng routing
-rw-r--r-- | main.go | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -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)) } |