Skip to content

Commit

Permalink
Merge branch 'prebid:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
AdviewOpen authored Jan 22, 2024
2 parents f2697d8 + 18aa538 commit 41dc731
Show file tree
Hide file tree
Showing 54 changed files with 2,114 additions and 599 deletions.
118 changes: 73 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,106 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/prebid/prebid-server?style=flat-square)](https://goreportcard.com/report/github.com/prebid/prebid-server)
![Go Version](https://img.shields.io/github/go-mod/go-version/prebid/prebid-server?style=flat-square)

# Prebid Server
<br />
<br />
<p align="center"><img alt="Prebid Server Logo" src="/static/pbs-logo.svg" style="width:80%; max-width:600px;"></p>
<br />

Prebid Server is an open source implementation of Server-Side Header Bidding.
It is managed by [Prebid.org](https://prebid.org/about/),
and upholds the principles from the [Prebid Code of Conduct](https://prebid.org/code-of-conduct/).
<a href="https://prebid.org/product-suite/prebid-server/">Prebid Server</a> is an open-source solution for running real-time advertising auctions in the cloud. This project is part of the <a href="https://prebid.org/">Prebid</a> ecosystem, seamlessly integrating with <a href="https://prebid.org/product-suite/prebidjs/">Prebid.js</a> and the <a href="https://prebid.org/product-suite/prebid-mobile/">Prebid Mobile SDKs</a> to deliver world-class header bidding for any ad format and for any type of digital media.

This project does not support the same set of Bidders as Prebid.js, although there is overlap.
The current set can be found in the [adapters](./adapters) package. If you don't see the one you want, feel free to [contribute it](https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html).
## Getting Started
- [What is Prebid Server](https://docs.prebid.org/prebid-server/overview/prebid-server-overview.html)
- [Intro to Header Bidding](https://docs.prebid.org/overview/intro-to-header-bidding.html)
- [Header Bidding with Prebid](https://docs.prebid.org/overview/intro.html#header-bidding-with-prebid)
- [API Endpoints](https://docs.prebid.org/prebid-server/endpoints/pbs-endpoint-overview.html)

## Configuring

For more information, see:
When hosting Prebid Server or developing locally, **you must set a default GDPR value**. This configuration determines whether GDPR is enabled when no regulatory signal is available in the request, where a value of `"0"` disables it by default and a value of `"1"` enables it. This is required as there is no consensus on a good default.

- [What is Prebid?](https://docs.prebid.org/overview/intro.html)
- [Prebid Server Overview](https://docs.prebid.org/prebid-server/overview/prebid-server-overview.html)
- [Current Bidders](https://docs.prebid.org/dev-docs/pbs-bidders.html)
Refer to the [configuration guide](docs/developers/configuration.md) for additional information and a list of available configuration options.

Please consider [registering your Prebid Server](https://docs.prebid.org/prebid-server/hosting/pbs-hosting.html#optional-registration) to get on the mailing list for updates, etc.
## Hosting Prebid Server
> [!NOTE]
> Please consider [registering as a Prebid Server host](https://docs.prebid.org/prebid-server/hosting/pbs-hosting.html#optional-registration) to join the mailing list for updates and feedback.
## Installation
The quickest way to host Prebid Server is to deploy our [official Docker image](https://hub.docker.com/r/prebid/prebid-server). If you're hosting the container with Kubernetes, you can configure Prebid Server with environment variables [using a pod file](https://kubernetes.io/docs/tasks/inject-data-application/define-interdependent-environment-variables/) or [using a config map](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables). Alternatively, you can use a configuration file [embedded in a config map](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#populate-a-volume-with-data-stored-in-a-configmap) which Prebid Server will read from at the path `/etc/config`.

First install [Go](https://golang.org/doc/install) version 1.19 or newer.
For deploying a fork, you can create a custom Docker container using the command:
``` bash
docker build -t prebid-server .
```
or compile a standalone binary using the command:
``` bash
go build .
```
Ensure that you deploy the `/static` directory, as Prebid Server requires those files at startup.

Note that prebid-server is using [Go modules](https://blog.golang.org/using-go-modules).
We officially support the most recent two major versions of the Go runtime. However, if you'd like to use a version <1.13 and are inside GOPATH `GO111MODULE` needs to be set to `GO111MODULE=on`.
## Developing

Download and prepare Prebid Server:
Prebid Server requires [Go](https://go.dev) version 1.19 or newer. You can develop on any operating system that Go supports; however, please note that our helper scripts are written in bash.

```bash
cd YOUR_DIRECTORY
git clone https://github.com/prebid/prebid-server src/github.com/prebid/prebid-server
cd src/github.com/prebid/prebid-server
1. Clone The Repository
``` bash
git clone git@github.com:prebid/prebid-server.git
cd prebid-server
```

Run the automated tests:
3. Download Dependencies
``` bash
go mod download
```

3. Verify Tests Pass
```bash
./validate.sh
```

Or just run the server locally:

4. Run The Server
```bash
go build .
./prebid-server
go run .
```

Run format:
```
make format
```
or
```bash
./scripts/format.sh -f true
```
By default, Prebid Server will attach to port 8000. To confirm the server is running, visit `http://localhost:8000/` in your web browser.

### Code Style
To maintain consistency in the project's code, please:

- Follow the recommendations set by [Effective Go](https://go.dev/doc/effective_go). This article provides a comprehensive guide on how to write idiomatic Go code, covering topics such as naming and formatting. Many IDEs will automatically format your code upon save. If you need to manaully format your code, either run the bash script or execute the make step:
```
./scripts/format.sh -f true
```
```
make format
```

- Prefer small functions with descriptive names instead of complex functions with comments. This approach helps make the code more readable, maintainable, and testable.

- Do not discard errors. You should implement appropriate error handling, such as gracefully falling back to a default behavior or bubbling up an error.

### IDE Recommendation

Load the landing page in your browser at `http://localhost:8000/`.
For the full API reference, see [the endpoint documentation](https://docs.prebid.org/prebid-server/endpoints/pbs-endpoint-overview.html)
An option for developing Prebid Server in a reproducible environment isolated from your host OS is using Visual Studio Code with [Remote Container Setup](devcontainer.md). This is a recommendation, not a requirement. This approach is especially useful if you are developing on Windows, since the Remote Container runs within WSL providing you with the capability to execute bash scripts.

## Go Modules
## Importing Prebid Server

The packages within this repository are intended to be used as part of the Prebid Server compiled binary. If you
choose to import Prebid Server packages in other projects, please understand we make no promises on the stability
of exported types.
Prebid Server is not currently intended to be imported by other projects. Go Modules is used to manage dependencies, which also makes it possible to import Prebid Server packages. This is not supported. We offer no guarantees regarding the stability of packages and do not adhere to semantic versioning guidelines.

## Contributing
> [!IMPORTANT]
> All contributions must follow the [Prebid Code of Conduct](https://prebid.org/code-of-conduct/) and the [Prebid Module Rules](https://docs.prebid.org/dev-docs/module-rules.html).
Want to [add an adapter](https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html)? Found a bug? Great!
### Bid Adapter
Bid Adapters transform OpenRTB requests and responses for communicating with a bidding server. This may be as simple as a passthrough or as complex as mapping to a custom data model. We invite you to contribute an adapter for your company. Consult our guide on [building a bid adapter](https://docs.prebid.org/prebid-server/developers/add-new-bidder-go.html) for more information.

Report bugs, request features, and suggest improvements [on Github](https://github.com/prebid/prebid-server/issues).
### Analytics Module
Analytics Modules enable business intelligence tools to collect data from Prebid Server to provide publishers and hosts with valuable insights into their header bidding traffic. We welcome you to contribute a module for your platform. Refer to our guide on [building an analytics module](https://docs.prebid.org/prebid-server/developers/pbs-build-an-analytics-adapter.html) for further information.

Or better yet, [open a pull request](https://github.com/prebid/prebid-server/compare) with the changes you'd like to see.
### Auction Module
Auction Modules allow hosts to extend the behavior of Prebid Server at specfic spots in the auction pipeline using existing modules or by developing custom functionality. Auction Modules may provide creative validation, traffic optimization, and real time data services amoung many other potential uses. We welcome vendors and community members to contribute modules that publishers and hosts may find useful. Consult our guide on [building an auction module](https://docs.prebid.org/prebid-server/developers/add-a-module.html) for more information.

## IDE Recommendations
### Feature
We welcome everyone to contribute to this project by implementing a specification or by proposing a new feature. Please review the [prioritized project board](https://github.com/orgs/prebid/projects/4), where you can select an issue labeled "Ready For Dev". To avoid redundant effort, kindly leave a comment on the issue stating your intention to take it on. To propose a feature, [open a new issue](https://github.com/prebid/prebid-server/issues/new/choose) with as much detail as possible for consideration by the Prebid Server Committee.

The quickest way to start developing Prebid Server in a reproducible environment isolated from your host OS
is by using Visual Studio Code with [Remote Container Setup](devcontainer.md).
### Bug Fix
Bug reports may be submitted by [opening a new issue](https://github.com/prebid/prebid-server/issues/new/choose) and describing the error in detail with the steps to reproduce and example data. A member of the core development team will validate the bug and discuss next steps. You're encouraged to open an exploratory draft pull request to either demonstrate the bug by adding a test or offering a potential fix.
131 changes: 92 additions & 39 deletions adapters/adkernel/adkernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"text/template"

"github.com/prebid/openrtb/v19/openrtb2"
Expand All @@ -15,6 +16,14 @@ import (
"github.com/prebid/prebid-server/v2/openrtb_ext"
)

const (
mf_suffix = "__mf"
mf_suffix_banner = "b" + mf_suffix
mf_suffix_video = "v" + mf_suffix
mf_suffix_audio = "a" + mf_suffix
mf_suffix_native = "n" + mf_suffix
)

type adkernelAdapter struct {
EndpointTemplate *template.Template
}
Expand Down Expand Up @@ -78,9 +87,6 @@ func validateImpression(imp *openrtb2.Imp, impExt *openrtb_ext.ExtImpAdkernel) e
if impExt.ZoneId < 1 {
return newBadInputError(fmt.Sprintf("Invalid zoneId value: %d. Ignoring imp id=%s", impExt.ZoneId, imp.ID))
}
if imp.Video == nil && imp.Banner == nil && imp.Native == nil {
return newBadInputError(fmt.Sprintf("Invalid imp id=%s. Expected imp.banner / imp.video / imp.native", imp.ID))
}
return nil
}

Expand All @@ -90,54 +96,75 @@ func dispatchImpressions(imps []openrtb2.Imp, impsExt []openrtb_ext.ExtImpAdkern
errors := make([]error, 0)
for idx := range imps {
imp := imps[idx]
err := compatImpression(&imp)
if err != nil {
errors = append(errors, err)
continue
}
imp.Ext = nil
impExt := impsExt[idx]
if res[impExt] == nil {
res[impExt] = make([]openrtb2.Imp, 0)
}
res[impExt] = append(res[impExt], imp)
if isMultiFormatImp(&imp) {
splImps := splitMultiFormatImp(&imp)
res[impExt] = append(res[impExt], splImps...)
} else {
res[impExt] = append(res[impExt], imp)
}
}
return res, errors
}

// Alter impression info to comply with adkernel platform requirements
func compatImpression(imp *openrtb2.Imp) error {
imp.Ext = nil //do not forward ext to adkernel platform
if imp.Banner != nil {
return compatBannerImpression(imp)
}
func isMultiFormatImp(imp *openrtb2.Imp) bool {
count := 0
if imp.Video != nil {
return compatVideoImpression(imp)
count++
}
if imp.Audio != nil {
count++
}
if imp.Banner != nil {
count++
}
if imp.Native != nil {
return compatNativeImpression(imp)
count++
}
return newBadInputError("Invalid impression")
return count > 1
}

func compatBannerImpression(imp *openrtb2.Imp) error {
imp.Audio = nil
imp.Video = nil
imp.Native = nil
return nil
}
func splitMultiFormatImp(imp *openrtb2.Imp) []openrtb2.Imp {
splitImps := make([]openrtb2.Imp, 0, 4)
if imp.Banner != nil {
impCopy := *imp
impCopy.Video = nil
impCopy.Native = nil
impCopy.Audio = nil
impCopy.ID += mf_suffix_banner
splitImps = append(splitImps, impCopy)
}
if imp.Video != nil {
impCopy := *imp
impCopy.Banner = nil
impCopy.Native = nil
impCopy.Audio = nil
impCopy.ID += mf_suffix_video
splitImps = append(splitImps, impCopy)
}

func compatVideoImpression(imp *openrtb2.Imp) error {
imp.Banner = nil
imp.Audio = nil
imp.Native = nil
return nil
}
if imp.Native != nil {
impCopy := *imp
impCopy.Banner = nil
impCopy.Video = nil
impCopy.Audio = nil
impCopy.ID += mf_suffix_native
splitImps = append(splitImps, impCopy)
}

func compatNativeImpression(imp *openrtb2.Imp) error {
imp.Banner = nil
imp.Audio = nil
imp.Video = nil
return nil
if imp.Audio != nil {
impCopy := *imp
impCopy.Banner = nil
impCopy.Video = nil
impCopy.Native = nil
impCopy.ID += mf_suffix_audio
splitImps = append(splitImps, impCopy)
}
return splitImps
}

func getImpressionExt(imp *openrtb2.Imp) (*openrtb_ext.ExtImpAdkernel, error) {
Expand Down Expand Up @@ -232,19 +259,45 @@ func (adapter *adkernelAdapter) MakeBids(internalRequest *openrtb2.BidRequest, e
bidResponse.Currency = bidResp.Cur
for i := 0; i < len(seatBid.Bid); i++ {
bid := seatBid.Bid[i]
origImpId := bid.ImpID
typeSuffix := ""
if strings.HasSuffix(origImpId, mf_suffix) {
sfxStart := len(origImpId) - len(mf_suffix) - 1
typeSuffix = origImpId[sfxStart:]
bid.ImpID = origImpId[:sfxStart]
}
bidResponse.Bids = append(bidResponse.Bids, &adapters.TypedBid{
Bid: &bid,
BidType: getMediaTypeForImpID(bid.ImpID, internalRequest.Imp),
BidType: getMediaTypeForImpID(bid.ImpID, typeSuffix, internalRequest.Imp),
})
}
return bidResponse, nil
}

// getMediaTypeForImp figures out which media type this bid is for
func getMediaTypeForImpID(impID string, imps []openrtb2.Imp) openrtb_ext.BidType {
for _, imp := range imps {
if imp.ID == impID && imp.Banner != nil {
func getMediaTypeForImpID(impID string, typeSuffix string, imps []openrtb2.Imp) openrtb_ext.BidType {
if len(typeSuffix) > 0 {
if typeSuffix == mf_suffix_banner {
return openrtb_ext.BidTypeBanner
} else if typeSuffix == mf_suffix_video {
return openrtb_ext.BidTypeVideo
} else if typeSuffix == mf_suffix_audio {
return openrtb_ext.BidTypeAudio
} else if typeSuffix == mf_suffix_native {
return openrtb_ext.BidTypeNative
}
}
for _, imp := range imps {
if imp.ID == impID {
if imp.Banner != nil {
return openrtb_ext.BidTypeBanner
} else if imp.Video != nil {
return openrtb_ext.BidTypeVideo
} else if imp.Audio != nil {
return openrtb_ext.BidTypeAudio
} else if imp.Native != nil {
return openrtb_ext.BidTypeNative
}
}
}
return openrtb_ext.BidTypeVideo
Expand Down
2 changes: 1 addition & 1 deletion adapters/adkernel/adkernel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestJsonSamples(t *testing.T) {
bidder, buildErr := Builder(openrtb_ext.BidderAdkernel, config.Adapter{
Endpoint: "https://pbs.adksrv.com/hb?zone={{.ZoneID}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})
Endpoint: "http://pbs.adksrv.com/hb?zone={{.ZoneID}}"}, config.Server{ExternalUrl: "http://hosturl.com", GvlID: 1, DataCenter: "2"})

if buildErr != nil {
t.Fatalf("Builder returned unexpected error %v", buildErr)
Expand Down
Loading

0 comments on commit 41dc731

Please sign in to comment.