-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_coordinates_post.go
99 lines (96 loc) · 2.61 KB
/
server_coordinates_post.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
package api
// Copyright 2016, JuanDeFu.ca. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
import (
"encoding/json"
"net/http"
"sabey.co/spoofgo/coordinates"
)
func (self *Server) SetCoordinates(w http.ResponseWriter, r *http.Request) {
// SETS COORDINATES/ACCELERATING IF KEY EXISTS
// returns API
/*
Example:
clear && curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"key":"vancouver","accelerating":false}' http://localhost:8844/coordinates/set; echo
*/
if r.Method != "POST" {
// failed
api := self.GetAPI()
api.Error = "POST only!"
self.APIRespond(api, w, r, 400)
return
}
decoder := json.NewDecoder(r.Body)
post := &APICoordinates{}
err := decoder.Decode(&post)
if err != nil {
// failed
api := self.GetAPI()
api.Error = "failed to decode"
self.APIRespond(api, w, r, 400)
return
}
// success
coordinates.Set(post.Key, post.Accelerating)
self.APIRespond(nil, w, r, 200)
}
func (self *Server) AddCoordinates(w http.ResponseWriter, r *http.Request) {
// ADD COORDINATES
/*
Example:
clear && curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"key":"vancouver","latitude":49.282220,"longitude":-123.121165}' http://localhost:8844/coordinates/add; echo
*/
if r.Method != "POST" {
// failed
api := &APICoordinates{
Error: "POST only!",
}
self.APICoordinatesRespond(api, w, r, 400)
return
}
decoder := json.NewDecoder(r.Body)
post := &APICoordinates{}
err := decoder.Decode(&post)
if err != nil {
// failed
api := &APICoordinates{
Error: "POST only!",
}
api.Error = "failed to decode"
self.APICoordinatesRespond(api, w, r, 400)
return
}
// success
coordinates.Add(post.Key, post.Latitude, post.Longitude)
self.APICoordinatesRespond(nil, w, r, 200)
}
func (self *Server) DeleteCoordinates(w http.ResponseWriter, r *http.Request) {
// ADD COORDINATES
/*
Example:
clear && curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"key":"vancouver"}' http://localhost:8844/coordinates/delete; echo
*/
if r.Method != "POST" {
// failed
api := &APICoordinates{
Error: "POST only!",
}
self.APICoordinatesRespond(api, w, r, 400)
return
}
decoder := json.NewDecoder(r.Body)
post := &APICoordinates{}
err := decoder.Decode(&post)
if err != nil {
// failed
api := &APICoordinates{
Error: "POST only!",
}
api.Error = "failed to decode"
self.APICoordinatesRespond(api, w, r, 400)
return
}
// success
coordinates.Delete(post.Key)
self.APICoordinatesRespond(nil, w, r, 200)
}