-
Notifications
You must be signed in to change notification settings - Fork 12
/
repository.go
202 lines (178 loc) · 5.73 KB
/
repository.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
package main
import (
"encoding/json"
"fmt"
"github.com/jitsucom/bulker/jitsubase/appbase"
"io"
"sync/atomic"
"time"
)
type RepositoryConfig struct {
RepositoryURL string `mapstructure:"REPOSITORY_URL"`
RepositoryAuthToken string `mapstructure:"REPOSITORY_AUTH_TOKEN"`
RepositoryRefreshPeriodSec int `mapstructure:"REPOSITORY_REFRESH_PERIOD_SEC" default:"2"`
}
func (r *RepositoryConfig) PostInit(settings *appbase.AppSettings) error {
return nil
}
type Streams struct {
streams []*StreamWithDestinations
apiKeyBindings map[string]*ApiKeyBinding
streamsByIds map[string]*StreamWithDestinations
streamsByDomains map[string][]*StreamWithDestinations
lastModified time.Time
}
func (s *Streams) getStreamByKeyId(keyId string) *ApiKeyBinding {
return s.apiKeyBindings[keyId]
}
func (s *Streams) GetStreamById(slug string) *StreamWithDestinations {
return s.streamsByIds[slug]
}
func (s *Streams) GetStreamsByDomain(domain string) []*StreamWithDestinations {
return s.streamsByDomains[domain]
}
func (s *Streams) GetStreams() []*StreamWithDestinations {
return s.streams
}
type StreamsRepositoryData struct {
data atomic.Pointer[Streams]
}
func (s *StreamsRepositoryData) Init(reader io.Reader, tag any) error {
dec := json.NewDecoder(reader)
// read open bracket
_, err := dec.Token()
if err != nil {
return fmt.Errorf("error reading open bracket: %v", err)
}
streams := make([]*StreamWithDestinations, 0)
apiKeyBindings := map[string]*ApiKeyBinding{}
streamsByIds := map[string]*StreamWithDestinations{}
streamsByDomains := map[string][]*StreamWithDestinations{}
// while the array contains values
for dec.More() {
swd := StreamWithDestinations{}
err = dec.Decode(&swd)
if err != nil {
return fmt.Errorf("Error unmarshalling stream config: %v", err)
}
swd.init()
streams = append(streams, &swd)
streamsByIds[swd.Stream.Id] = &swd
for _, domain := range swd.Stream.Domains {
domainStreams, ok := streamsByDomains[domain]
if !ok {
domainStreams = make([]*StreamWithDestinations, 0, 1)
}
streamsByDomains[domain] = append(domainStreams, &swd)
}
for _, key := range swd.Stream.PublicKeys {
apiKeyBindings[key.Id] = &ApiKeyBinding{
Hash: key.Hash,
KeyType: "browser",
StreamId: swd.Stream.Id,
}
}
for _, key := range swd.Stream.PrivateKeys {
apiKeyBindings[key.Id] = &ApiKeyBinding{
Hash: key.Hash,
KeyType: "s2s",
StreamId: swd.Stream.Id,
}
}
}
// read closing bracket
_, err = dec.Token()
if err != nil {
return fmt.Errorf("error reading closing bracket: %v", err)
}
data := Streams{
streams: streams,
apiKeyBindings: apiKeyBindings,
streamsByIds: streamsByIds,
streamsByDomains: streamsByDomains,
}
if tag != nil {
data.lastModified = tag.(time.Time)
}
s.data.Store(&data)
return nil
}
func (s *StreamsRepositoryData) GetData() *Streams {
return s.data.Load()
}
func (s *StreamsRepositoryData) Store(writer io.Writer) error {
d := s.data.Load()
if d != nil {
encoder := json.NewEncoder(writer)
err := encoder.Encode(d.streams)
return err
}
return nil
}
func NewStreamsRepository(url, token string, refreshPeriodSec int, cacheDir string) appbase.Repository[Streams] {
return appbase.NewHTTPRepository[Streams]("streams-with-destinations", url, token, appbase.HTTPTagLastModified, &StreamsRepositoryData{}, 1, refreshPeriodSec, cacheDir)
}
type DataLayout string
const (
DataLayoutSegmentCompatible = "segment-compatible"
DataLayoutSegmentSingleTable = "segment-single-table"
DataLayoutJitsuLegacy = "jitsu-legacy"
)
type ApiKey struct {
Id string `json:"id"`
Plaintext string `json:"plaintext"`
Hash string `json:"hash"`
Hint string `json:"hint"`
}
type ApiKeyBinding struct {
Hash string `json:"hash"`
KeyType string `json:"keyType"`
StreamId string `json:"streamId"`
}
type StreamConfig struct {
Id string `json:"id"`
Type string `json:"type"`
WorkspaceId string `json:"workspaceId"`
Name string `json:"name"`
Domains []string `json:"domains"`
AuthorizedJavaScriptDomains string `json:"authorizedJavaScriptDomains"`
PublicKeys []ApiKey `json:"publicKeys"`
PrivateKeys []ApiKey `json:"privateKeys"`
}
type ShortDestinationConfig struct {
TagDestinationConfig
Id string `json:"id"`
ConnectionId string `json:"connectionId"`
DestinationType string `json:"destinationType"`
Options map[string]any `json:"options,omitempty"`
Credentials map[string]any `json:"credentials,omitempty"`
}
type TagDestinationConfig struct {
Mode string `json:"mode,omitempty"`
Code string `json:"code,omitempty"`
}
type StreamWithDestinations struct {
Stream StreamConfig `json:"stream"`
UpdateAt time.Time `json:"updatedAt"`
BackupEnabled bool `json:"backupEnabled"`
Throttle int `json:"throttle"`
Destinations []ShortDestinationConfig `json:"destinations"`
SynchronousDestinations []*ShortDestinationConfig
AsynchronousDestinations []*ShortDestinationConfig
}
func (s *StreamWithDestinations) init() {
s.SynchronousDestinations = make([]*ShortDestinationConfig, 0)
s.AsynchronousDestinations = make([]*ShortDestinationConfig, 0)
for _, d := range s.Destinations {
if d.Id == "" || d.DestinationType == "" {
continue
}
_, ok := DeviceOptions[d.DestinationType]
cp := d
if ok {
s.SynchronousDestinations = append(s.SynchronousDestinations, &cp)
} else {
s.AsynchronousDestinations = append(s.AsynchronousDestinations, &cp)
}
}
}