-
Notifications
You must be signed in to change notification settings - Fork 0
/
Go-Server.go
224 lines (194 loc) · 5.94 KB
/
Go-Server.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
package main
import (
"net/url"
"strconv"
"io/ioutil"
"net/http"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
)
func main() {
uc := NewLocationController(getSession()) // Getting the instance of LocationController
r := httprouter.New() // Create new router
r.POST("/Locations", uc.CreateLocation) // Adding a new address
r.GET("/Locations/:location_id", uc.GetLocation) // Get the location of the added address
r.PUT("/Locations/:location_id", uc.UpdateLocation) // Update an already existing address
r.DELETE("/Locations/:location_id", uc.DeleteLocation ) // Delete an already existing address
http.ListenAndServe("localhost:8080", r) // Running the server
}
// Establish a new session
func getSession() *mgo.Session {
s, err := mgo.Dial("mongodb://nom:root@ds045464.mongolab.com:45464/mongolocations")
if err != nil {
panic(err)
}
s.SetMode(mgo.Monotonic, true)
return s
}
// Creating a new address
func (uc LocationController) CreateLocation(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var u InputAddress
var oA OutputAddress
json.NewDecoder(r.Body).Decode(&u)
googResCoor := getGoogleCoordinates (u.Address + "+" + u.City + "+" + u.State + "+" + u.Zip);
fmt.Println("resp is: ", googResCoor.Coordinate.Lat, googResCoor.Coordinate.Lang);
oA.Id = bson.NewObjectId()
oA.Name = u.Name
oA.Address = u.Address
oA.City= u.City
oA.State= u.State
oA.Zip = u.Zip
oA.Coordinate.Lat = googResCoor.Coordinate.Lat
oA.Coordinate.Lang = googResCoor.Coordinate.Lang
uc.session.DB("mongolocations").C("locations").Insert(oA)
uj, _ := json.Marshal(oA)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
fmt.Fprintf(w, "%s", uj)
}
// Getting the address which is already existing
func (uc LocationController) GetLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("location_id")
if !bson.IsObjectIdHex(id) {
w.WriteHeader(404)
return
}
oid := bson.ObjectIdHex(id)
var o OutputAddress
if err := uc.session.DB("mongolocations").C("locations").FindId(oid).One(&o); err != nil {
w.WriteHeader(404)
return
}
uj, _ := json.Marshal(o)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s", uj)
}
// Delete an already existing address
func (uc LocationController) DeleteLocation (w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("location_id")
if !bson.IsObjectIdHex(id) {
w.WriteHeader(404)
return
}
oid := bson.ObjectIdHex(id)
if err := uc.session.DB("mongolocations").C("locations").RemoveId(oid); err != nil {
w.WriteHeader(404)
return
}
w.WriteHeader(200)
fmt.Println("Address has been deleted successfully!")
}
//Update an already existing address
func (uc LocationController) UpdateLocation(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
var i InputAddress
var o OutputAddress
id := p.ByName("location_id")
if !bson.IsObjectIdHex(id) {
w.WriteHeader(404)
return
}
oid := bson.ObjectIdHex(id)
if err := uc.session.DB("mongolocations").C("locations").FindId(oid).One(&o); err != nil {
w.WriteHeader(404)
return
}
json.NewDecoder(r.Body).Decode(&i)
googResCoor := getGoogleCoordinates (i.Address + "+" + i.City + "+" + i.State + "+" + i.Zip);
fmt.Println("resp is: ", googResCoor.Coordinate.Lat, googResCoor.Coordinate.Lang);
o.Address = i.Address
o.City = i.City
o.State = i.State
o.Zip = i.Zip
o.Coordinate.Lat = googResCoor.Coordinate.Lat
o.Coordinate.Lang = googResCoor.Coordinate.Lang
c := uc.session.DB("mongolocations").C("locations")
id2 := bson.M{"_id": oid}
err := c.Update(id2, o)
if err != nil {
panic(err)
}
uj, _ := json.Marshal(o)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
fmt.Fprintf(w, "%s", uj)
}
type GoogleResponse struct {
Results []GoogleResult
}
type GoogleResult struct {
Address string `json:"formatted_address"`
AddressParts []GoogleAddressPart `json:"address_components"`
Geometry Geometry
Types []string
}
type GoogleAddressPart struct {
Name string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string
}
type Geometry struct {
Bounds Bounds
Location Point
Type string
Viewport Bounds
}
type Bounds struct {
NorthEast, SouthWest Point
}
type Point struct {
Lat float64
Lng float64
}
type LocationController struct {
session *mgo.Session
}
type InputAddress struct {
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
}
type OutputAddress 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 string `json:"lat"`
Lang string `json:"lang"`
}
}
// Reference to a LocationController with the session
func NewLocationController(s *mgo.Session) *LocationController {
return &LocationController{s}
}
func getGoogleCoordinates (address string) OutputAddress{
client := &http.Client{}
reqURL := "http://maps.google.com/maps/api/geocode/json?address=" + url.QueryEscape(address) + "&sensor=false";
req, err := http.NewRequest("GET", reqURL , nil)
resp, err := client.Do(req)
if err != nil {
fmt.Println("error in sending req to google: ", err);
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("error in reading response: ", err);
}
var res GoogleResponse
err = json.Unmarshal(body, &res)
if err != nil {
fmt.Println("error in unmashalling response: ", err);
}
var ret OutputAddress
ret.Coordinate.Lat = strconv.FormatFloat(res.Results[0].Geometry.Location.Lat,'f',7,64)
ret.Coordinate.Lang = strconv.FormatFloat(res.Results[0].Geometry.Location.Lng,'f',7,64)
return ret;
}