-
Notifications
You must be signed in to change notification settings - Fork 743
/
main.go
87 lines (71 loc) · 2.84 KB
/
main.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
package main
import (
"flag"
"net/http"
"path/filepath"
"runtime"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/currency"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/router"
"github.com/prebid/prebid-server/v3/server"
"github.com/prebid/prebid-server/v3/util/jsonutil"
"github.com/prebid/prebid-server/v3/util/task"
"github.com/golang/glog"
"github.com/spf13/viper"
)
func init() {
jsoniter.RegisterExtension(&jsonutil.RawMessageExtension{})
}
func main() {
flag.Parse() // required for glog flags and testing package flags
bidderInfoPath, err := filepath.Abs(infoDirectory)
if err != nil {
glog.Exitf("Unable to build configuration directory path: %v", err)
}
bidderInfos, err := config.LoadBidderInfoFromDisk(bidderInfoPath)
if err != nil {
glog.Exitf("Unable to load bidder configurations: %v", err)
}
cfg, err := loadConfig(bidderInfos)
if err != nil {
glog.Exitf("Configuration could not be loaded or did not pass validation: %v", err)
}
// Create a soft memory limit on the total amount of memory that PBS uses to tune the behavior
// of the Go garbage collector. In summary, `cfg.GarbageCollectorThreshold` serves as a fixed cost
// of memory that is going to be held garbage before a garbage collection cycle is triggered.
// This amount of virtual memory won’t translate into physical memory allocation unless we attempt
// to read or write to the slice below, which PBS will not do.
garbageCollectionThreshold := make([]byte, cfg.GarbageCollectorThreshold)
defer runtime.KeepAlive(garbageCollectionThreshold)
err = serve(cfg)
if err != nil {
glog.Exitf("prebid-server failed: %v", err)
}
}
const configFileName = "pbs"
const infoDirectory = "./static/bidder-info"
func loadConfig(bidderInfos config.BidderInfos) (*config.Configuration, error) {
v := viper.New()
config.SetupViper(v, configFileName, bidderInfos)
return config.New(v, bidderInfos, openrtb_ext.NormalizeBidderName)
}
func serve(cfg *config.Configuration) error {
fetchingInterval := time.Duration(cfg.CurrencyConverter.FetchIntervalSeconds) * time.Second
staleRatesThreshold := time.Duration(cfg.CurrencyConverter.StaleRatesSeconds) * time.Second
currencyConverter := currency.NewRateConverter(&http.Client{}, cfg.CurrencyConverter.FetchURL, staleRatesThreshold)
currencyConverterTickerTask := task.NewTickerTask(fetchingInterval, currencyConverter)
currencyConverterTickerTask.Start()
r, err := router.New(cfg, currencyConverter)
if err != nil {
return err
}
corsRouter := router.SupportCORS(r)
if err := server.Listen(cfg, router.NoCache{Handler: corsRouter}, router.Admin(currencyConverter, fetchingInterval), r.MetricsEngine); err != nil {
glog.Fatalf("prebid-server returned an error: %v", err)
}
r.Shutdown()
return nil
}