-
Notifications
You must be signed in to change notification settings - Fork 748
/
syncersbuilder.go
125 lines (106 loc) · 3.44 KB
/
syncersbuilder.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
package usersync
import (
"fmt"
"sort"
"strings"
"github.com/prebid/prebid-server/config"
)
type namedSyncerConfig struct {
name string
cfg config.Syncer
}
// SyncerBuildError represents an error with building a syncer.
type SyncerBuildError struct {
Bidder string
SyncerKey string
Err error
}
// Error implements the standard error interface.
func (e SyncerBuildError) Error() string {
return fmt.Sprintf("cannot create syncer for bidder %s with key %s: %v", e.Bidder, e.SyncerKey, e.Err)
}
func BuildSyncers(hostConfig *config.Configuration, bidderInfos config.BidderInfos) (map[string]Syncer, []error) {
// map syncer config by bidder
cfgByBidder := make(map[string]config.Syncer, len(bidderInfos))
for bidder, cfg := range bidderInfos {
if shouldCreateSyncer(cfg) {
cfgByBidder[bidder] = *cfg.Syncer
}
}
// map syncer config by key
cfgBySyncerKey := make(map[string][]namedSyncerConfig, len(bidderInfos))
for bidder, cfg := range cfgByBidder {
if cfg.Key == "" {
cfg.Key = bidder
}
cfgBySyncerKey[cfg.Key] = append(cfgBySyncerKey[cfg.Key], namedSyncerConfig{bidder, cfg})
}
// resolve host endpoint
hostUserSyncConfig := hostConfig.UserSync
if hostUserSyncConfig.ExternalURL == "" {
hostUserSyncConfig.ExternalURL = hostConfig.ExternalURL
}
// create syncers
errs := []error{}
syncers := make(map[string]Syncer, len(bidderInfos))
for key, cfgGroup := range cfgBySyncerKey {
primaryCfg, err := chooseSyncerConfig(cfgGroup)
if err != nil {
errs = append(errs, err)
continue
}
for _, bidder := range cfgGroup {
syncer, err := NewSyncer(hostUserSyncConfig, primaryCfg.cfg, bidder.name)
if err != nil {
errs = append(errs, SyncerBuildError{
Bidder: primaryCfg.name,
SyncerKey: key,
Err: err,
})
continue
}
syncers[bidder.name] = syncer
}
}
if len(errs) > 0 {
return nil, errs
}
return syncers, nil
}
func shouldCreateSyncer(cfg config.BidderInfo) bool {
if cfg.Disabled {
return false
}
if cfg.Syncer == nil {
return false
}
// a syncer may provide just a Supports field to provide hints to the host. we should only try to create a syncer
// if there is at least one non-Supports value populated.
return cfg.Syncer.Key != "" || cfg.Syncer.IFrame != nil || cfg.Syncer.Redirect != nil || cfg.Syncer.SupportCORS != nil
}
func chooseSyncerConfig(biddersSyncerConfig []namedSyncerConfig) (namedSyncerConfig, error) {
if len(biddersSyncerConfig) == 1 {
return biddersSyncerConfig[0], nil
}
var bidderNames []string
var bidderNamesWithEndpoints []string
var syncerConfig namedSyncerConfig
for _, bidder := range biddersSyncerConfig {
bidderNames = append(bidderNames, bidder.name)
if bidder.cfg.IFrame != nil || bidder.cfg.Redirect != nil {
bidderNamesWithEndpoints = append(bidderNamesWithEndpoints, bidder.name)
syncerConfig = bidder
}
}
if len(bidderNamesWithEndpoints) == 0 {
sort.Strings(bidderNames)
bidders := strings.Join(bidderNames, ", ")
return namedSyncerConfig{}, fmt.Errorf("bidders %s share the same syncer key, but none define endpoints (iframe and/or redirect)", bidders)
}
if len(bidderNamesWithEndpoints) > 1 {
sort.Strings(bidderNamesWithEndpoints)
bidders := strings.Join(bidderNamesWithEndpoints, ", ")
return namedSyncerConfig{}, fmt.Errorf("bidders %s define endpoints (iframe and/or redirect) for the same syncer key, but only one bidder is permitted to define endpoints", bidders)
}
return syncerConfig, nil
}