/
server.go
234 lines (170 loc) · 5.32 KB
/
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
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"fmt"
"log"
"io/ioutil"
"net/http"
"encoding/json"
"github.com/jasonwinn/geocoder"
"math/rand"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"github.com/julienschmidt/httprouter"
"strconv"
)
type Coordinates struct{
Lat float64 `json: "Lat"`
Lng float64 `json: "Lng"`
}
type Response struct{
Id int `json: "Id"`
Name string `json: "Name"`
Address string `json: "Address"`
City string `json: "City"`
State string `json: "State"`
Zip string `json: "Zip"`
Coordinate Coordinates `json: "Coordinate"`
}
func CreateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var idata Response
session :=connectToDb()
c:= session.DB("vinaysh").C("admin")
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatal("Read Error"+err.Error())
}
if err:= req.Body.Close();
err != nil{
log.Fatal("Body Close Error"+err.Error())
}
fmt.Println("Input JSON data", string(body))
if err:= json.Unmarshal(body, &idata);
err != nil {
panic(err)
fmt.Println("Unmarshal failed")
}
fmt.Println("Unmarshaled string is", idata.Name, idata.Address, idata.City, idata.State, idata.Zip)
randnum := rand.Int();
fmt.Println("Random number generated is",randnum)
query:= idata.Address + " " + idata.City + " " + idata.State + " " + idata.Zip
fmt.Println("Query is", query)
lat1, lng1, err := geocoder.Geocode(query)
if err != nil {
panic("THERE WAS SOME ERROR!!")
}
fmt.Println("Lat Long Coordinates")
idata.Coordinate.Lat = lat1
idata.Coordinate.Lng = lng1
fmt.Println("Latitude is",idata.Coordinate.Lat)
fmt.Println("Longitude is",idata.Coordinate.Lng)
lol:= Coordinates{idata.Coordinate.Lat, idata.Coordinate.Lng}
resp1, err := json.Marshal(lol)
if err != nil {
panic(err)
}
fmt.Println("Marshaled lol is", string(resp1))
if err := c.Insert(&Response{randnum,idata.Name,idata.Address,idata.City,idata.State,idata.Zip,idata.Coordinate});err != nil {
rw.WriteHeader(404)
return
}
rData:= Response{randnum, idata.Name, idata.Address, idata.City, idata.State, idata.Zip, idata.Coordinate}
fmt.Println("Response is",rData)
resp, err := json.Marshal(rData)
if err != nil {
panic(err)
}
fmt.Println("Marshaled Json Response is", string(resp))
rw.Header().Set("Content-Type", "application/json")
rw.Write(resp)
}
func ReadLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params){
session :=connectToDb()
c:= session.DB("vinaysh").C("admin")
var result Response
id,_ := strconv.Atoi(p.ByName("id"))
err := c.Find(bson.M{"id":id }).One(&result)
if err != nil {
rw.WriteHeader(404)
return
}
js, err := json.Marshal(result)
if err != nil {
panic(err)
}
rw.Write(js)
}
func UpdateLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var idata Response
session :=connectToDb()
c:= session.DB("vinaysh").C("admin")
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatal("Read Error"+err.Error())
}
if err:= req.Body.Close();
err != nil{
log.Fatal("Body Close Error"+err.Error())
}
fmt.Println("Input JSON data", string(body))
if err:= json.Unmarshal(body, &idata);
err != nil {
panic(err)
fmt.Println("Unmarshal failed")
}
fmt.Println("Unmarshaled string is", idata.Address, idata.City, idata.State, idata.Zip)
query:= idata.Address + " " + idata.City + " " + idata.State + " " + idata.Zip
fmt.Println("Query is", query)
lat1, lng1, err := geocoder.Geocode(query)
if err != nil {
panic("THERE WAS SOME ERROR!!")
}
fmt.Println("Lat Long Coordinates")
idata.Coordinate.Lat = lat1
idata.Coordinate.Lng = lng1
id,_ := strconv.Atoi(p.ByName("id"))
if err := c.Update(bson.M{"id": id}, bson.M{"id": id,"name":idata.Name,"address":idata.Address,"city":idata.City,"state":idata.State,"zip":idata.Zip,"coordinate":idata.Coordinate});err != nil {
rw.WriteHeader(404)
return
}
rData:= Response{id, idata.Name, idata.Address, idata.City, idata.State, idata.Zip, idata.Coordinate}
fmt.Println("Response is",rData)
resp, err := json.Marshal(rData)
if err != nil {
panic(err)
}
fmt.Println("Marshaled Json Response is", string(resp))
rw.Header().Set("Content-Type", "application/json")
rw.Write(resp)
}
func DeleteLocation(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
session :=connectToDb()
c:= session.DB("vinaysh").C("admin")
fmt.Println("Name",p.ByName("id"))
id,_ := strconv.Atoi(p.ByName("id"))
err := c.Remove(bson.M{"id":id})
if err != nil {
rw.WriteHeader(404)
return
}
}
func connectToDb() *mgo.Session{
session, err := mgo.Dial("mongodb://admin:admin@ds045054.mongolab.com:45054/vinaysh")
if err != nil {
panic("Couldn't connect to the database")
}
session.SetMode(mgo.Monotonic, true)
fmt.Println("Session is ",session)
return session
}
func main(){
mux := httprouter.New()
mux.POST("/location",CreateLocation)
mux.GET("/location/:id",ReadLocation)
mux.PUT("/location/:id",UpdateLocation)
mux.DELETE("/location/:id",DeleteLocation)
server := http.Server{
Addr: "127.0.0.1:8080",
Handler: mux,
}
server.ListenAndServe()
}