forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbs_light.go
745 lines (664 loc) · 25.6 KB
/
pbs_light.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package main
import (
"context"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/pprof"
"sort"
"strconv"
"time"
"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
"github.com/mssola/user_agent"
"github.com/rs/cors"
"github.com/spf13/viper"
"crypto/tls"
"strings"
_ "github.com/lib/pq"
"github.com/prebid/prebid-server/adapters"
"github.com/prebid/prebid-server/adapters/adform"
"github.com/prebid/prebid-server/adapters/appnexus"
"github.com/prebid/prebid-server/adapters/audienceNetwork"
"github.com/prebid/prebid-server/adapters/conversant"
"github.com/prebid/prebid-server/adapters/indexExchange"
"github.com/prebid/prebid-server/adapters/lifestreet"
"github.com/prebid/prebid-server/adapters/pubmatic"
"github.com/prebid/prebid-server/adapters/pulsepoint"
"github.com/prebid/prebid-server/adapters/rubicon"
"github.com/prebid/prebid-server/adapters/sovrn"
analyticsConf "github.com/prebid/prebid-server/analytics/config"
"github.com/prebid/prebid-server/cache"
"github.com/prebid/prebid-server/cache/dummycache"
"github.com/prebid/prebid-server/cache/filecache"
"github.com/prebid/prebid-server/cache/postgrescache"
"github.com/prebid/prebid-server/config"
"github.com/prebid/prebid-server/endpoints"
infoEndpoints "github.com/prebid/prebid-server/endpoints/info"
"github.com/prebid/prebid-server/endpoints/openrtb2"
"github.com/prebid/prebid-server/exchange"
"github.com/prebid/prebid-server/gdpr"
"github.com/prebid/prebid-server/openrtb_ext"
"github.com/prebid/prebid-server/pbs"
"github.com/prebid/prebid-server/pbsmetrics"
metricsConf "github.com/prebid/prebid-server/pbsmetrics/config"
pbc "github.com/prebid/prebid-server/prebid_cache_client"
"github.com/prebid/prebid-server/server"
"github.com/prebid/prebid-server/ssl"
"github.com/prebid/prebid-server/usersync"
"github.com/prebid/prebid-server/usersync/usersyncers"
storedRequestsConf "github.com/prebid/prebid-server/stored_requests/config"
)
// Holds binary revision string
// Set manually at build time using:
// go build -ldflags "-X main.Rev=`git rev-parse --short HEAD`"
// Populated automatically at build / release time via .travis.yml
// `gox -os="linux" -arch="386" -output="{{.Dir}}_{{.OS}}_{{.Arch}}" -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose ./...;`
// See issue #559
var Rev string
var exchanges map[string]adapters.Adapter
var dataCache cache.Cache
type bidResult struct {
bidder *pbs.PBSBidder
bid_list pbs.PBSBidSlice
}
const schemaDirectory = "./static/bidder-params"
const defaultPriceGranularity = "med"
// Constant keys for ad server targeting for responses to Prebid Mobile
const hbpbConstantKey = "hb_pb"
const hbCreativeLoadMethodConstantKey = "hb_creative_loadtype"
const hbBidderConstantKey = "hb_bidder"
const hbCacheIdConstantKey = "hb_cache_id"
const hbDealIdConstantKey = "hb_deal"
const hbSizeConstantKey = "hb_size"
// hb_creative_loadtype key can be one of `demand_sdk` or `html`
// default is `html` where the creative is loaded in the primary ad server's webview through AppNexus hosted JS
// `demand_sdk` is for bidders who insist on their creatives being loaded in their own SDK's webview
const hbCreativeLoadMethodHTML = "html"
const hbCreativeLoadMethodDemandSDK = "demand_sdk"
func min(x, y int) int {
if x < y {
return x
}
return y
}
func writeAuctionError(w http.ResponseWriter, s string, err error) {
var resp pbs.PBSResponse
if err != nil {
resp.Status = fmt.Sprintf("%s: %v", s, err)
} else {
resp.Status = s
}
b, err := json.Marshal(&resp)
if err != nil {
glog.Errorf("Failed to marshal auction error JSON: %s", err)
} else {
w.Write(b)
}
}
type auctionDeps struct {
cfg *config.Configuration
syncers map[openrtb_ext.BidderName]usersync.Usersyncer
gdprPerms gdpr.Permissions
metricsEngine pbsmetrics.MetricsEngine
}
func (deps *auctionDeps) auction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
labels := pbsmetrics.Labels{
Source: pbsmetrics.DemandUnknown,
RType: pbsmetrics.ReqTypeLegacy,
PubID: "",
Browser: pbsmetrics.BrowserOther,
CookieFlag: pbsmetrics.CookieFlagUnknown,
RequestStatus: pbsmetrics.RequestStatusOK,
}
if ua := user_agent.New(r.Header.Get("User-Agent")); ua != nil {
name, _ := ua.Browser()
if name == "Safari" {
labels.Browser = pbsmetrics.BrowserSafari
}
}
pbs_req, err := pbs.ParsePBSRequest(r, &deps.cfg.AuctionTimeouts, dataCache, &(deps.cfg.HostCookie))
// Defer here because we need pbs_req defined.
defer func() {
if pbs_req == nil {
deps.metricsEngine.RecordRequest(labels)
deps.metricsEngine.RecordImps(labels, 0)
} else {
// handles the case that ParsePBSRequest returns an error, so pbs_req.Start is not defined
deps.metricsEngine.RecordRequest(labels)
deps.metricsEngine.RecordImps(labels, len(pbs_req.AdUnits))
deps.metricsEngine.RecordRequestTime(labels, time.Since(pbs_req.Start))
}
}()
if err != nil {
if glog.V(2) {
glog.Infof("Failed to parse /auction request: %v", err)
}
writeAuctionError(w, "Error parsing request", err)
labels.RequestStatus = pbsmetrics.RequestStatusBadInput
return
}
status := "OK"
if pbs_req.App != nil {
labels.Source = pbsmetrics.DemandApp
} else {
labels.Source = pbsmetrics.DemandWeb
if pbs_req.Cookie.LiveSyncCount() == 0 {
labels.CookieFlag = pbsmetrics.CookieFlagNo
status = "no_cookie"
} else {
labels.CookieFlag = pbsmetrics.CookieFlagYes
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(pbs_req.TimeoutMillis))
defer cancel()
account, err := dataCache.Accounts().Get(pbs_req.AccountID)
if err != nil {
if glog.V(2) {
glog.Infof("Invalid account id: %v", err)
}
writeAuctionError(w, "Unknown account id", fmt.Errorf("Unknown account"))
labels.RequestStatus = pbsmetrics.RequestStatusBadInput
return
}
labels.PubID = pbs_req.AccountID
pbs_resp := pbs.PBSResponse{
Status: status,
TID: pbs_req.Tid,
BidderStatus: pbs_req.Bidders,
}
ch := make(chan bidResult)
sentBids := 0
for _, bidder := range pbs_req.Bidders {
if ex, ok := exchanges[bidder.BidderCode]; ok {
// Make sure we have an independent label struct for each bidder. We don't want to run into issues with the goroutine below.
blabels := pbsmetrics.AdapterLabels{
Source: labels.Source,
RType: labels.RType,
Adapter: openrtb_ext.BidderMap[bidder.BidderCode],
PubID: labels.PubID,
Browser: labels.Browser,
CookieFlag: labels.CookieFlag,
AdapterBids: pbsmetrics.AdapterBidPresent,
}
if blabels.Adapter == "" {
// "districtm" is legal, but not in BidderMap. Other values will log errors in the go_metrics code
blabels.Adapter = openrtb_ext.BidderName(bidder.BidderCode)
}
if pbs_req.App == nil {
// If exchanges[bidderCode] exists, then deps.syncers[bidderCode] exists *except for districtm*.
// OpenRTB handles aliases differently, so this hack will keep legacy code working. For all other
// bidderCodes, deps.syncers[bidderCode] will exist if exchanges[bidderCode] also does.
// This is guaranteed by the following unit tests, which compare these maps to the (source of truth) openrtb_ext.BidderMap:
// 1. TestSyncers inside usersync/usersync_test.go
// 2. TestExchangeMap inside pbs_light_test.go
syncerCode := bidder.BidderCode
if syncerCode == "districtm" {
syncerCode = "appnexus"
}
syncer := deps.syncers[openrtb_ext.BidderName(syncerCode)]
uid, _, _ := pbs_req.Cookie.GetUID(syncer.FamilyName())
if uid == "" {
bidder.NoCookie = true
gdprApplies := pbs_req.ParseGDPR()
consent := pbs_req.ParseConsent()
if deps.shouldUsersync(ctx, openrtb_ext.BidderName(syncerCode), gdprApplies, consent) {
bidder.UsersyncInfo = syncer.GetUsersyncInfo(gdprApplies, consent)
}
blabels.CookieFlag = pbsmetrics.CookieFlagNo
if ex.SkipNoCookies() {
continue
}
}
}
sentBids++
go func(bidder *pbs.PBSBidder, blables pbsmetrics.AdapterLabels) {
start := time.Now()
bid_list, err := ex.Call(ctx, pbs_req, bidder)
deps.metricsEngine.RecordAdapterTime(blabels, time.Since(start))
bidder.ResponseTime = int(time.Since(start) / time.Millisecond)
if err != nil {
var s struct{}
switch err {
case context.DeadlineExceeded:
blabels.AdapterErrors = map[pbsmetrics.AdapterError]struct{}{pbsmetrics.AdapterErrorTimeout: s}
bidder.Error = "Timed out"
case context.Canceled:
fallthrough
default:
bidder.Error = err.Error()
switch err.(type) {
case *adapters.BadInputError:
blabels.AdapterErrors = map[pbsmetrics.AdapterError]struct{}{pbsmetrics.AdapterErrorBadInput: s}
case *adapters.BadServerResponseError:
blabels.AdapterErrors = map[pbsmetrics.AdapterError]struct{}{pbsmetrics.AdapterErrorBadServerResponse: s}
default:
glog.Warningf("Error from bidder %v. Ignoring all bids: %v", bidder.BidderCode, err)
blabels.AdapterErrors = map[pbsmetrics.AdapterError]struct{}{pbsmetrics.AdapterErrorUnknown: s}
}
}
} else if bid_list != nil {
bid_list = checkForValidBidSize(bid_list, bidder)
bidder.NumBids = len(bid_list)
for _, bid := range bid_list {
var cpm = float64(bid.Price * 1000)
deps.metricsEngine.RecordAdapterPrice(blables, cpm)
switch bid.CreativeMediaType {
case "banner":
deps.metricsEngine.RecordAdapterBidReceived(blabels, openrtb_ext.BidTypeBanner, bid.Adm != "")
case "video":
deps.metricsEngine.RecordAdapterBidReceived(blabels, openrtb_ext.BidTypeVideo, bid.Adm != "")
}
bid.ResponseTime = bidder.ResponseTime
}
} else {
bidder.NoBid = true
blabels.AdapterBids = pbsmetrics.AdapterBidNone
}
ch <- bidResult{
bidder: bidder,
bid_list: bid_list,
// Bidder done, record bidder metrics
}
deps.metricsEngine.RecordAdapterRequest(blabels)
}(bidder, blabels)
} else {
bidder.Error = "Unsupported bidder"
}
}
for i := 0; i < sentBids; i++ {
result := <-ch
for _, bid := range result.bid_list {
pbs_resp.Bids = append(pbs_resp.Bids, bid)
}
}
if pbs_req.CacheMarkup == 1 {
cobjs := make([]*pbc.CacheObject, len(pbs_resp.Bids))
for i, bid := range pbs_resp.Bids {
if bid.CreativeMediaType == "video" {
cobjs[i] = &pbc.CacheObject{
Value: bid.Adm,
IsVideo: true,
}
} else {
cobjs[i] = &pbc.CacheObject{
Value: &pbc.BidCache{
Adm: bid.Adm,
NURL: bid.NURL,
Width: bid.Width,
Height: bid.Height,
},
IsVideo: false,
}
}
}
err = pbc.Put(ctx, cobjs)
if err != nil {
writeAuctionError(w, "Prebid cache failed", err)
labels.RequestStatus = pbsmetrics.RequestStatusErr
return
}
for i, bid := range pbs_resp.Bids {
bid.CacheID = cobjs[i].UUID
bid.CacheURL = deps.cfg.GetCachedAssetURL(bid.CacheID)
bid.NURL = ""
bid.Adm = ""
}
}
if pbs_req.CacheMarkup == 2 {
cacheVideoOnly(pbs_resp.Bids, ctx, w, deps, &labels)
}
if pbs_req.SortBids == 1 {
sortBidsAddKeywordsMobile(pbs_resp.Bids, pbs_req, account.PriceGranularity)
}
if glog.V(2) {
glog.Infof("Request for %d ad units on url %s by account %s got %d bids", len(pbs_req.AdUnits), pbs_req.Url, pbs_req.AccountID, len(pbs_resp.Bids))
}
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)
enc.Encode(pbs_resp)
}
func (deps *auctionDeps) shouldUsersync(ctx context.Context, bidder openrtb_ext.BidderName, gdprApplies string, consent string) bool {
switch gdprApplies {
case "0":
return true
case "1":
if consent == "" {
return false
}
fallthrough
default:
if canSync, err := deps.gdprPerms.HostCookiesAllowed(ctx, consent); !canSync || err != nil {
return false
}
canSync, err := deps.gdprPerms.BidderSyncAllowed(ctx, bidder, consent)
return canSync && err == nil
}
}
// cache video bids only for Web
func cacheVideoOnly(bids pbs.PBSBidSlice, ctx context.Context, w http.ResponseWriter, deps *auctionDeps, labels *pbsmetrics.Labels) {
var cobjs []*pbc.CacheObject
for _, bid := range bids {
if bid.CreativeMediaType == "video" {
cobjs = append(cobjs, &pbc.CacheObject{
Value: bid.Adm,
IsVideo: true,
})
}
}
err := pbc.Put(ctx, cobjs)
if err != nil {
writeAuctionError(w, "Prebid cache failed", err)
labels.RequestStatus = pbsmetrics.RequestStatusErr
return
}
videoIndex := 0
for _, bid := range bids {
if bid.CreativeMediaType == "video" {
bid.CacheID = cobjs[videoIndex].UUID
bid.CacheURL = deps.cfg.GetCachedAssetURL(bid.CacheID)
bid.NURL = ""
bid.Adm = ""
videoIndex++
}
}
}
// checkForValidBidSize goes through list of bids & find those which are banner mediaType and with height or width not defined
// determine the num of ad unit sizes that were used in corresponding bid request
// if num_adunit_sizes == 1, assign the height and/or width to bid's height/width
// if num_adunit_sizes > 1, reject the bid (remove from list) and return an error
// return updated bid list object for next steps in auction
func checkForValidBidSize(bids pbs.PBSBidSlice, bidder *pbs.PBSBidder) pbs.PBSBidSlice {
finalValidBids := make([]*pbs.PBSBid, len(bids))
finalBidCounter := 0
bidLoop:
for _, bid := range bids {
if bid.CreativeMediaType == "banner" && (bid.Height == 0 || bid.Width == 0) {
for _, adunit := range bidder.AdUnits {
if adunit.BidID == bid.BidID && adunit.Code == bid.AdUnitCode {
if len(adunit.Sizes) == 1 {
bid.Width, bid.Height = adunit.Sizes[0].W, adunit.Sizes[0].H
finalValidBids[finalBidCounter] = bid
finalBidCounter = finalBidCounter + 1
} else if len(adunit.Sizes) > 1 {
glog.Warningf("Bid was rejected for bidder %s because no size was defined", bid.BidderCode)
}
continue bidLoop
}
}
} else {
finalValidBids[finalBidCounter] = bid
finalBidCounter = finalBidCounter + 1
}
}
return finalValidBids[:finalBidCounter]
}
// sortBidsAddKeywordsMobile sorts the bids and adds ad server targeting keywords to each bid.
// The bids are sorted by cpm to find the highest bid.
// The ad server targeting keywords are added to all bids, with specific keywords for the highest bid.
func sortBidsAddKeywordsMobile(bids pbs.PBSBidSlice, pbs_req *pbs.PBSRequest, priceGranularitySetting string) {
if priceGranularitySetting == "" {
priceGranularitySetting = defaultPriceGranularity
}
// record bids by ad unit code for sorting
code_bids := make(map[string]pbs.PBSBidSlice, len(bids))
for _, bid := range bids {
code_bids[bid.AdUnitCode] = append(code_bids[bid.AdUnitCode], bid)
}
// loop through ad units to find top bid
for _, unit := range pbs_req.AdUnits {
bar := code_bids[unit.Code]
if len(bar) == 0 {
if glog.V(3) {
glog.Infof("No bids for ad unit '%s'", unit.Code)
}
continue
}
sort.Sort(bar)
// after sorting we need to add the ad targeting keywords
for i, bid := range bar {
// We should eventually check for the error and do something.
roundedCpm, err := exchange.GetCpmStringValue(bid.Price, openrtb_ext.PriceGranularityFromString(priceGranularitySetting))
if err != nil {
glog.Error(err.Error())
}
hbSize := ""
if bid.Width != 0 && bid.Height != 0 {
width := strconv.FormatUint(bid.Width, 10)
height := strconv.FormatUint(bid.Height, 10)
hbSize = width + "x" + height
}
hbPbBidderKey := hbpbConstantKey + "_" + bid.BidderCode
hbBidderBidderKey := hbBidderConstantKey + "_" + bid.BidderCode
hbCacheIdBidderKey := hbCacheIdConstantKey + "_" + bid.BidderCode
hbDealIdBidderKey := hbDealIdConstantKey + "_" + bid.BidderCode
hbSizeBidderKey := hbSizeConstantKey + "_" + bid.BidderCode
if pbs_req.MaxKeyLength != 0 {
hbPbBidderKey = hbPbBidderKey[:min(len(hbPbBidderKey), int(pbs_req.MaxKeyLength))]
hbBidderBidderKey = hbBidderBidderKey[:min(len(hbBidderBidderKey), int(pbs_req.MaxKeyLength))]
hbCacheIdBidderKey = hbCacheIdBidderKey[:min(len(hbCacheIdBidderKey), int(pbs_req.MaxKeyLength))]
hbDealIdBidderKey = hbDealIdBidderKey[:min(len(hbDealIdBidderKey), int(pbs_req.MaxKeyLength))]
hbSizeBidderKey = hbSizeBidderKey[:min(len(hbSizeBidderKey), int(pbs_req.MaxKeyLength))]
}
// fixes #288 where map was being overwritten instead of updated
if bid.AdServerTargeting == nil {
bid.AdServerTargeting = make(map[string]string)
}
pbs_kvs := bid.AdServerTargeting
pbs_kvs[hbPbBidderKey] = roundedCpm
pbs_kvs[hbBidderBidderKey] = bid.BidderCode
pbs_kvs[hbCacheIdBidderKey] = bid.CacheID
if hbSize != "" {
pbs_kvs[hbSizeBidderKey] = hbSize
}
if bid.DealId != "" {
pbs_kvs[hbDealIdBidderKey] = bid.DealId
}
// For the top bid, we want to add the following additional keys
if i == 0 {
pbs_kvs[hbpbConstantKey] = roundedCpm
pbs_kvs[hbBidderConstantKey] = bid.BidderCode
pbs_kvs[hbCacheIdConstantKey] = bid.CacheID
if bid.DealId != "" {
pbs_kvs[hbDealIdConstantKey] = bid.DealId
}
if hbSize != "" {
pbs_kvs[hbSizeConstantKey] = hbSize
}
if bid.BidderCode == "audienceNetwork" {
pbs_kvs[hbCreativeLoadMethodConstantKey] = hbCreativeLoadMethodDemandSDK
} else {
pbs_kvs[hbCreativeLoadMethodConstantKey] = hbCreativeLoadMethodHTML
}
}
}
}
}
// NewJsonDirectoryServer is used to serve .json files from a directory as a single blob. For example,
// given a directory containing the files "a.json" and "b.json", this returns a Handle which serves JSON like:
//
// {
// "a": { ... content from the file a.json ... },
// "b": { ... content from the file b.json ... }
// }
//
// This function stores the file contents in memory, and should not be used on large directories.
// If the root directory, or any of the files in it, cannot be read, then the program will exit.
func NewJsonDirectoryServer(validator openrtb_ext.BidderParamValidator) httprouter.Handle {
// Slurp the files into memory first, since they're small and it minimizes request latency.
files, err := ioutil.ReadDir(schemaDirectory)
if err != nil {
glog.Fatalf("Failed to read directory %s: %v", schemaDirectory, err)
}
data := make(map[string]json.RawMessage, len(files))
for _, file := range files {
bidder := strings.TrimSuffix(file.Name(), ".json")
bidderName, isValid := openrtb_ext.BidderMap[bidder]
if !isValid {
glog.Fatalf("Schema exists for an unknown bidder: %s", bidder)
}
data[bidder] = json.RawMessage(validator.Schema(bidderName))
}
response, err := json.Marshal(data)
if err != nil {
glog.Fatalf("Failed to marshal bidder param JSON-schema: %v", err)
}
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Add("Content-Type", "application/json")
w.Write(response)
}
}
func serveIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.ServeFile(w, r, "static/index.html")
}
type NoCache struct {
handler http.Handler
}
func (m NoCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Add("Pragma", "no-cache")
w.Header().Add("Expires", "0")
m.handler.ServeHTTP(w, r)
}
func loadDataCache(cfg *config.Configuration, db *sql.DB) (err error) {
switch cfg.DataCache.Type {
case "dummy":
dataCache, err = dummycache.New()
if err != nil {
glog.Fatalf("Dummy cache not configured: %s", err.Error())
}
case "postgres":
if db == nil {
return fmt.Errorf("Nil db cannot connect to postgres. Did you forget to set the config.stored_requests.postgres values?")
}
dataCache = postgrescache.New(db, postgrescache.CacheConfig{
Size: cfg.DataCache.CacheSize,
TTL: cfg.DataCache.TTLSeconds,
})
return nil
case "filecache":
dataCache, err = filecache.New(cfg.DataCache.Filename)
if err != nil {
return fmt.Errorf("FileCache Error: %s", err.Error())
}
default:
return fmt.Errorf("Unknown datacache.type: %s", cfg.DataCache.Type)
}
return nil
}
func init() {
rand.Seed(time.Now().UnixNano())
flag.Parse() // read glog settings from cmd line
}
func main() {
v := viper.New()
config.SetupViper(v)
cfg, err := config.New(v)
if err != nil {
glog.Fatalf("Configuration could not be loaded or did not pass validation: %v", err)
}
if err := serve(Rev, cfg); err != nil {
glog.Errorf("prebid-server failed: %v", err)
}
}
func newExchangeMap(cfg *config.Configuration) map[string]adapters.Adapter {
// These keys _must_ coincide with the bidder code in Prebid.js, if the adapter exists in both projects
return map[string]adapters.Adapter{
"appnexus": appnexus.NewAppNexusAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["appnexus"].Endpoint),
"districtm": appnexus.NewAppNexusAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["appnexus"].Endpoint),
"indexExchange": indexExchange.NewIndexAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["indexexchange"].Endpoint),
"pubmatic": pubmatic.NewPubmaticAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["pubmatic"].Endpoint),
"pulsepoint": pulsepoint.NewPulsePointAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["pulsepoint"].Endpoint),
"rubicon": rubicon.NewRubiconAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["rubicon"].Endpoint,
cfg.Adapters["rubicon"].XAPI.Username, cfg.Adapters["rubicon"].XAPI.Password, cfg.Adapters["rubicon"].XAPI.Tracker),
"audienceNetwork": audienceNetwork.NewAdapterFromFacebook(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["facebook"].PlatformID),
"lifestreet": lifestreet.NewLifestreetAdapter(adapters.DefaultHTTPAdapterConfig),
"conversant": conversant.NewConversantAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["conversant"].Endpoint),
"adform": adform.NewAdformAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["adform"].Endpoint),
"sovrn": sovrn.NewSovrnAdapter(adapters.DefaultHTTPAdapterConfig, cfg.Adapters["sovrn"].Endpoint),
}
}
func serve(revision string, cfg *config.Configuration) error {
router := httprouter.New()
theClient := &http.Client{
Transport: &http.Transport{
MaxIdleConns: 400,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 60 * time.Second,
TLSClientConfig: &tls.Config{RootCAs: ssl.GetRootCAPool()},
},
}
fetcher, ampFetcher, db, shutdown := storedRequestsConf.NewStoredRequests(&cfg.StoredRequests, theClient, router)
defer shutdown()
if err := loadDataCache(cfg, db); err != nil {
return fmt.Errorf("Prebid Server could not load data cache: %v", err)
}
pbsAnalytics := analyticsConf.NewPBSAnalytics(&cfg.Analytics)
// Hack because of how legacy handles districtm
bidderList := openrtb_ext.BidderList()
bidderList = append(bidderList, openrtb_ext.BidderName("districtm"))
metricsEngine := metricsConf.NewMetricsEngine(cfg, bidderList)
paramsValidator, err := openrtb_ext.NewBidderParamsValidator(schemaDirectory)
if err != nil {
glog.Fatalf("Failed to create the bidder params validator. %v", err)
}
exchanges = newExchangeMap(cfg)
theExchange := exchange.NewExchange(theClient, pbc.NewClient(&cfg.CacheURL), cfg, metricsEngine)
openrtbEndpoint, err := openrtb2.NewEndpoint(theExchange, paramsValidator, fetcher, cfg, metricsEngine, pbsAnalytics)
if err != nil {
glog.Fatalf("Failed to create the openrtb endpoint handler. %v", err)
}
ampEndpoint, err := openrtb2.NewAmpEndpoint(theExchange, paramsValidator, ampFetcher, cfg, metricsEngine, pbsAnalytics)
if err != nil {
glog.Fatalf("Failed to create the amp endpoint handler. %v", err)
}
syncers := usersyncers.NewSyncerMap(cfg)
gdprPerms := gdpr.NewPermissions(context.Background(), cfg.GDPR, usersyncers.GDPRAwareSyncerIDs(syncers), theClient)
bidderInfos := adapters.ParseBidderInfos("./static/bidder-info", openrtb_ext.BidderList())
router.POST("/auction", (&auctionDeps{cfg, syncers, gdprPerms, metricsEngine}).auction)
router.POST("/openrtb2/auction", openrtbEndpoint)
router.GET("/openrtb2/amp", ampEndpoint)
router.GET("/info/bidders", infoEndpoints.NewBiddersEndpoint())
router.GET("/info/bidders/:bidderName", infoEndpoints.NewBidderDetailsEndpoint(bidderInfos))
router.GET("/bidders/params", NewJsonDirectoryServer(paramsValidator))
router.POST("/cookie_sync", endpoints.NewCookieSyncEndpoint(syncers, &(cfg.HostCookie), gdprPerms, metricsEngine, pbsAnalytics))
router.GET("/status", endpoints.NewStatusEndpoint(cfg.StatusResponse))
router.GET("/", serveIndex)
router.ServeFiles("/static/*filepath", http.Dir("static"))
userSyncDeps := &pbs.UserSyncDeps{
HostCookieConfig: &(cfg.HostCookie),
ExternalUrl: cfg.ExternalURL,
RecaptchaSecret: cfg.RecaptchaSecret,
MetricsEngine: metricsEngine,
PBSAnalytics: pbsAnalytics,
}
router.GET("/setuid", endpoints.NewSetUIDEndpoint(cfg.HostCookie, gdprPerms, pbsAnalytics, metricsEngine))
router.POST("/optout", userSyncDeps.OptOut)
router.GET("/optout", userSyncDeps.OptOut)
pbc.InitPrebidCache(cfg.CacheURL.GetBaseURL())
// Add CORS middleware
c := cors.New(cors.Options{
AllowCredentials: true,
AllowedHeaders: []string{"Origin", "X-Requested-With", "Content-Type", "Accept"}})
corsRouter := c.Handler(router)
// Add no cache headers
noCacheHandler := NoCache{corsRouter}
// Add endpoints to the admin server
// Making sure to add pprof routes
adminRouter := http.NewServeMux()
// Register pprof handlers
adminRouter.HandleFunc("/debug/pprof/", pprof.Index)
adminRouter.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
adminRouter.HandleFunc("/debug/pprof/profile", pprof.Profile)
adminRouter.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
adminRouter.HandleFunc("/debug/pprof/trace", pprof.Trace)
// Register prebid-server defined admin handlers
adminRouter.HandleFunc("/version", endpoints.NewVersionEndpoint(revision))
server.Listen(cfg, noCacheHandler, adminRouter, metricsEngine)
return nil
}