-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·1224 lines (1030 loc) · 36.7 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
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"crypto/md5"
"database/sql"
"encoding/json"
"fmt"
"github.com/lib/pq"
"log"
"net/http"
"net/url"
"os"
"strconv"
"sync"
"time"
)
type Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Heading float64 `json:"heading"`
latestTime time.Time
}
//Status int constants
const pending int = 1
const confirmed int = 2
const completed int = 3
const inactive int = 0
const canceled int = 5 //only used when copying into pastPickups table
type Pickup struct {
PhoneNumber string `json:"phoneNumber"`
devicePhrase string
InitialLocation Location `json:"initialLocation"`
InitialTime time.Time `json:"initialTime"`
LatestLocation Location `json:"latestLocation"`
LatestTime time.Time `json:"latestTime"`
ConfirmTime time.Time `json:"confirmTime"`
CompleteTime time.Time `json:"completeTime"`
Status int `json:"status"`
version int
}
var pickups map[string]Pickup
var pickupsLock *sync.RWMutex
var vanLocations []Location
var startTime = time.Now()
var successResponse string
var failResponse string
var wrongPasswordResponse string
var db *(sql.DB)
var serialChannel chan func()
func generateSuccessResponse(targetString *string) {
tmp, err := json.Marshal(map[string]string{"status": "0"})
*targetString = string(tmp)
if err != nil {
fmt.Printf("Generating success response failed. %v", err)
}
}
func generateFailResponse(targetString *string) {
tmp, err := json.Marshal(map[string]string{"status": "-1"})
*targetString = string(tmp)
if err != nil {
fmt.Printf("Generating fail response failed. %v", err)
}
}
func generateWrongPasswordResponse(targetString *string) {
tmp, err := json.Marshal(map[string]string{"status": "-2"})
*targetString = string(tmp)
if err != nil {
fmt.Printf("Generating wrong password response failed. %v", err)
}
}
func doKeysExist(targetDictionary url.Values, targetKeys []string) bool {
for _, v := range targetKeys {
if _, exists := targetDictionary[v]; !exists {
return false
}
}
return true
}
func areFieldsEmpty(targetDictionary url.Values, targetKeys []string) bool {
for _, v := range targetKeys {
if len(targetDictionary[v]) == 0 {
return true
}
}
return false
}
func isFieldEmpty(field string) bool {
if len(field) > 0 {
return false
}
return true
}
func isAsyncRequest(targetDictionary url.Values) bool {
return doKeysExist(targetDictionary, []string{"async"}) && !areFieldsEmpty(targetDictionary, []string{"async"})
}
func checkMD5(password []byte) bool {
digest := fmt.Sprintf("%x", md5.Sum(password))
/*
if digest == "34d1f8a7e29f3f3497ec05d0c9c8e4fc" {
return true
}
*/
if digest == os.Getenv("SHIPMATE_PHRASE_DIGEST") {
return true
}
return false
}
func isDriverPhraseCorrect(targetDictionary url.Values) bool {
if doKeysExist(targetDictionary, []string{"phrase"}) && !areFieldsEmpty(targetDictionary, []string{"phrase"}) {
if checkMD5([]byte(targetDictionary["phrase"][0])) {
return true
} else {
//fmt.Println("Wrong driver phrase \"" + targetDictionary["phrase"][0] + "\" received")
}
} else {
fmt.Println("No phrase HTTP parameter received.")
}
return false
}
//Determine if update has failed due to holding onto stale record and update memory. Return the updated rows (if any).
func updateIfStale(targetResult sql.Result, targetTable string, targetPhoneNumber string) *(sql.Rows) {
//if result is nil, reload data
if targetResult == nil {
log.Printf("Nil result passed to stale function (query error?). Load current pickups from database into memory.")
return selectRowsFromTableByPhoneNumber(targetTable, targetPhoneNumber);
}
//If rows affected is 0, then NO row with the request "version" was found. Likely another instance has modifed it already.
if rowsAffected, _ := targetResult.RowsAffected(); rowsAffected == 0 { //Stop, get most recent version of table
log.Printf("%v rows affected. Instance had a stale entry. Load current pickups from database into memory.", rowsAffected)
return selectRowsFromTableByPhoneNumber(targetTable, targetPhoneNumber);
}
return nil
/*
if rows := selectRowsFromTableByPhoneNumber(targetTable, targetPhoneNumber); rows != nil {
if loadPickupRowsIntoMemory(rows) > 0 {
return true
} else {
setPickupToInactiveInMemory(&pickups, targetPhoneNumber)
return false
}
} else {
log.Println("selectRowsFromTableByPhoneNumber returned nil object")
return true
}
} else {
//set noIncrementVersion to true for thing like DELETE/INSERT so that the phone number in memory's version is not incremented because we may have a newer pickup in the map already
if !noIncrementVersion {
var tmp = pickups[targetPhoneNumber]
tmp.version = tmp.version+1
pickups[targetPhoneNumber] = tmp
log.Printf("OK - %v version incremented to %v", targetPhoneNumber, tmp.version)
}
}
return false //no update needed, all good
*/
}
//INSERT new pickup row in inprogress table. Return boolean on success of operation as in changes written to DB.
func databaseInsertPickupInCurrentTable(targetPickup Pickup) *(sql.Rows) {
if checkDatabaseHandleValid(db) {
var result sql.Result
var err error
if result, err = db.Exec(`INSERT INTO inprogress (PhoneNumber, DeviceId, InitialLatitude, InitialLongitude, InitialTime, LatestLatitude, LatestLongitude, LatestTime, ConfirmTime, CompleteTime, Status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);`, targetPickup.PhoneNumber, targetPickup.devicePhrase, targetPickup.InitialLocation.Latitude, targetPickup.InitialLocation.Longitude, targetPickup.InitialTime, targetPickup.LatestLocation.Latitude, targetPickup.LatestLocation.Longitude, targetPickup.LatestTime, targetPickup.ConfirmTime, targetPickup.CompleteTime, targetPickup.Status); err != nil {
log.Println(err)
} else {
rowsAffected, _ := result.RowsAffected()
fmt.Printf("INSERT %v rows affected for databaseInsertPickupInCurrentTable()\n", rowsAffected)
}
return updateIfStale(result, "inprogress", targetPickup.PhoneNumber)
}
return nil
}
//UPDATE pickup status in inprogress table
func databaseUpdatePickupStatusInCurrentTable(targetPickup Pickup, newStatus int) *(sql.Rows) {
if checkDatabaseHandleValid(db) {
var result sql.Result
var err error
if result, err = db.Exec(`UPDATE inprogress
SET Status = $1, Version = $4
WHERE PhoneNumber = $2 AND Version = $3;`, newStatus, targetPickup.PhoneNumber, targetPickup.version, targetPickup.version+1); err != nil {
log.Println(err)
} else {
rowsAffected, _ := result.RowsAffected()
fmt.Printf("UPDATE %v rows affected for databaseUpdatePickupStatusInCurrentTable()\n", rowsAffected)
}
return updateIfStale(result, "inprogress", targetPickup.PhoneNumber)
}
return nil
}
//UPDATE pickup latestLocation in inprogress table
func databaseUpdatePickupLatestLocationInCurrentTable(targetPickup Pickup) *(sql.Rows) {
if checkDatabaseHandleValid(db) {
var result sql.Result
var err error
log.Println("Phone number",targetPickup.PhoneNumber,"version", targetPickup.version)
if result, err = db.Exec(`UPDATE inprogress
SET LatestLatitude = $1, LatestLongitude = $2, LatestTime = $3, Version = $6
WHERE PhoneNumber = $4 AND Version = $5;`, targetPickup.LatestLocation.Latitude, targetPickup.LatestLocation.Longitude, targetPickup.LatestTime, targetPickup.PhoneNumber, targetPickup.version, targetPickup.version+1); err != nil {
log.Println(err)
} else {
rowsAffected, _ := result.RowsAffected()
fmt.Printf("UPDATE %v rows affected for databaseUpdatePickupLatestLocationInCurrentTable()\n", rowsAffected)
}
return updateIfStale(result, "inprogress", targetPickup.PhoneNumber)
}
return nil
}
//Copy over to pastpickups table and call function to delete from inprogress table
func databaseInsertPickupInPastTable(targetPickup Pickup) bool {
if checkDatabaseHandleValid(db) {
var result sql.Result
var err error
if result, err = db.Exec(`INSERT INTO pastpickups (PhoneNumber, DeviceId, InitialLatitude, InitialLongitude, InitialTime, LatestLatitude, LatestLongitude, LatestTime, ConfirmTime, CompleteTime, Status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);`, targetPickup.PhoneNumber, targetPickup.devicePhrase, targetPickup.InitialLocation.Latitude, targetPickup.InitialLocation.Longitude, targetPickup.InitialTime, targetPickup.LatestLocation.Latitude, targetPickup.LatestLocation.Longitude, targetPickup.LatestTime, targetPickup.ConfirmTime, targetPickup.CompleteTime, targetPickup.Status); err != nil {
log.Println(err)
} else {
rowsAffected, _ := result.RowsAffected()
fmt.Printf("INSERT %v rows affected for databaseInsertPickupInPastTable()\n", rowsAffected)
if rowsAffected == 1 {
return true
}
}
}
return false
}
//DELETE pickup from inprogress table
func databaseDeletePickupInCurrentTable(targetPickup Pickup) *(sql.Rows) {
if checkDatabaseHandleValid(db) {
var result sql.Result
var err error
//Identify pickups by phoneNumber and initialTime instead of version since the phoneNumber might have another entry with new pickup
if result, err = db.Exec(`DELETE FROM inprogress
WHERE PhoneNumber = $1 AND InitialTime = $2;`, targetPickup.PhoneNumber, targetPickup.InitialTime); err != nil {
log.Println("delete error")
log.Println(err)
} else {
rowsAffected, _ := result.RowsAffected()
fmt.Printf("DELETE %v rows affected for databaseDeletePickupInCurrentTable()\n", rowsAffected)
}
return updateIfStale(result, "inprogress", targetPickup.PhoneNumber)
}
return nil
}
//UPDATE new van location in vanlocations table
func databaseUpdateVanLocations(vanId int, targetLocation Location) bool {
if checkDatabaseHandleValid(db) {
var result sql.Result
var err error
if result, err = db.Exec(`UPDATE vanlocations
SET LatestLatitude = $1, LatestLongitude = $2, LatestTime = $3
WHERE VanId = $4;`, targetLocation.Latitude, targetLocation.Longitude, targetLocation.latestTime, vanId); err != nil {
log.Println(err)
} else {
if rowsAffected, _ := result.RowsAffected(); rowsAffected == 0 {
if result, err = db.Exec("INSERT INTO vanlocations (VanId, LatestLatitude, LatestLongitude, LatestTime) VALUES ($1, $2, $3, $4);", vanId, targetLocation.Latitude, targetLocation.Longitude, targetLocation.latestTime); err != nil {
log.Println(err)
} else {
log.Println("Created new van row on DB.")
}
} else {
//log.Println("Updated van row on DB. %v", targetLocation)
}
}
return true
}
return false
}
func updateVanLocation(w http.ResponseWriter, r *http.Request) {
log.Println("updateVanLocation()")
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//parse http parameters
r.ParseForm()
//check passphrase in "phrase" parameter
if !isDriverPhraseCorrect(r.Form) {
fmt.Fprintf(w, failResponse)
return
}
if !doKeysExist(r.Form, []string{"vanNumber", "latitude", "longitude"}) && areFieldsEmpty(r.Form, []string{"vanNumber", "latitude", "longitude"}) {
log.Println("required http parameters not found for updateVanLocation")
}
var vanNumber int
var location Location
vanNumber, err := strconv.Atoi(r.Form["vanNumber"][0])
if err != nil {
log.Println(err)
}
//5 vans max, #1-5
if vanNumber < 1 || vanNumber > 5 {
if output, err := json.Marshal(Location{}); err == nil {
fmt.Fprintf(w, string(output[:]))
} else {
log.Println(err)
}
return
}
lat, err := strconv.ParseFloat(r.Form["latitude"][0], 64)
lon, err := strconv.ParseFloat(r.Form["longitude"][0], 64)
if err != nil {
log.Println(err)
} else {
location = Location{Latitude: lat, Longitude: lon}
}
if doKeysExist(r.Form, []string{"heading"}) && !areFieldsEmpty(r.Form, []string{"heading"}) {
heading, err := strconv.ParseFloat(r.Form["heading"][0], 64)
if err != nil {
log.Println(err)
} else {
location.Heading = heading
}
} else {
location.Heading = -1
}
for len(vanLocations) < vanNumber {
vanLocations = append(vanLocations, Location{})
}
vanLocations[vanNumber-1] = location
vanLocations[vanNumber-1].latestTime = time.Now()
//reply with van location on server
if output, err := json.Marshal(vanLocations[vanNumber-1]); err == nil {
fmt.Fprintf(w, string(output[:]))
} else {
log.Println(err)
}
databaseUpdateVanLocations(vanNumber, vanLocations[vanNumber-1])
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
http.Redirect(w, r, "https://github.com/ansonl/shipmate", http.StatusFound)
log.Println("About requested")
}
func uptimeHandler(w http.ResponseWriter, r *http.Request) {
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
diff := time.Since(startTime)
fmt.Fprintf(w, "Uptime:\t%v\nPickups total:\t%v\nVans total:\t%v", diff.String(), len(pickups), len(vanLocations))
log.Println("Uptime requested")
}
func asyncTest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprintf(w, "loading")
time.Sleep(5*time.Second)
fmt.Fprintf(w, "done")
}
func newPickup(w http.ResponseWriter, r *http.Request) {
pickupsLock.Lock()
defer pickupsLock.Unlock()
log.Println("newPickup()")
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
r.ParseForm()
if !doKeysExist(r.Form, []string{"phoneNumber", "latitude", "longitude", "phrase"}) && areFieldsEmpty(r.Form, []string{"phoneNumber", "latitude", "longitude", "phrase"}) {
log.Println("required http parameters not found for newPickup")
}
var number, devicePhrase string
var location Location
/*
number, err := strconv.Atoi(r.Form["phoneNumber"][0]);
if err != nil {
log.Println(err)
}
*/
number = r.Form["phoneNumber"][0]
devicePhrase = r.Form["phrase"][0]
lat, err := strconv.ParseFloat(r.Form["latitude"][0], 64)
lon, err := strconv.ParseFloat(r.Form["longitude"][0], 64)
if err != nil {
log.Println(err)
} else {
location = Location{Latitude: lat, Longitude: lon}
}
//if someone else if already using that number and devicePhrase does not match, maybe the user reinstalled the app
//we want to allow the same device to continue using the phoneNumber if the app relaunched
if pickups[number].Status != 0 && pickups[number].devicePhrase != "" && pickups[number].devicePhrase != devicePhrase {
fmt.Fprintf(w, failResponse)
return
}
tmp := Pickup{number, devicePhrase, location, time.Now(), location, time.Now(), time.Time{}, time.Time{}, pending, 0}
//Sync to database
if isAsyncRequest(r.Form) {
fmt.Println("async requested") //TO DO
} else { //Syncronous request
//INSERT pickup as new row into inprogress table
if newRows := databaseInsertPickupInCurrentTable(tmp); newRows != nil {
loadPickupRowsIntoMemory(&pickups, newRows, nil);
fmt.Fprintf(w, failResponse)
} else {
//commit changes to instance memory
pickups[number] = tmp
if output, err := json.Marshal(pickups[number]); err == nil {
fmt.Fprintf(w, string(output))
} else {
log.Println(err)
}
}
}
}
func getPickupInfo(w http.ResponseWriter, r *http.Request) {
pickupsLock.Lock()
defer pickupsLock.Unlock()
/*
//Disable logging for getPickupInfo for brevity
log.Println("getPickupInfo()")
*/
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//parse http parameters
r.ParseForm()
if !doKeysExist(r.Form, []string{"phoneNumber", "latitude", "longitude", "phrase"}) && areFieldsEmpty(r.Form, []string{"phoneNumber", "latitude", "longitude", "phrase"}) {
log.Println("required http parameters not found for getPickupInfo")
}
var number string
var location Location
number = r.Form["phoneNumber"][0]
//if the pickup does not exist, return status 0, so that monitorStatus on iOS will show pickupInactive
if _, exist := pickups[number]; !exist {
fmt.Fprintf(w, successResponse)
return
}
//check passphrase in "phrase" parameter
if !isDriverPhraseCorrect(r.Form) {
if r.Form["phrase"][0] != pickups[number].devicePhrase && pickups[number].devicePhrase != "" {
fmt.Fprintf(w, wrongPasswordResponse)
return
}
}
var tmp = pickups[number]
if lat, err := strconv.ParseFloat(r.Form["latitude"][0], 64); err == nil {
if lon, err := strconv.ParseFloat(r.Form["longitude"][0], 64); err == nil {
location = Location{Latitude: lat, Longitude: lon}
tmp.LatestLocation = location
tmp.LatestTime = time.Now()
} else {
log.Println(err)
}
} else {
log.Println(err)
}
//Sync to database
if isAsyncRequest(r.Form) {
fmt.Println("async requested") //TO DO
} else { //Syncronous request
//INSERT pickup as new row into inprogress table
if newRows := databaseUpdatePickupLatestLocationInCurrentTable(tmp); newRows != nil {
loadPickupRowsIntoMemory(&pickups, newRows, nil);
fmt.Fprintf(w, failResponse)
} else {
//increment pickup counter in tmp struct
tmp.version = tmp.version+1
//commit changes to instance memory
pickups[number] = tmp
if output, err := json.Marshal(pickups[number]); err == nil {
fmt.Fprintf(w, string(output))
} else {
log.Println(err)
}
}
}
}
func getVanLocations(w http.ResponseWriter, r *http.Request) {
/*
//Disabled logging of getVanLocations for brevity
log.Println("getVanLocations()")
*/
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//reply with all van locations on server
if output, err := json.Marshal(vanLocations); err == nil {
fmt.Fprintf(w, string(output[:]))
} else {
log.Println(err)
}
}
func cancelPickup(w http.ResponseWriter, r *http.Request) {
pickupsLock.Lock()
defer pickupsLock.Unlock()
log.Println("cancelPickup()")
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//parse http parameters
r.ParseForm()
if !doKeysExist(r.Form, []string{"phoneNumber", "phrase"}) && areFieldsEmpty(r.Form, []string{"phoneNumber", "phrase"}) {
log.Println("required http parameters not found for getPickupInfo")
}
var number string
number = r.Form["phoneNumber"][0]
//check passphrase in "phrase" parameter
if !isDriverPhraseCorrect(r.Form) {
if r.Form["phrase"][0] != pickups[number].devicePhrase && pickups[number].devicePhrase != "" {
fmt.Fprintf(w, wrongPasswordResponse)
return
}
}
var tmp = pickups[number]
tmp.Status = inactive
tmp.LatestTime = time.Now()
tmp.devicePhrase = ""
/*
//perform INSERT, DELETE in order
//serialChannel <- func() { databaseUpdatePickupStatusInCurrentTable(number, canceled) } //canceled status (5) only shown in database, server app structs will never see it
serialChannel <- func() { databaseInsertPickupInPastTable(tmp) }
serialChannel <- func() { databaseDeletePickupInCurrentTable(tmp) }
*/
//Sync to database
if isAsyncRequest(r.Form) {
fmt.Println("async requested") //TO DO
} else { //Syncronous request
//INSERT pickup as new row into inprogress table
if databaseInsertPickupInPastTable(tmp) {
if newRows := databaseDeletePickupInCurrentTable(tmp); newRows != nil {
loadPickupRowsIntoMemory(&pickups, newRows, nil);
fmt.Fprintf(w, failResponse)
} else {
//commit changes to instance memory
pickups[number] = tmp
delete(pickups, number)
fmt.Fprintf(w, successResponse)
}
} else {
fmt.Fprintf(w, failResponse)
}
}
}
func getPickupList(w http.ResponseWriter, r *http.Request) {
//Use RLock which locks for reading only
pickupsLock.RLock()
defer pickupsLock.RUnlock()
//log.Println("getPickupList()")
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//parse http parameters
r.ParseForm()
//check passphrase in "phrase" parameter
if !isDriverPhraseCorrect(r.Form) {
fmt.Fprintf(w, wrongPasswordResponse)
return
}
if output, err := json.Marshal(pickups); err == nil {
fmt.Fprintf(w, string(output[:]))
} else {
log.Println(err)
}
}
func confirmPickup(w http.ResponseWriter, r *http.Request) {
log.Println("confirmPickup()")
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//parse http parameters
r.ParseForm()
//check passphrase in "phrase" parameter
if !isDriverPhraseCorrect(r.Form) {
fmt.Fprintf(w, failResponse)
return
}
if !doKeysExist(r.Form, []string{"phoneNumber"}) && areFieldsEmpty(r.Form, []string{"phoneNumber"}) {
log.Println("required http parameters not found for confirmPickup")
}
var number string
number = r.Form["phoneNumber"][0]
var tmp = pickups[number]
tmp.Status = confirmed
tmp.ConfirmTime = time.Now()
//Sync to database
if isAsyncRequest(r.Form) {
fmt.Println("async requested") //TO DO
} else { //Syncronous request
//INSERT pickup as new row into inprogress table
if newRows := databaseUpdatePickupStatusInCurrentTable(tmp, confirmed); newRows != nil {
loadPickupRowsIntoMemory(&pickups, newRows, nil);
fmt.Fprintf(w, failResponse)
} else {
//increment pickup counter in tmp struct
tmp.version = tmp.version+1
//commit changes to instance memory
pickups[number] = tmp
fmt.Fprintf(w, successResponse)
}
}
}
func completePickup(w http.ResponseWriter, r *http.Request) {
pickupsLock.Lock()
defer pickupsLock.Unlock()
log.Println("completePickup()")
//bypass same origin policy
w.Header().Set("Access-Control-Allow-Origin", "*")
//parse http parameters
r.ParseForm()
//check passphrase in "phrase" parameter
if !isDriverPhraseCorrect(r.Form) {
fmt.Fprintf(w, failResponse)
return
}
if !doKeysExist(r.Form, []string{"phoneNumber"}) && areFieldsEmpty(r.Form, []string{"phoneNumber"}) {
log.Println("required http parameters not found for completePickup")
}
var number string
number = r.Form["phoneNumber"][0]
var tmp = pickups[number]
tmp.Status = completed
tmp.CompleteTime = time.Now()
//Sync to database
if isAsyncRequest(r.Form) {
fmt.Println("async requested") //TO DO
} else { //Syncronous request
//INSERT pickup as new row into inprogress table
if newRows := databaseUpdatePickupStatusInCurrentTable(tmp, completed); newRows != nil {
loadPickupRowsIntoMemory(&pickups, newRows, nil);
fmt.Fprintf(w, failResponse)
} else {
databaseInsertPickupInPastTable(tmp)
//increment pickup counter in tmp struct
tmp.version = tmp.version+1
//commit changes to instance memory
pickups[number] = tmp
fmt.Fprintf(w, successResponse)
}
}
/*
//perform UPDATE, INSERT, DELETE in order
*/
//Deferred delete
go func() {
time.Sleep(1 * time.Minute) //DELETE from table after 1 minute to allow device to get completed status
if databaseDeletePickupInCurrentTable(tmp) != nil {
log.Println("Deferred DELETE of completed pickup failed")
}
//maybe should clear device phrase for phone number at this time, will test at some point to find issues
}()
}
//Check *(sql.DB) handle initialized and connected
func checkDatabaseHandleValid(targetHandle *(sql.DB)) bool {
if db != nil {
if err := db.Ping(); err == nil {
return true
} else {
fmt.Println("DB ping failed.")
}
} else {
log.Println("DB handle is nil")
}
return false
}
func server(wg *sync.WaitGroup) {
//general functions
http.HandleFunc("/", aboutHandler)
http.HandleFunc("/uptime", uptimeHandler)
//pickupee functions
http.HandleFunc("/newPickup", newPickup)
http.HandleFunc("/getPickupInfo", getPickupInfo)
http.HandleFunc("/getVanLocations", getVanLocations)
//shared functions
http.HandleFunc("/cancelPickup", cancelPickup)
//driver functions
http.HandleFunc("/getPickupList", getPickupList)
http.HandleFunc("/confirmPickup", confirmPickup)
http.HandleFunc("/completePickup", completePickup)
http.HandleFunc("/updateVanLocation", updateVanLocation)
//test functions
http.HandleFunc("/asyncTest", asyncTest)
//bind to $PORT environment variable
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
fmt.Println("Listening on " + os.Getenv("PORT"))
if err != nil {
log.Println(err)
}
wg.Done()
}
func setPickupToInactiveInMemory(targetMap *map[string]Pickup, targetPhoneNumber string) {
tmp := (*targetMap)[targetPhoneNumber]
tmp.Status = inactive
tmp.devicePhrase = ""
(*targetMap)[targetPhoneNumber] = tmp
}
//anything that is not inactive is set to inactive
func removeInactivePickups(targetMap *map[string]Pickup, timeDifference time.Duration) {
//_ = k
for _, v := range *targetMap {
if v.Status != inactive && time.Since(v.LatestTime) > timeDifference { //only check active pickups
//delete(*targetMap, k) do not delete, because we want to preserve the pickup records
/*
v.Status = inactive
*/
//we comment out above below and query db commands so that the device phrase is clear after a timeout, but the pickup is still there for accountability
//this way someone who reset theri phone or uses a new phone is get to use the same number after so many minutes
v.devicePhrase = ""
/*
(*targetMap)[k] = v
*/
/*
//perform UPDATE, INSERT, DELETE in order
serialChannel <- func() { databaseUpdatePickupStatusInCurrentTable(v, inactive) }
serialChannel <- func() { databaseInsertPickupInPastTable(v) }
serialChannel <- func() { databaseDeletePickupInCurrentTable(v) }
*/
}
}
}
func removeInactiveVanLocations(targetArray []Location, timeDifference time.Duration) {
var numberOfEmptyLocations int
for i := 0; i < len(targetArray); i++ {
fmt.Println(time.Since((targetArray)[i].latestTime))
if (targetArray[i].latestTime != time.Time{} && time.Since((targetArray)[i].latestTime) > timeDifference) {
/*
fmt.Println(time.Since((targetArray)[i].latestTime))
fmt.Println(timeDifference)
*/
targetArray[i].Latitude = 0
targetArray[i].Longitude = 0
targetArray[i].latestTime = time.Time{}
numberOfEmptyLocations++
} else if (targetArray[i].latestTime == time.Time{}) {
numberOfEmptyLocations++
}
}
//if there are 'len' empty locations in the array, no vans are around so realloc array to size 0
if numberOfEmptyLocations == len(targetArray) {
vanLocations = make([]Location, 0)
}
}
func checkForInactive(wg *sync.WaitGroup) {
t := time.NewTicker(time.Duration(30) * time.Second)
for now := range t.C {
now = now
go removeInactivePickups(&pickups, time.Duration(5)*time.Minute)
go removeInactiveVanLocations(vanLocations, time.Duration(10)*time.Minute)
}
wg.Done()
}
//Get updated table from database and return *(sql.Rows)
func selectRowsFromTable(targetTable string) *(sql.Rows) {
//we construct the SELECT query in Go because SQL does not support ordinal marker for table names
query := fmt.Sprintf("SELECT * from %v;", targetTable)
rows, err := db.Query(query)
if err != nil {
log.Println(err)
} else {
return rows
}
return nil
}
//Get specific updated row from table from database and return *(sql.Rows)
func selectRowsFromTableByPhoneNumber(targetTable string, targetPhoneNumber string) *(sql.Rows) {
//we construct the SELECT query in Go because SQL does not support ordinal marker for table names
query := fmt.Sprintf("SELECT * from %v WHERE PhoneNumber = $1;", targetTable)
rows, err := db.Query(query, targetPhoneNumber)
if err != nil {
log.Println(err)
} else {
return rows
}
return nil
}
//Scan a passed in *(sql.Rows) and load into passed map. Don't lock, this should be called from some syncronous methods
func loadPickupRowsIntoMemory(targetMap *map[string]Pickup, targetRows *(sql.Rows), notificationObj *pq.Notification) int {
var countOfRows = 0
for targetRows.Next() {
var tmpPickup Pickup
if err := targetRows.Scan(&tmpPickup.PhoneNumber, &tmpPickup.devicePhrase, &tmpPickup.InitialLocation.Latitude, &tmpPickup.InitialLocation.Longitude, &tmpPickup.InitialTime, &tmpPickup.LatestLocation.Latitude, &tmpPickup.LatestLocation.Longitude, &tmpPickup.LatestTime, &tmpPickup.ConfirmTime, &tmpPickup.CompleteTime, &tmpPickup.Status, &tmpPickup.version); err != nil {
log.Println(err)
}
fmt.Printf("Loaded existing pickup for %v\n", tmpPickup.PhoneNumber)
pickups[tmpPickup.PhoneNumber] = tmpPickup
countOfRows++
}
targetRows.Close()
log.Printf("Finished loading %v pickups.\n", countOfRows)
//Handle DELETE of a row. If no rows are returned, that means the row was deleted
if countOfRows == 0 && notificationObj != nil{
log.Println("Row", notificationObj.Extra, "deleted from database. Set to inactive in memory.")
setPickupToInactiveInMemory(&pickups, notificationObj.Extra);
}
return countOfRows
/*
//Currently no built in database/sql way to handle a column that can be time.Time or NULL. Need to write own method at some point.
var tmpConfirmTime sql.NullString
var tmpCompleteTime sql.NullString
layout := "2016-01-19 22:25:13.047371"
if tmpConfirmTime.Valid {
timeStamp, err := time.Parse(layout, tmpConfirmTime.String)
if err != nil {
tmpPickup.ConfirmTime = timeStamp
} else {
tmpPickup.ConfirmTime = time.Time{}
}
} else {
tmpPickup.ConfirmTime = time.Time{}
}
if tmpCompleteTime.Valid {
timeStamp, err := time.Parse(layout, tmpCompleteTime.String)
if err != nil {
tmpPickup.CompleteTime = timeStamp
} else {
tmpPickup.CompleteTime = time.Time{}
}
} else {
tmpPickup.CompleteTime = time.Time{}
}
*/
}
//Scan a passed in *(sql.Rows) and load into memory
func loadVanLocationRowsIntoMemory(targetRows *(sql.Rows)) {
var countOfRows = 0
for targetRows.Next() {
var vanId int
var tmpLocation Location
var version int
if err := targetRows.Scan(&vanId, &tmpLocation.Latitude, &tmpLocation.Longitude, &tmpLocation.latestTime, &version); err != nil {
log.Println(err)
}
fmt.Printf("Loaded existing van location for van ID %v\n", vanId)
for len(vanLocations) < vanId {
vanLocations = append(vanLocations, Location{})
}
vanLocations[vanId-1] = tmpLocation
countOfRows++
}
targetRows.Close()
log.Printf("Finished loading %v van locations.\n", countOfRows)
}
func setupTable(tableName string, query string) bool{
if !checkDatabaseHandleValid(db) {
return false
}
var tableExist bool
if err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1);", tableName).Scan(&tableExist); err != nil {
log.Println(err)
}
if !tableExist {
if _, err := db.Exec(query); err != nil {
log.Println(err)
} else {
return true
}