/
TripPlanner.go
251 lines (223 loc) · 7.1 KB
/
TripPlanner.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"encoding/json"
"fmt"
//"github.com/julienschmidt/httprouter"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
//"mongo-tools/vendor/src/gopkg.in/mgo.v2"
//"mongo-tools/vendor/src/gopkg.in/mgo.v2/bson"
"httprouter"
//"mgo.v2"
//"mgo.v2/bson"
"net/http"
"strings"
"time"
)
type LocationReq struct {
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
}
type LocationRes struct {
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Coordinate struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"coordinate"`
}
type GeoLoc struct {
Results []struct {
AddressComponents []struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string `json:"types"`
} `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
LocationType string `json:"location_type"`
Viewport struct {
Northeast struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"northeast"`
Southwest struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"southwest"`
} `json:"viewport"`
} `json:"geometry"`
PlaceID string `json:"place_id"`
Types []string `json:"types"`
} `json:"results"`
Status string `json:"status"`
}
type GoogleLocationRes struct {
Results []struct {
AddressComponents []struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string `json:"types"`
} `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
LocationType string `json:"location_type"`
Viewport struct {
Northeast struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"northeast"`
Southwest struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"southwest"`
} `json:"viewport"`
} `json:"geometry"`
PlaceID string `json:"place_id"`
Types []string `json:"types"`
} `json:"results"`
Status string `json:"status"`
}
var collection *mgo.Collection
var locRes LocationRes
const (
timeout = time.Duration(time.Second * 100)
)
func connectMongo() {
uri := "mongodb://arpit9691:Arpit#9691@ds043694.mongolab.com:43694/location_db"
//uri := "mongodb://nipun:nipun@ds045464.mongolab.com:45464/db2"
ses, err := mgo.Dial(uri)
if err != nil {
fmt.Printf("Can't connect to mongo, go error %v\n", err)
} else {
ses.SetSafe(&mgo.Safe{})
collection = ses.DB("location_db").C("test")
//collection = ses.DB("db2").C("qwerty")
}
}
func getGoogleLoc(address string) (geoLocation GeoLoc) {
client := http.Client{Timeout: timeout}
url := fmt.Sprintf("http://maps.google.com/maps/api/geocode/json?address=%s", address)
res, err := client.Get(url)
if err != nil {
fmt.Errorf("Can't read Google API: %v", err)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
err = decoder.Decode(&geoLocation)
if err != nil {
fmt.Errorf("Error in decoding the Google: %v", err)
}
return geoLocation
}
func getLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
id := bson.ObjectIdHex(p.ByName("locationID"))
err := collection.FindId(id).One(&locRes)
if err != nil {
fmt.Printf("error finding a doc %v\n")
}
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.WriteHeader(200)
json.NewEncoder(rw).Encode(locRes)
}
func addLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var tempLocReq LocationReq
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&tempLocReq)
if err != nil {
fmt.Errorf("Error in decoding the Input: %v", err)
}
address := tempLocReq.Address + " " + tempLocReq.City + " " + tempLocReq.State + " " + tempLocReq.Zip
address = strings.Replace(address, " ", "%20", -1)
locationDetails := getGoogleLoc(address)
locRes.ID = bson.NewObjectId()
locRes.Address = tempLocReq.Address
locRes.City = tempLocReq.City
locRes.Name = tempLocReq.Name
locRes.State = tempLocReq.State
locRes.Zip = tempLocReq.Zip
locRes.Coordinate.Lat = locationDetails.Results[0].Geometry.Location.Lat
locRes.Coordinate.Lng = locationDetails.Results[0].Geometry.Location.Lng
err = collection.Insert(locRes)
if err != nil {
fmt.Printf("Can't insert document: %v\n", err)
}
err = collection.FindId(locRes.ID).One(&locRes)
if err != nil {
fmt.Printf("error finding a doc %v\n")
}
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.WriteHeader(201)
json.NewEncoder(rw).Encode(locRes)
}
func updateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var tempLocRes LocationRes
var locRes LocationRes
id := bson.ObjectIdHex(p.ByName("locationID"))
err := collection.FindId(id).One(&locRes)
if err != nil {
fmt.Printf("error finding a doc %v\n")
}
tempLocRes.Name = locRes.Name
tempLocRes.Address = locRes.Address
tempLocRes.City = locRes.City
tempLocRes.State = locRes.State
tempLocRes.Zip = locRes.Zip
decoder := json.NewDecoder(req.Body)
err = decoder.Decode(&tempLocRes)
if err != nil {
fmt.Errorf("Error in decoding the Input: %v", err)
}
address := tempLocRes.Address + " " + tempLocRes.City + " " + tempLocRes.State + " " + tempLocRes.Zip
address = strings.Replace(address, " ", "%20", -1)
locationDetails := getGoogleLoc(address)
tempLocRes.Coordinate.Lat = locationDetails.Results[0].Geometry.Location.Lat
tempLocRes.Coordinate.Lng = locationDetails.Results[0].Geometry.Location.Lng
err = collection.UpdateId(id, tempLocRes)
if err != nil {
fmt.Printf("got an error updating a doc %v\n")
}
err = collection.FindId(id).One(&locRes)
if err != nil {
fmt.Printf("got an error finding a doc %v\n")
}
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.WriteHeader(201)
json.NewEncoder(rw).Encode(locRes)
}
func deleteLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
id := bson.ObjectIdHex(p.ByName("locationID"))
err := collection.RemoveId(id)
if err != nil {
fmt.Printf("got an error deleting a doc %v\n")
}
rw.WriteHeader(200)
}
func main() {
mux := httprouter.New()
mux.GET("/locations/:locationID", getLocation)
mux.POST("/locations", addLocation)
mux.PUT("/locations/:locationID", updateLocation)
mux.DELETE("/locations/:locationID", deleteLocation)
server := http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
connectMongo()
server.ListenAndServe()
}