summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-05-28 10:37:14 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-05-28 10:37:14 -0400
commite4d4d271d22b0efe8c59f4f0117e884a73fb81b3 (patch)
tree1468fae1abba8eaee4053a90eeea32f86ae07c24 /main.go
parent7fdc2d19e14bc972426f11ee9ff8567efbff07de (diff)
peach: add handling for /search
Initial version.
Diffstat (limited to 'main.go')
-rw-r--r--main.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/main.go b/main.go
index 11cde3b..503938a 100644
--- a/main.go
+++ b/main.go
@@ -16,6 +16,7 @@ import (
"time"
"ricketyspace.net/peach/nws"
+ "ricketyspace.net/peach/photon"
)
// peach port. defaults to 8151
@@ -61,6 +62,13 @@ type WeatherTimeline struct {
Periods []WeatherPeriod
}
+type Search struct {
+ Title string
+ Location string
+ Message string
+ MatchingCoords []photon.Coordinates
+}
+
func init() {
flag.Parse()
if *peachPort < 80 {
@@ -73,6 +81,9 @@ func main() {
// static files handler.
http.Handle("/static/", http.FileServer(http.FS(peachFS)))
+ // search handler.
+ http.HandleFunc("/search", showSearch)
+
// default handler.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
@@ -95,6 +106,8 @@ func main() {
}
showWeather(w, float32(lat), float32(lng))
})
+
+ // start server
log.Fatal(http.ListenAndServe(peachAddr, nil))
}
@@ -127,9 +140,28 @@ func showWeather(w http.ResponseWriter, lat, lng float32) {
// render.
err = peachTemplates.ExecuteTemplate(w, "weather.tmpl", weather)
if err != nil {
+ log.Printf("weather: template: %v", err)
+ return
+ }
+}
+
+func showSearch(w http.ResponseWriter, r *http.Request) {
+ // Search is disabled if photon is not enabled.
+ if !photon.Enabled() {
+ http.NotFound(w, r)
+ return
+ }
+
+ search, err := NewSearch(r)
+ if err != nil {
http.Error(w, err.Error(), 500)
return
}
+ err = peachTemplates.ExecuteTemplate(w, "search.tmpl", search)
+ if err != nil {
+ log.Printf("search: template: %v", err)
+ return
+ }
}
func NewWeather(point *nws.NWSPoint, f, fh *nws.NWSForecast) (*Weather, error) {
@@ -178,3 +210,36 @@ func NewWeather(point *nws.NWSPoint, f, fh *nws.NWSForecast) (*Weather, error) {
return w, nil
}
+
+func NewSearch(r *http.Request) (*Search, error) {
+ s := new(Search)
+ s.Title = "search"
+
+ if r.Method == "GET" {
+ return s, nil
+ }
+
+ // get location.
+ err := r.ParseForm()
+ if err != nil {
+ return s, fmt.Errorf("form: %v", err)
+ }
+ location := strings.TrimSpace(r.PostForm.Get("location"))
+ s.Location = location
+ if len(location) < 2 {
+ s.Message = "location invalid"
+ }
+
+ // try to fetch matching coordinates.
+ s.MatchingCoords, err = photon.Geocode(location)
+ if err != nil {
+ log.Printf("search: geocode: %v", err)
+ s.Message = "unable to lookup location"
+ return s, nil
+ }
+ if len(s.MatchingCoords) < 1 {
+ s.Message = "location not found"
+ return s, nil
+ }
+ return s, nil
+}