Skip to content

Commit 7a0fe32

Browse files
committed
adapters. fix state on branch.
1 parent 69a218a commit 7a0fe32

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

cmd/app/main.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ func main() {
4747
os.Exit(1)
4848
}
4949

50-
l.Infof("application started. %s\n", time.Now().Format("2006-01-02 15:04:05"))
50+
l.Infof(
51+
"application started. %s\n",
52+
time.Now().Format("2006-01-02 15:04:05"),
53+
)
5154

5255
ctx := context.Background()
5356
ctx, cancel := context.WithCancel(ctx)
@@ -72,13 +75,18 @@ func main() {
7275
os.Exit(1)
7376
}
7477

75-
deviceDetector, err := browscap_devicedetector.New(config.DeviceDBPath)
78+
deviceDetector, err := browscap_devicedetector.New(
79+
config.DeviceDBPath,
80+
)
7681
if err != nil {
7782
l.Errorf("deviceDB open: %s\n", err.Error())
7883
os.Exit(1)
7984
}
8085

81-
geoDetector, err := geoip2_detector.New(config.GeoDBPath, "ru")
86+
geoDetector, err := geoip2_detector.New(
87+
config.GeoDBPath,
88+
"ru",
89+
)
8290
if err != nil {
8391
l.Errorf("geoDB open: %s\n", err.Error())
8492
os.Exit(1)
@@ -87,7 +95,10 @@ func main() {
8795
app := core.New(s, l)
8896

8997
enabledDSPAdapters := []interfaces.DSPName{}
90-
dspConfigProvider, err := default_config_provider.New(config.DSPAdaptersConfigFilePath)
98+
dspConfigProvider, err := default_config_provider.New(
99+
config.DSPAdaptersConfigFilePath,
100+
l,
101+
)
91102
if err != nil {
92103
l.Errorf("dsp config provider: %s\n", err.Error())
93104
os.Exit(1)
@@ -105,5 +116,8 @@ func main() {
105116
os.Exit(1)
106117
}
107118

108-
l.Infof("application successfully stopped. %s", time.Now().Format("2006-01-02 15:04:05"))
119+
l.Infof(
120+
"application successfully stopped. %s",
121+
time.Now().Format("2006-01-02 15:04:05"),
122+
)
109123
}
+60-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,57 @@
11
package default_config_provider
22

3-
import "github.com/RapidCodeLab/rapid-prebid-server/internal/application/interfaces"
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"io/fs"
7+
"os"
8+
"path/filepath"
49

5-
type provider struct{}
10+
"github.com/RapidCodeLab/rapid-prebid-server/internal/application/interfaces"
11+
)
12+
13+
var NoConfigFilesErr = errors.New("no config files found in path")
14+
15+
type provider struct {
16+
data map[interfaces.DSPName]interfaces.DSPAdapterConfig
17+
}
618

719
func New(
820
path string,
21+
l interfaces.Logger,
922
) (
1023
interfaces.DSPConfigProvider,
1124
error,
1225
) {
13-
return &provider{}, nil
26+
configFiles, err := findFiles(path, ".json")
27+
if err != nil {
28+
return nil, err
29+
}
30+
if len(configFiles) < 1 {
31+
return nil, NoConfigFilesErr
32+
}
33+
34+
provider := &provider{}
35+
provider.data = make(map[interfaces.DSPName]interfaces.DSPAdapterConfig)
36+
37+
for _, filePath := range configFiles {
38+
f, err := os.Open(filePath)
39+
if err != nil {
40+
l.Errorf("read dsp config file: %s", err.Error())
41+
continue
42+
}
43+
44+
decoder := json.NewDecoder(f)
45+
config := interfaces.DSPAdapterConfig{}
46+
err = decoder.Decode(&config)
47+
if err != nil {
48+
l.Errorf("decode config:%s", err.Error())
49+
continue
50+
}
51+
provider.data[config.Name] = config
52+
}
53+
54+
return provider, nil
1455
}
1556

1657
func (i *provider) Read(
@@ -21,3 +62,19 @@ func (i *provider) Read(
2162
) {
2263
return interfaces.DSPAdapterConfig{}, nil
2364
}
65+
66+
func findFiles(root, ext string) ([]string, error) {
67+
var a []string
68+
err := filepath.WalkDir(
69+
root,
70+
func(s string, d fs.DirEntry, e error) error {
71+
if e != nil {
72+
return e
73+
}
74+
if filepath.Ext(d.Name()) == ext {
75+
a = append(a, s)
76+
}
77+
return nil
78+
})
79+
return a, err
80+
}

0 commit comments

Comments
 (0)