forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (68 loc) · 2.65 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
package main
import (
"flag"
"math/rand"
"net/http"
"path/filepath"
"runtime"
"time"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/currency"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/router"
"github.com/prebid/prebid-server/v2/server"
"github.com/prebid/prebid-server/v2/util/task"
"github.com/golang/glog"
"github.com/spf13/viper"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
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)
server.Listen(cfg, router.NoCache{Handler: corsRouter}, router.Admin(currencyConverter, fetchingInterval), r.MetricsEngine)
r.Shutdown()
return nil
}