summaryrefslogtreecommitdiffstats
path: root/search
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2022-06-12 19:51:32 -0400
committersiddharth ravikumar <s@ricketyspace.net>2022-06-12 19:51:32 -0400
commit2f14203efe0514cd4b64085efe9cc5980a276983 (patch)
tree1e5f9b1132a71f7308e71340939ea1b6f181a372 /search
parent2726d7d51bb89c0ab41783ab359b5ed7ad29a758 (diff)
search: move search stuff to its own package
Diffstat (limited to 'search')
-rw-r--r--search/search.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/search/search.go b/search/search.go
new file mode 100644
index 0000000..1b297c3
--- /dev/null
+++ b/search/search.go
@@ -0,0 +1,56 @@
+// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net>
+// SPDX-License-Identifier: ISC
+
+package search
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+ "strings"
+
+ "ricketyspace.net/peach/photon"
+ "ricketyspace.net/peach/version"
+)
+
+type Search struct {
+ Title string
+ Version string
+ Location string
+ Message string
+ MatchingCoords []photon.Coordinates
+}
+
+func NewSearch(r *http.Request) (*Search, error) {
+ s := new(Search)
+ s.Title = "search"
+ s.Version = version.Version
+
+ 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
+}