/
search.go
143 lines (125 loc) · 3.05 KB
/
search.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package site
import (
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"strconv"
"text/template"
"time"
"github.com/tstromberg/campwiz/pkg/campwiz"
"github.com/tstromberg/campwiz/pkg/mangle"
"github.com/tstromberg/campwiz/pkg/search"
"k8s.io/klog/v2"
)
type templateContext struct {
Query campwiz.Query
Results []campwiz.Result
Sources map[string]campwiz.Source
Errors []error
Today time.Time
SelectDate time.Time
Version string
}
func futureFriday() time.Time {
try := time.Now().Add(time.Duration(7 * 6 * 24 * time.Hour))
offset := 5 - int(try.Weekday())
return try.Add(time.Duration(offset) * 24 * time.Hour)
}
// Search returns search results
func (h *Handlers) Search() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
klog.Infof("Incoming request: %+v", r)
q := campwiz.Query{
Lon: h.c.Longitude,
Lat: h.c.Latitude,
StayLength: getInt(r.URL, "nights", 2),
MaxDistance: getInt(r.URL, "distance", 100),
MinRating: getFloat(r.URL, "min_rating", 0.0),
Keywords: []string{getStr(r.URL, "keywords", "")},
}
selectDate := futureFriday()
for _, ds := range r.URL.Query()["dates"] {
t, err := time.Parse("2006-01-02", ds)
if err != nil {
h.error(w, err)
return
}
q.Dates = append(q.Dates, t)
selectDate = t
}
var rs []campwiz.Result
var errs []error
if len(q.Dates) > 0 {
rs, errs = search.Run(h.c.Providers, q, h.c.Cache, h.c.Properties)
if len(errs) > 0 {
klog.Errorf("search errors: %v", errs)
}
}
p := filepath.Join(h.c.BaseDirectory, "search.tmpl")
outTmpl, err := ioutil.ReadFile(p)
if err != nil {
h.error(w, err)
return
}
fmap := template.FuncMap{
"Ellipsis": ellipse,
"toDate": toDate,
}
tmpl := template.Must(template.New("http").Funcs(fmap).Parse(string(outTmpl)))
ctx := templateContext{
Query: q,
Sources: h.c.Sources,
Results: rs,
Errors: errs,
SelectDate: selectDate,
Today: time.Now(),
Version: VERSION,
}
err = tmpl.ExecuteTemplate(w, "http", ctx)
if err != nil {
h.error(w, err)
return
}
}
}
func ellipse(s string) string {
return mangle.Ellipsis(s, 100)
}
func toDate(t time.Time) string {
return t.Format("2006-01-02")
}
// helper to get integers from a URL
func getInt(url *url.URL, key string, fallback int) int {
vals := url.Query()[key]
if len(vals) == 1 {
i, err := strconv.ParseInt(vals[0], 10, 32)
if err != nil {
klog.Warningf("bad %s int value: %v", key, vals)
return fallback
}
return int(i)
}
return fallback
}
// helper to get floats from a URL
func getFloat(url *url.URL, key string, fallback float64) float64 {
vals := url.Query()[key]
if len(vals) == 1 {
i, err := strconv.ParseFloat(vals[0], 64)
if err != nil {
klog.Warningf("bad %s int value: %v", key, vals)
return fallback
}
return i
}
return fallback
}
// helper to get string from a URL
func getStr(url *url.URL, key string, fallback string) string {
vals := url.Query()[key]
if len(vals) > 0 {
return vals[0]
}
return fallback
}