summaryrefslogtreecommitdiffstats
path: root/search/search.go
blob: 7e0d6e04a173ab8c491caf7332ccab6dc9472eba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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
	Enabled        bool
}

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, 200
	}

	// Get location.
	err := r.ParseForm()
	if err != nil {
		return s, fmt.Errorf("form: %v", err), 500
	}
	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, 200
	}
	if len(s.MatchingCoords) < 1 {
		s.Message = "location not found"
		return s, nil, 200
	}
	return s, nil, 200
}