forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (57 loc) · 2.14 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
package main
import (
"flag"
"math/rand"
"net/http"
"runtime"
"time"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/currency"
"github.com/prebid/prebid-server/router"
"github.com/prebid/prebid-server/server"
"github.com/prebid/prebid-server/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
cfg, err := loadConfig()
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"
func loadConfig() (*config.Configuration, error) {
v := viper.New()
config.SetupViper(v, configFileName)
return config.New(v)
}
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
}