github.com/prebid/prebid-server/v2@v2.18.0/main.go (about)

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