github.com/prebid/prebid-server@v0.275.0/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"math/rand"
     6  	"net/http"
     7  	"path/filepath"
     8  	"runtime"
     9  	"time"
    10  
    11  	"github.com/prebid/prebid-server/config"
    12  	"github.com/prebid/prebid-server/currency"
    13  	"github.com/prebid/prebid-server/openrtb_ext"
    14  	"github.com/prebid/prebid-server/router"
    15  	"github.com/prebid/prebid-server/server"
    16  	"github.com/prebid/prebid-server/util/task"
    17  
    18  	"github.com/golang/glog"
    19  	"github.com/spf13/viper"
    20  )
    21  
    22  func init() {
    23  	rand.Seed(time.Now().UnixNano())
    24  }
    25  
    26  func main() {
    27  	flag.Parse() // required for glog flags and testing package flags
    28  
    29  	bidderInfoPath, err := filepath.Abs(infoDirectory)
    30  	if err != nil {
    31  		glog.Exitf("Unable to build configuration directory path: %v", err)
    32  	}
    33  
    34  	bidderInfos, err := config.LoadBidderInfoFromDisk(bidderInfoPath)
    35  	if err != nil {
    36  		glog.Exitf("Unable to load bidder configurations: %v", err)
    37  	}
    38  	cfg, err := loadConfig(bidderInfos)
    39  	if err != nil {
    40  		glog.Exitf("Configuration could not be loaded or did not pass validation: %v", err)
    41  	}
    42  
    43  	// Create a soft memory limit on the total amount of memory that PBS uses to tune the behavior
    44  	// of the Go garbage collector. In summary, `cfg.GarbageCollectorThreshold` serves as a fixed cost
    45  	// of memory that is going to be held garbage before a garbage collection cycle is triggered.
    46  	// This amount of virtual memory won’t translate into physical memory allocation unless we attempt
    47  	// to read or write to the slice below, which PBS will not do.
    48  	garbageCollectionThreshold := make([]byte, cfg.GarbageCollectorThreshold)
    49  	defer runtime.KeepAlive(garbageCollectionThreshold)
    50  
    51  	err = serve(cfg)
    52  	if err != nil {
    53  		glog.Exitf("prebid-server failed: %v", err)
    54  	}
    55  }
    56  
    57  const configFileName = "pbs"
    58  const infoDirectory = "./static/bidder-info"
    59  
    60  func loadConfig(bidderInfos config.BidderInfos) (*config.Configuration, error) {
    61  	v := viper.New()
    62  	config.SetupViper(v, configFileName, bidderInfos)
    63  	return config.New(v, bidderInfos, openrtb_ext.NormalizeBidderName)
    64  }
    65  
    66  func serve(cfg *config.Configuration) error {
    67  	fetchingInterval := time.Duration(cfg.CurrencyConverter.FetchIntervalSeconds) * time.Second
    68  	staleRatesThreshold := time.Duration(cfg.CurrencyConverter.StaleRatesSeconds) * time.Second
    69  	currencyConverter := currency.NewRateConverter(&http.Client{}, cfg.CurrencyConverter.FetchURL, staleRatesThreshold)
    70  
    71  	currencyConverterTickerTask := task.NewTickerTask(fetchingInterval, currencyConverter)
    72  	currencyConverterTickerTask.Start()
    73  
    74  	r, err := router.New(cfg, currencyConverter)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	corsRouter := router.SupportCORS(r)
    80  	server.Listen(cfg, router.NoCache{Handler: corsRouter}, router.Admin(currencyConverter, fetchingInterval), r.MetricsEngine)
    81  
    82  	r.Shutdown()
    83  	return nil
    84  }