From 276b82f0d49d52ca02a7a51f7acb27b8cbc8381b Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Sun, 12 Jun 2022 19:57:22 -0400 Subject: search: update `Search` Add `Enabled`. Determines whether search is enabled or not. --- main.go | 9 +++------ search/search.go | 17 +++++++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index e328636..9e1a6f7 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,6 @@ import ( "regexp" "strconv" - "ricketyspace.net/peach/photon" "ricketyspace.net/peach/search" "ricketyspace.net/peach/version" "ricketyspace.net/peach/weather" @@ -102,15 +101,13 @@ func showWeather(w http.ResponseWriter, lat, lng float32) { func showSearch(w http.ResponseWriter, r *http.Request) { logRequest(r) - // Search is disabled if photon is not enabled. - if !photon.Enabled() { + search, err, status := search.NewSearch(r) + if err != nil && status == 404 { http.NotFound(w, r) return } - - search, err := search.NewSearch(r) if err != nil { - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), status) return } err = peachTemplates.ExecuteTemplate(w, "search.tmpl", search) diff --git a/search/search.go b/search/search.go index 1b297c3..7e0d6e0 100644 --- a/search/search.go +++ b/search/search.go @@ -19,21 +19,26 @@ type Search struct { Location string Message string MatchingCoords []photon.Coordinates + Enabled bool } -func NewSearch(r *http.Request) (*Search, error) { +func NewSearch(r *http.Request) (*Search, error, int) { s := new(Search) s.Title = "search" s.Version = version.Version + s.Enabled = photon.Enabled() + if !s.Enabled { + return s, fmt.Errorf("search disabled"), 404 + } if r.Method == "GET" { - return s, nil + return s, nil, 200 } // Get location. err := r.ParseForm() if err != nil { - return s, fmt.Errorf("form: %v", err) + return s, fmt.Errorf("form: %v", err), 500 } location := strings.TrimSpace(r.PostForm.Get("location")) s.Location = location @@ -46,11 +51,11 @@ func NewSearch(r *http.Request) (*Search, error) { if err != nil { log.Printf("search: geocode: %v", err) s.Message = "unable to lookup location" - return s, nil + return s, nil, 200 } if len(s.MatchingCoords) < 1 { s.Message = "location not found" - return s, nil + return s, nil, 200 } - return s, nil + return s, nil, 200 } -- cgit v1.2.3