This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
route.go
745 lines (734 loc) · 19.4 KB
/
route.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/* Apache v2 license
* Copyright (C) <2019> Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
package routes
import (
"database/sql"
"github.com/gorilla/mux"
"github.com/intel/rsp-sw-toolkit-im-suite-inventory-service/app/config"
"github.com/intel/rsp-sw-toolkit-im-suite-inventory-service/app/routes/handlers"
"github.com/intel/rsp-sw-toolkit-im-suite-inventory-service/pkg/middlewares"
"github.com/intel/rsp-sw-toolkit-im-suite-inventory-service/pkg/web"
)
// Route struct holds attributes to declare routes
type Route struct {
Name string
Method string
Pattern string
HandlerFunc web.Handler
}
// NewRouter creates the routes for GET and POST
func NewRouter(masterDB *sql.DB, maxSize int) *mux.Router {
inventory := handlers.Inventory{MasterDB: masterDB, MaxSize: maxSize, Url: config.AppConfig.MappingSkuUrl}
var routes = []Route{
//swagger:operation GET / default Healthcheck
//
// Healthcheck Endpoint
//
// Endpoint that is used to determine if the application is ready to take web requests
//
// ---
// consumes:
// - application/json
//
// produces:
// - application/json
//
// schemes:
// - http
//
// responses:
// '200':
// description: OK
//
{
"Index",
"GET",
"/",
inventory.Index,
},
//swagger:route GET /inventory/tags tags getTags
//
// Retrieves Tag Data
//
// This API call is used to retrieve a list of inventory tags. <br><br>
//
// + Search by epc: To search by epc, you would use the filter query parameter like so: /inventory/tags?$filter=(epc eq 'example')
//
// /inventory/tags
// /inventory/tags?$top=10&$select=epc,tid - Useful for paging data. Grab the top 10 records and only pull back the epc and tid fields
// /inventory/tags?$count - Shows how many records are in the database
// /inventory/tags?$filter=(epc eq 'example') and (tid ne '1000030404') - Filters on a particular epc whose tid does not match the one specified
// /inventory/tags?$filter=startswith(epc,'100') or endswith(epc,'003') or contains(epc,'2') - Allows you to filter based on only certain portions of an epc
//
// Example of one object being returned:<br><br>
// ```
// {
// "results":[
// {
// "arrived": 1501863300375,
// "encode_format": "tbd",
// "epc": "30143639F84191AD22900204",
// "epc_state": "",
// "event": "cycle_count",
// "facility_id": "",
// "fixed": 1,
// "gtin": "00888446671424",
// "company_prefix": 36232,
// "item_filter": 3,
// "handheld": 1,
// "last_read": 1501863300375,
// "location_history": [
// {
// "location": "RSP-95bd71",
// "source": "fixed",
// "timestamp": 1501863300375
// }
// ],
// "qualified_state": "unknown",
// "source": "fixed",
// "tid": "",
// "ttl": 1503704119
// }
// ]
// }
// ```
//
// + arrived - Arrival time in milliseconds epoch
// + encode_format - TBD
// + epc - SGTIN EPC code
// + epc_state - Current state of tag, either 'present' or 'departed'
// + event - Last event recorded for tag
// + facility_id - Facility ID
// + fixed - Count of how many times tag was read by fixed
// + gtin - GTIN-14 decoded from EPC
// + company_prefix - Part of EPC assigned by GS1
// + item_filter - Part of EPC, denotes packaging level of the item
// + handheld - Count of how many times tag was read by handheld
// + last_read - Tag last read Time in milliseconds epoch
// + location_history - Array of objects showing tag history
// + location - Location of tag at below time
// + source - Where tags were read from (fixed or handheld)
// + timestamp - Time in milliseconds epoch
// + qualified_state - Customer defined state
// + source - Where tags were read from (fixed or handheld)
// + tid - Tag manufacturer ID
// + ttl - Time to live, used for db purging - always in sync with last read
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 500: internalError
//
{
"GetTags",
"GET",
"/inventory/tags",
inventory.GetTags,
},
//swagger:operation GET /inventory/facilities facilities getFacilities
//
// Retrieves Data for Facilities
//
// This API call is used to retrieve data for facilities that are configured on RSP.<br><br>
//
// /inventory/facilities
// /inventory/facilities?$filter=(name eq 'CH6_Common_Area') - Filter facilities by name
//
// Example Result:
// ```
// {
// "results": [
// {
// "coefficients": {
// "dailyinventorypercentage": 0.01,
// "probexiterror": 0.1,
// "probinstoreread": 0.75,
// "probunreadtoread": 0.2
// },
// "name": "CH6"
// }
// ]
// }
// ```
//
// ---
// consumes:
// - application/json
//
// produces:
// - application/json
//
// schemes:
// - http
//
// responses:
// 200:
// description: OK
// schema:
// type: object
// properties:
// results:
// type: array
// description: Array containing results of query
// items:
// "$ref": "#/definitions/Facility"
// count:
// description: Count of records for query
// type: integer
//
// 400:
// description: BadRequest
// schema:
// "$ref": "#/responses/schemaValidation"
// 500:
// description: InternalError
// schema:
// "$ref": "#/responses/internalError"
//
{
"GetFacilities",
"GET",
"/inventory/facilities",
inventory.GetFacilities,
},
//swagger:operation GET /inventory/handheldevents handheldevents getHandheldevents
//
// Retrieves Handheld Event Data
//
// This API call is used to retrieve handheld events that have been received.<br><br>
//
// + `/inventory/handheldevents`
// + `/inventory/handheldevents?$filter=(event eq 'FullScanStart')`
// + `/inventory/handheldevents?$filter=(event eq 'FullScanComplete')`
// + `/inventory/handheldevents?$filter=(event eq 'Calculate')`
//
// Example Result:
// ```
// {
// "results": [
// {
// "_id": "59d2818dd0cb6260bf85e3cf",
// "timestamp": 1506967944919,
// "event": "FullScanStart"
// },
// {
// "_id": "59d28294d0cb6260bf85f70e",
// "timestamp": 1506968207311,
// "event": "FullScanComplete"
// },
// {
// "_id": "59d28294d0cb6260bf85f710",
// "timestamp": 1506968212265,
// "event": "Calculate"
// }]
// }
// ```
//
// ---
// consumes:
// - application/json
//
// produces:
// - application/json
//
// schemes:
// - http
//
// responses:
// 200:
// description: OK
// schema:
// description: Results Response
// type: object
// properties:
// results:
// type: array
// description: Array containing results of query
// items:
// "$ref": "#/definitions/HandheldEvent"
// count:
// description: Count of records for query
// type: integer
// 400:
// "$ref": "#/responses/schemaValidation"
// 500:
// "$ref": "#/responses/internalError"
//
{
"GetHandheldEvents",
"GET",
"/inventory/handheldevents",
inventory.GetHandheldEvents,
},
//swagger:route POST /inventory/query/current current postCurrentInventory
//
// Post current inventory snapshot to the cloud connector
//
// Example Request Input:
// ```
// {
// "qualified_state":"sold",
// "facility_id":"store001"
// }
// ```
//
//
//
// + __qualified_state__ - User set qualified state for the item
// + __facility_id__ - Return only facilities provided
// + __epc_state__ - EPC state of 'present' or 'departed'
// + __starttime__ - Millisecond epoch start time
// + __endtime__ - Millisecond epoch stop time
//
//
//
// ---
// consumes:
// - application/json
//
// produces:
// - application/json
//
// schemes:
// - http
//
// responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"PostCurrentInventory",
"POST",
"/inventory/query/current",
inventory.PostCurrentInventory,
},
//swagger:route POST /inventory/query/searchByProductID searchByProductID GetSearchByProductID
//
// Retrieves EPC data corresponding to specified ProductID
//
// Returns a list of unique EPCs matching the ProductID provided. Body parameters shall be provided in request body in JSON format.<br><br>
//
// Example Request Input:
// ```
// {
// "productId":"00012345678905",
// "facility_id":"store001",
// "confidence":.75,
// "cursor":"aGksIDovMSB0aGlz",
// "size":500,
// "count_only":false
// }
// ```
//
//
// + productId - A valid productId(GTIN-14) to search for
// + facility_id - Return only facilities provided
// + confidence - Minimum probability items must meet
// + cursor - Cursor from previous response used to retrieve next page of results
// + size - Number of results per page
// + count_only - Return only tag count
//
//
//
// Example Response:
// ```
// {
// "paging":{
// "cursor":"string"
// },
// "results":[
// {
// "epc":"string",
// "facility_id":"string",
// "event":"string",
// "productId":"string",
// "last_read":0,
// "arrived":0,
// "epc_state":"string",
// "confidence":0,
// "encode_format":"string",
// "tid":"string",
// "qualified_state":"string",
// "epc_context":"string",
// "location_history":[
// {
// "location":"string",
// "timestamp":0
// }
// ]
// }
// ]
// }
// ```
//
// + paging - Paging object
// + cursor - Cursor used to get next page of results
// + results - Array of result objects
// + epc - SGTIN EPC code
// + facility_id - Facility ID
// + event - Last event recorded for tag
// + productId - productId(GTIN-14)
// + last_read - Tag last read Time in milliseconds epoch
// + arrived - Arrival time in milliseconds epoch
// + epc_state - Current state of tag, either 'present' or 'departed'
// + confidence - Probability item is in inventory
// + encode_format -
// + tid - Tag manufacturer ID
// + qualified_state - Customer defined state
// + epc_context - Customer defined context
// + location_history - Array of objects showing tag history
// + location - Location of tag at below time
// + timestamp - Time in milliseconds epoch
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"GetSearchByProductID",
"POST",
"/inventory/query/searchByProductID",
inventory.GetSearchByProductID,
},
//swagger:route PUT /inventory/update/coefficients update updateCoefficients
//
// Update Facility Coefficents
//
// This API call is used to update probabilistic algorithm coefficients for a particular facility. Coefficient variables are used to calculate the confidence of a tag. Default values are set as configuration variables.<br><br>
//
//
// Example Request Input:
// ```
// {
// "dailyinventorypercentage": 0.01,
// "probexiterror": 0.1,
// "probinstoreread": 0.75,
// "probunreadtoread": 0.2,
// "facility_id": "Facility"
// }
// ```
//
//
// + dailyinventorypercentage - Percent of inventory that is sold daily
// + probexiterror - Probability of an exit error (missed 'departed' event) occurring
// + probinstoreread - Probability of a tag in the store being read by the overhead sensor each day
// + probunreadtoread - Probability of an unreadable tag becoming readable again each day (i.e. moved or retagged)
// + facility_id - Facility name
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 500: internalError
//
{
"UpdateCoefficients",
"PUT",
"/inventory/update/coefficients",
inventory.UpdateCoefficients,
},
//swagger:route PUT /inventory/update/qualifiedstate update updateQualifiedState
//
// Upload inventory events
//
// The update endpoint is for uploading inventory events such as those from a handheld RFID reader.<br><br>
//
// Example Request Input:
// ```
// {
// "qualified_state":"string",
// "epc":"string",
// "facility_id":"string"
// }
// ```
//
// + qualified_state - User-defined state
// + epc - SGTIN-96 EPC
// + facility_id - Facility code or identifier
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"UpdateQualifiedState",
"PUT",
"/inventory/update/qualifiedstate",
inventory.UpdateQualifiedState,
},
//swagger:route POST /inventory/search epc getSearchByEpc
//
// Retrieves tag data corresponding to specified EPC pattern
//
// Returns a list of tags with their EPCs matching a pattern. Body parameters shall be provided in request body in JSON format.<br><br>
//
// Example Request Input:
// ```
// {
// "epc":"3038E511C6E9A6400012D687",
// "facility_id":"store001",
// "cursor":"aGksIDovMSB0aGlz",
// "size":500
// }
// ```
//
// + epc - EPC search string which can contain a single asterisk at the beginning, middle, or end of EPC string
// + facility_id - Facility code or identifier
// + cursor - Cursor from previous response used to retrieve next page of results
// + size - Number of results per page
//
// Example Response:
// ```
// {
// "paging":{
// "cursor":"string"
// },
// "results":[
// {
// "epc":"string",
// "facility_id":"string",
// "event":"string",
// "gtin":"string",
// "last_read":0,
// "arrived":0,
// "epc_state":"string",
// "confidence":0,
// "encode_format":"string",
// "tid":"string",
// "qualified_state":"string",
// "epc_context":"string",
// "location_history":[
// {
// "location":"string",
// "timestamp":0
// }
// ]
// }
// ]
// }
// ```
//
// + paging - Paging object
// + cursor - Cursor used to get next page of results
// + results - Array of result objects
// + epc - SGTIN EPC code
// + facility_id - Facility ID
// + event - Last event recorded for tag
// + gtin - GTIN-14 decoded from EPC
// + last_read - Tag last read Time in milliseconds epoch
// + arrived - Arrival time in milliseconds epoch
// + epc_state - Current state of tag, either 'present' or 'departed'
// + confidence - Probability item is in inventory
// + encode_format -
// + tid - Tag manufacturer ID
// + qualified_state - Customer defined state
// + epc_context - Customer defined context
// + location_history - Array of objects showing tag history
// + location - Location of tag at below time
// + timestamp - Time in milliseconds epoch
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"GetSearchByEpc",
"POST",
"/inventory/search",
inventory.GetSearchByEpc,
},
//swagger:route PUT /inventory/update/epccontext epc setEpcContext
//
// Set EPC context
//
// This endpoint allows the customer to arbitrarily set the context for a particular EPC. For example, the customer may want to mark the tag as received, sold, lost, stolen, and anything else the customer decides is appropriate. Body parameters shall be provided in request body in JSON format.<br><br>
//
// Example Request Input:
// ```
// {
// "epc_context":"received",
// "epc":"3038E511C6E9A6400012D687",
// "facility_id":"store555"
// }
// ```
//
// + epc_context - User-defined context
// + facility_id - Facility code or identifier
// + epc - SGTIN-96 EPC
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"SetEpcContext",
"PUT",
"/inventory/update/epccontext",
inventory.SetEpcContext,
},
//swagger:route DELETE /inventory/update/epccontext epc deleteEpcContext
//
// Delete EPC context
//
// This endpoint allows the customer to delete the context for a particular EPC. Body parameters shall be provided in request body in JSON format.<br><br>
//
// Example Request Input:
// ```
// {
// "epc":"3038E511C6E9A6400012D687",
// "facility_id":"store100"
// }
// ```
//
// + epc - SGTIN-96 EPC
// + facility_id - Facility code or identifier
//
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"DeleteEpcContext",
"DELETE",
"/inventory/update/epccontext",
inventory.DeleteEpcContext,
},
//swagger:route DELETE /inventory/tags tags deleteAllTags
//
// Delete Tag Collection in database
//
// This endpoint allows the customer to delete all the tags in the tags table.<br><br>
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 204: body:resultsResponse
// 400: schemaValidation
// 403: forbidden
// 500: internalError
// 502: externalError
// 503: serviceUnavailable
// 504: externalServiceTimeout
//
{
"DeleteAllTags",
"DELETE",
"/inventory/tags",
inventory.DeleteAllTags,
},
}
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler = route.HandlerFunc
handler = middlewares.Recover(handler)
handler = middlewares.Logger(handler)
handler = middlewares.Bodylimiter(handler)
if config.AppConfig.EnableCORS {
handler = middlewares.CORS(config.AppConfig.CORSOrigin, handler)
}
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)
}
return router
}