summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorsiddharth <s@ricketyspace.net>2022-05-22 19:11:16 -0400
committersiddharth <s@ricketyspace.net>2022-05-22 19:11:16 -0400
commit1ae67bc9f8382c6da7047e88af6648d71c3c73f3 (patch)
treedc7fa57d94583814e03db959dea12efbd6afd844 /main.go
parent81d119d5112b4c3fd40d28b80e5af3305cb90c90 (diff)
peach: implement lat,lng routing
Diffstat (limited to 'main.go')
-rw-r--r--main.go26
1 files 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))
}