forked from gaulliath/operative-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_tracker.go
97 lines (80 loc) · 2.7 KB
/
api_tracker.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
package api
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/graniet/operative-framework/session"
"net/http"
"sort"
)
func (api *ARestFul) Trackers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
var trackers []session.Tracking
for _, tracker := range api.sess.Tracker.Tracked {
if tracker.IsHistory == true {
continue
}
trackers = append(trackers, *tracker)
}
message := api.Core.PrintData("request executed", false, trackers)
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) GetPositions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
message := api.Core.PrintData("request executed", false, api.sess.Tracker.Position)
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) GetMovers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
trackers := api.sess.Tracker.Tracked
sort.Slice(trackers, func(i, j int) bool {
return len(trackers[i].Memories) > len(trackers[j].Memories)
})
message := api.Core.PrintData("request executed", false, trackers)
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) GetBestMovers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
message := api.Core.PrintData("request executed", false, api.sess.GetTrackerBestMover())
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) Tracker(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
var t session.Tracking
params := mux.Vars(r)
identifier := params["identifier"]
for _, tracker := range api.sess.Tracker.Tracked {
if tracker.IsHistory == true {
continue
}
if tracker.Id == identifier {
t = *tracker
}
}
message := api.Core.PrintData("request executed", false, t)
_ = json.NewEncoder(w).Encode(message)
return
}
func (api *ARestFul) PutTracker(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
var tracker session.Tracking
err := json.NewDecoder(r.Body).Decode(&tracker)
if err != nil {
message := api.Core.PrintMessage(err.Error(), true)
_ = json.NewEncoder(w).Encode(message)
return
}
tracker = api.sess.AddTracker(tracker)
message := api.Core.PrintData("request executed", false, tracker)
_ = json.NewEncoder(w).Encode(message)
return
}