-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
191 lines (142 loc) · 3.18 KB
/
main.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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"geo/geo"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
)
//go:embed escooters
var JSONFILE []byte
type EScooter struct {
ID string `json:"id"`
GPS *geo.Point `json:"gps"`
KM int64 `json: km`
AKKU int16 `json: akku`
STATE bool `json:state`
}
type scootersHandlers struct {
sync.Mutex
store map[string]EScooter
}
func newScootersHandlers() *scootersHandlers {
return &scootersHandlers{
store: setUp(JSONFILE),
}
}
func (h *scootersHandlers) scooters(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
h.get(w, r)
return
case "POST":
h.post(w, r)
return
default:
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return
}
}
func (h *scootersHandlers) post(w http.ResponseWriter, r *http.Request) {
respBody, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Something went wrong"))
return
}
/*
//TODO maybe not neccesary
//check if request type is json
if r.Header.Get("Content-Type") != "application/json" {
w.WriteHeader(http.StatusUnsupportedMediaType)
w.Write([]byte("Use only application/json"))
return
}
*/
var scooter EScooter
err = json.Unmarshal(respBody, &scooter)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
s := h.store[scooter.ID]
//Assign old values to the scooter
if scooter.GPS == nil {
scooter.GPS = s.GPS
}
if scooter.AKKU == 0 {
scooter.AKKU = s.AKKU
}
if scooter.KM == 0 {
scooter.KM = s.KM
}
h.Lock()
h.store[scooter.ID] = scooter
defer h.Unlock()
}
func (h *scootersHandlers) get(w http.ResponseWriter, r *http.Request) {
scooters := make([]EScooter, len(h.store))
h.Lock()
i := 0
for _, scooter := range h.store {
scooters[i] = scooter
i++
}
h.Unlock()
jsonBytes, err := json.Marshal(scooters)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonBytes)
}
func (h *scootersHandlers) getScooter(w http.ResponseWriter, r *http.Request) {
url := strings.Split(r.URL.String(), "/")
if len(url) != 3 {
w.WriteHeader(http.StatusNotFound)
return
}
h.Lock()
scooter, ok := h.store[url[2]]
h.Unlock()
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
jsonBytes, err := json.Marshal(scooter)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonBytes)
}
func setUp(j []byte) map[string]EScooter {
// Unmarshal in an array of EScooter's
var fJSON []EScooter
e := json.Unmarshal(j, &fJSON)
if e != nil {
fmt.Println(e.Error())
}
scooters := make(map[string]EScooter, len(fJSON))
for _, v := range fJSON {
scooters[v.ID] = v
}
return scooters
}
func main() {
sMap := newScootersHandlers()
http.HandleFunc("/api", sMap.scooters)
http.HandleFunc("/api/", sMap.getScooter)
log.Fatal(http.ListenAndServe(":10000", nil))
}