/
geo_jots.go
160 lines (131 loc) · 3.34 KB
/
geo_jots.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/mattbaird/elastigo/core"
"net/http"
"strconv"
)
type Location struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
type GeoJot struct {
Id string `json:"id"`
Name string `json:"name"`
Location Location `json:"location"`
}
type GeoJotJSON struct {
GeoJot GeoJot `json:"geo_jot"`
}
type GeoJotsJSON struct {
GeoJots []GeoJot `json:"geo_jots"`
}
var es_index = "geo_jots"
var es_index_type = "geo_jot"
func GeoJotsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := r.ParseForm()
if err != nil {
serveError(w, err)
return
}
lat, err := strconv.ParseFloat(r.Form.Get("lat"), 64)
if err != nil {
serveError(w, err)
return
}
lon, err := strconv.ParseFloat(r.Form.Get("lon"), 64)
if err != nil {
serveError(w, err)
return
}
// Find all GeoJots that are close by
qry := map[string]interface{}{
"from": 0,
"size": 50,
"query": map[string]interface{}{
"filtered": map[string]interface{}{
"query": map[string]interface{}{
"match_all": map[string]interface{}{},
},
"filter": map[string]interface{}{
"geo_distance": map[string]interface{}{
"distance": "1km",
"geo_jot.location": map[string]interface{}{
"lat": lat,
"lon": lon,
},
},
},
},
},
}
resp, err := core.SearchRequest(true, es_index, es_index_type, qry, "")
//resp, err := core.SearchUri(es_index, es_index_type, "name:*", "")
if err != nil {
serveError(w, err)
}
var jots []GeoJot
if resp.Hits.Total > 0 {
for _, value := range resp.Hits.Hits {
var jot GeoJot
if err := json.Unmarshal(value.Source, &jot); err != nil {
serveError(w, err)
return
}
jot.Id = value.Id
jots = append(jots, jot)
}
}
j, err := json.Marshal(GeoJotsJSON{GeoJots: jots})
if err != nil {
serveError(w, err)
}
w.Write(j)
}
func ShowGeoJotHandler(w http.ResponseWriter, r *http.Request) {
}
func CreateGeoJotHandler(w http.ResponseWriter, r *http.Request) {
// Parse the incoming geojot from the request body
var geojotJSON GeoJotJSON
err := json.NewDecoder(r.Body).Decode(&geojotJSON)
if err != nil {
serveError(w, err)
}
geojot := geojotJSON.GeoJot
// add single go struct entity
response, _ := core.Index(true, es_index, es_index_type, "", geojot)
// Grab the Id for the client
geojot.Id = response.Id
// Serialize the modified geojot to JSON
j, err := json.Marshal(GeoJotJSON{GeoJot: geojot})
if err != nil {
serveError(w, err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}
func UpdateGeoJotHandler(w http.ResponseWriter, r *http.Request) {
// Decode the incoming geojot json
var geojotJSON GeoJotJSON
err := json.NewDecoder(r.Body).Decode(&geojotJSON)
if err != nil {
serveError(w, err)
}
// TODO find the GeoJot and update it in ES
// Grab the geojot's id from the incoming url
// vars := mux.Vars(r)
// vars["id"]
// Respond with a 204 indicating success, but no content
w.WriteHeader(http.StatusNoContent)
}
func DeleteGeoJotHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := core.Delete(true, es_index, es_index_type, vars["id"], 1, "")
if err != nil {
serveError(w, err)
}
// Respond with a 204 indicating success, but no content
w.WriteHeader(http.StatusNoContent)
}