-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
325 lines (265 loc) · 7.87 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package app
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"github.com/gorilla/mux"
"time"
)
type madminServer struct {
*http.Server
}
func NewMAdminServer(port, dbPath string) *madminServer {
database := newDB(dbPath)
maUserManager := NewUserManager(database)
maHandler := NewMAdminHandler(database)
r := mux.NewRouter()
r.Handle("/data/{path:.*}", authMiddleware(maHandler, maUserManager))
r.Handle("/{path:.*}", authMiddleware(http.FileServer(http.Dir("static/")), maUserManager))
registerCleanUp(database)
return &madminServer{
&http.Server{Addr: port, Handler: r},
}
}
// todo: move functionalities to the (m madminServer) Shutdown() method
func registerCleanUp(db *sql.DB) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
for range c {
// todo: gracefully shut down http server and close db
db.Close()
os.Exit(0)
}
}()
}
type madminHandler struct {
router *mux.Router
userManager UserManager
warehouse Warehouse
database *sql.DB
}
func (m madminHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.router.ServeHTTP(w, r)
}
func NewMAdminHandler(db *sql.DB) *madminHandler {
maHandler := &madminHandler{}
maHandler.database = db
maHandler.userManager = NewUserManager(maHandler.database)
maHandler.warehouse = NewWarehouse(maHandler.database)
maHandler.router = mux.NewRouter()
maHandler.router.HandleFunc("/data/stock/{id:....-..-..-..-......}", maHandler.stockItemHandler).Methods("GET", "DELETE", "PUT")
maHandler.router.HandleFunc("/data/stock/", maHandler.stockHandler).Methods("GET", "POST")
maHandler.router.HandleFunc("/data/stock/insufficient/", maHandler.insufficientStockHandler).Methods("GET")
maHandler.router.HandleFunc("/data/stock/expiring/", maHandler.expiringStockHandler).Methods("GET")
return maHandler
}
func (m *madminHandler) stockHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
m.listStockHandler(w, r)
case "POST":
m.addStockHandler(w, r)
default:
respondMethodNotAllowed(w, r)
}
}
func (m *madminHandler) stockItemHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
m.getStockItemHandler(w, r)
case "DELETE":
m.removeStockItemHandler(w, r)
case "PUt":
m.updateStockItemHandler(w, r)
default:
respondMethodNotAllowed(w, r)
}
}
// Handler for GET /stock/
//
// Lists existing stock items.
func (m *madminHandler) listStockHandler(w http.ResponseWriter, r *http.Request) {
var (
resp = &CollectionResponseDTO{"List of existing stock items", make([]string, 0, m.warehouse.Size())}
itemURL string
)
for _, item := range m.warehouse.Stock() {
itemURL = fmt.Sprintf("/data/stock/%s", item.ID())
resp.URLs = append(resp.URLs, itemURL)
}
respBytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error in marshalling results: %s", err)
return
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write(respBytes); err != nil {
log.Printf("Error while writing response: %s", err)
}
}
// Handler for GET /stock/<id>
//
// Returns JSON with data for the stock item with the given id (if such item exists in the warehouse)
// or an emptry response with status code 204 (if there is no such item in the warehouse).
func (m *madminHandler) getStockItemHandler(w http.ResponseWriter, r *http.Request) {
var (
query = r.URL
_, id = path.Split(query.String())
item, ok = m.warehouse.ReadStock(id)
)
if !ok {
w.Header().Add("ContentLength", "0")
w.WriteHeader(http.StatusNoContent)
return
}
resp := &StockDTO{}
resp.ID = item.ID()
resp.Name = item.Name()
resp.Type = item.Type()
if item.IsExpirable() {
resp.ExpirationDate = item.ExpirationDate().String()
}
resp.MinQuantity = item.MinQuantity().String()
resp.DistributorID = item.DistributorID()
respBytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error in marshalling results: %s", err)
return
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write(respBytes); err != nil {
log.Printf("Error while writing response: %s", err)
}
}
// Handler for POST /stock/
//
// Adds an item to the warehouse.
func (m *madminHandler) addStockHandler(w http.ResponseWriter, r *http.Request) {
var (
newItem = &NewStockDTO{}
decoder = json.NewDecoder(r.Body)
err = decoder.Decode(newItem)
)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("Error in unmarshaling request body: %s", err)
return
}
defer r.Body.Close()
stockItem, err := NewStock(newItem)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("Error in creating stock item: %s", err)
return
}
m.warehouse.CreateStock(stockItem)
w.WriteHeader(http.StatusCreated)
if _, err := w.Write([]byte(stockItem.ID())); err != nil {
log.Printf("Error while writing response: %s", err)
}
}
// Handler for DELETE /stock/<id>
//
// Removes the item with <id> from the warehouse.
func (m *madminHandler) removeStockItemHandler(w http.ResponseWriter, r *http.Request) {
var (
query = r.URL
_, id = path.Split(query.String())
)
m.warehouse.DeleteStock(id)
w.WriteHeader(http.StatusNoContent)
}
// Handler for PUT /stock/<id>
//
// UPDATE the item with <id> in the warehouse
// or return error if there is no such item.
func (m *madminHandler) updateStockItemHandler(w http.ResponseWriter, r *http.Request) {
var (
query = r.URL
_, id = path.Split(query.String())
)
var (
updateDto = &StockDTO{}
decoder = json.NewDecoder(r.Body)
err = decoder.Decode(updateDto)
)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("Error in unmarshaling request body: %s", err)
return
}
defer r.Body.Close()
stockItem, ok := m.warehouse.ReadStock(id)
if !ok {
w.WriteHeader(http.StatusNotFound)
}
err = stockItem.Update(*updateDto)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
m.warehouse.UpdateStock(stockItem)
w.WriteHeader(http.StatusAccepted)
}
// Handler for GET /stock/insufficient/
//
// Lists insufficient stock items
func (m *madminHandler) insufficientStockHandler(w http.ResponseWriter, r *http.Request) {
stockItems := m.warehouse.Stock()
insufficientStockItems := make([]string, 0, len(stockItems))
for _, stock := range stockItems {
if stock.Quantity().Cmp(stock.MinQuantity()) == -1 {
itemURL := fmt.Sprintf("/data/stock/%s", stock.ID())
insufficientStockItems = append(insufficientStockItems, itemURL)
}
}
resp := &CollectionResponseDTO{"List of insufficient stock items", insufficientStockItems}
respBytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error in marshalling results: %s", err)
return
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write(respBytes); err != nil {
log.Printf("Error while writing response: %s", err)
}
}
// TODO: add list of insufficient stock items for concrete distributor
// Handler for GET /stock/expiring/
//
// Lists expiring stock items
func (m *madminHandler) expiringStockHandler(w http.ResponseWriter, r *http.Request) {
var (
stockItems = m.warehouse.Stock()
expiringStockItems = make([]string, 0, len(stockItems))
dateLimit = time.Now().AddDate(0, 0, 7)
)
for _, stock := range stockItems {
if stock.IsExpirable() {
if !stock.ExpirationDate().Before(dateLimit) {
itemURL := fmt.Sprintf("/data/stock/%s", stock.ID())
expiringStockItems = append(expiringStockItems, itemURL)
}
}
}
resp := &CollectionResponseDTO{"List of existing stock items", expiringStockItems}
respBytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error in marshalling results: %s", err)
return
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write(respBytes); err != nil {
log.Printf("Error while writing response: %s", err)
}
}