forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.go
95 lines (79 loc) · 2.98 KB
/
modules.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
package modules
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/hooks"
"github.com/prebid/prebid-server/v2/modules/moduledeps"
"github.com/prebid/prebid-server/v2/util/jsonutil"
)
//go:generate go run ./generator/buildergen.go
// NewBuilder returns a new module builder.
func NewBuilder() Builder {
return &builder{builders()}
}
// Builder is the interfaces intended for building modules
// implementing hook interfaces [github.com/prebid/prebid-server/hooks/hookstage].
type Builder interface {
// Build initializes existing hook modules passing them config and other dependencies.
// It returns hook repository created based on the implemented hook interfaces by modules
// and a map of modules to a list of stage names for which module provides hooks
// or an error encountered during module initialization.
Build(cfg config.Modules, client moduledeps.ModuleDeps) (hooks.HookRepository, map[string][]string, error)
}
type (
// ModuleBuilders mapping between module name and its builder: map[vendor]map[module]ModuleBuilderFn
ModuleBuilders map[string]map[string]ModuleBuilderFn
// ModuleBuilderFn returns an interface{} type that implements certain hook interfaces.
ModuleBuilderFn func(cfg json.RawMessage, deps moduledeps.ModuleDeps) (interface{}, error)
)
type builder struct {
builders ModuleBuilders
}
// Build walks over the list of registered modules and initializes them.
//
// The ID chosen for the module's hooks represents a fully qualified module path in the format
// "vendor.module_name" and should be used to retrieve module hooks from the hooks.HookRepository.
//
// Method returns a hooks.HookRepository and a map of modules to a list of stage names
// for which module provides hooks or an error occurred during modules initialization.
func (m *builder) Build(
cfg config.Modules,
deps moduledeps.ModuleDeps,
) (hooks.HookRepository, map[string][]string, error) {
modules := make(map[string]interface{})
for vendor, moduleBuilders := range m.builders {
for moduleName, builder := range moduleBuilders {
var err error
var conf json.RawMessage
var isEnabled bool
id := fmt.Sprintf("%s.%s", vendor, moduleName)
if data, ok := cfg[vendor][moduleName]; ok {
if conf, err = jsonutil.Marshal(data); err != nil {
return nil, nil, fmt.Errorf(`failed to marshal "%s" module config: %s`, id, err)
}
if values, ok := data.(map[string]interface{}); ok {
if value, ok := values["enabled"].(bool); ok {
isEnabled = value
}
}
}
if !isEnabled {
glog.Infof("Skip %s module, disabled.", id)
continue
}
module, err := builder(conf, deps)
if err != nil {
return nil, nil, fmt.Errorf(`failed to init "%s" module: %s`, id, err)
}
modules[id] = module
}
}
collection, err := createModuleStageNamesCollection(modules)
if err != nil {
return nil, nil, err
}
repo, err := hooks.NewHookRepository(modules)
return repo, collection, err
}