-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
156 lines (135 loc) · 3.33 KB
/
env.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
package catTrackslib
import (
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"strings"
"sync"
"time"
"github.com/jellydator/ttlcache/v3"
"github.com/paulmach/orb/geojson"
bolt "go.etcd.io/bbolt"
)
const (
testesPrefix = "testes-------"
)
var testes = false
var forwardTargetRequests map[url.URL]*ttlcache.Cache[int64, *forwardingQueueItem]
var forwardTargetRequestsLock = sync.Mutex{}
var lastPushTTLCache = ttlcache.New[string, []*geojson.Feature](
ttlcache.WithTTL[string, []*geojson.Feature](24 * time.Hour))
var tracksGZPath string
var tracksGZPathDevop string
var tracksGZPathEdge string
var masterdbpath string
var devopdbpath string
var edgedbpath string
var placesLayer bool
var (
masterlock, devoplock, edgelock string
)
// SetTestes run
func SetTestes(flagOption bool) {
testes = flagOption
}
func SetLogOutput(w io.Writer) {
log.SetOutput(w)
}
// SetForwardPopulate sets the 'downstream' urls that should be forwarded
// any request that this client receives for populating points. Forward requests
// will be sent as POST requests in identical JSON as they are received.
// NOTE that forwardPopulate is a []string, so all uri's should be given in comma-separated
// format.
func SetForwardPopulate(arguments string) {
forwardTargetRequests = make(map[url.URL]*ttlcache.Cache[int64, *forwardingQueueItem])
for _, rawURI := range strings.Split(arguments, ",") {
if rawURI == "" {
continue
}
uri, e := url.Parse(rawURI)
if e != nil {
panic(e)
}
forwardTargetRequests[*uri] = ttlcache.New[int64, *forwardingQueueItem](
ttlcache.WithTTL[int64, *forwardingQueueItem](24 * time.Hour),
)
// Start the cache cleaner
go forwardTargetRequests[*uri].Start()
log.Println("-> Set forwarding target:", uri)
}
// Run an asynchronous loop repeating attempts to forward pending requests.
go func() {
ticker := time.NewTicker(60 * time.Second)
for {
select {
case <-ticker.C:
if forwardPopulateLastRun.Add(60 * time.Second).Before(time.Now()) {
forwardTargetRequestsLock.Lock()
tryForwardPopulate()
forwardTargetRequestsLock.Unlock()
}
}
}
}()
}
func SetLiveTracksGZ(pathto string) {
tracksGZPath = pathto
}
func SetLiveTracksGZDevop(pathto string) {
tracksGZPathDevop = pathto
}
func SetLiveTracksGZEdge(pathto string) {
tracksGZPathEdge = pathto
}
func SetMasterLock(pathto string) {
masterlock = pathto
}
func SetDevopLock(pathto string) {
devoplock = pathto
}
func SetEdgeLock(pathto string) {
edgelock = pathto
}
func SetDBPath(whichdb, pathto string) {
switch whichdb {
case "master", "":
masterdbpath = pathto
case "devop":
devopdbpath = pathto
case "edge":
edgedbpath = pathto
default:
panic("invalid db name")
}
}
func SetPlacesLayer(b bool) {
placesLayer = b
}
func getTestesPrefix() string {
if testes {
return testesPrefix
}
return ""
}
// DeleteTestes wipes the entire database of all points with names prefixed with testes prefix. Saves an rm keystorke
func DeleteTestes() error {
e := GetDB("master").Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(trackKey))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
var tp TrackPoint
e := json.Unmarshal(v, &tp)
if e != nil {
fmt.Println("Error deleting testes.")
return e
}
if strings.HasPrefix(tp.Name, testesPrefix) {
b.Delete(k)
}
}
return nil
})
return e
}