-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
181 lines (139 loc) · 5.18 KB
/
index.ts
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
/*
Copyright 2022 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// DSP
import express, { Application, Request, Response } from "express"
const { EXTERNAL_PORT, PORT, DSP_HOST, DSP_TOKEN, DSP_DETAIL, SSP_HOST, SHOP_HOST } = process.env
const app: Application = express()
app.use((req, res, next) => {
res.setHeader("Origin-Trial", DSP_TOKEN as string)
next()
})
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
app.use(
express.static("src/public", {
setHeaders: (res: Response, path, stat) => {
const url = new URL(path, `https://${DSP_HOST}`)
if (url.pathname.endsWith("bidding_logic.js")) {
return res.set("X-Allow-FLEDGE", "true")
}
if (url.pathname.endsWith("bidding_signal.json")) {
return res.set("X-Allow-FLEDGE", "true")
}
}
})
)
app.use((req, res, next) => {
// opt-in fencedframe
if (req.get("sec-fetch-dest") === "fencedframe") {
res.setHeader("Supports-Loading-Mode", "fenced-frame")
}
next()
})
app.set("view engine", "ejs")
app.set("views", "src/views")
app.get("/ads", async (req, res) => {
const { advertiser, id } = req.query
console.log("Loading frame content : ", { advertiser, id })
const title = `Your special ads from ${advertiser}`
const move = new URL(`https://${advertiser}:${EXTERNAL_PORT}/items/${id}`)
const creative = new URL(`https://${advertiser}:${EXTERNAL_PORT}/ads/${id}`)
const registerSource = new URL(`https://${SSP_HOST}:${EXTERNAL_PORT}/register-source`)
registerSource.searchParams.append("advertiser", advertiser as string)
registerSource.searchParams.append("id", id as string)
res.render("ads.html.ejs", { title, move, creative, registerSource })
})
app.get("/join-ad-interest-group.html", async (req: Request, res: Response) => {
const title = "Join Ad Interest Group"
res.render("join-ad-interest-group", { title, DSP_TOKEN, DSP_HOST, EXTERNAL_PORT })
})
app.get("/interest-group.json", async (req: Request, res: Response) => {
const { advertiser, id, adType } = req.query
if (advertiser === undefined || id === undefined) {
return res.sendStatus(400)
}
const imageCreative = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}/ads`)
imageCreative.searchParams.append("advertiser", advertiser as string)
imageCreative.searchParams.append("id", id as string)
const videoCreative = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}/html/video-ad-creative.html`)
const renderUrl = adType === "video" ? videoCreative : imageCreative.toString()
const owner = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}`)
const biddingLogicUrl = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}/js/bidding_logic.js`)
const trustedBiddingSignalsUrl = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}/bidding_signal.json`)
const dailyUpdateUrl = new URL(`https://${DSP_HOST}:${EXTERNAL_PORT}/daily_update_url`)
res.json({
name: advertiser,
owner,
// x-allow-fledge: true
biddingLogicUrl,
// x-allow-fledge: true
trustedBiddingSignalsUrl,
trustedBiddingSignalsKeys: ["trustedBiddingSignalsKeys-1", "trustedBiddingSignalsKeys-2"],
// dailyUpdateUrl, // not implemented yet
userBiddingSignals: {
user_bidding_signals: "user_bidding_signals"
},
ads: [
{
renderUrl,
metadata: {
type: advertiser
}
}
]
})
})
app.get("/bidding_signal.json", async (req: Request, res: Response) => {
res.setHeader("X-Allow-FLEDGE", "true")
res.setHeader("X-fledge-bidding-signals-format-version", "2")
res.json({
keys: {
key1: "xxxxxxxx",
key2: "yyyyyyyy"
},
perInterestGroupData: {
name1: {
priorityVector: {
signal1: 100,
signal2: 200
}
}
}
})
})
// TODO: Implement
// app.get("/daily_update_url", async (req: Request, res: Response) => {
// })
app.post("/.well-known/private-aggregation/report-shared-storage", (req, res) => {
console.log( `Received Aggregatable Report on live endpoint`);
let aggregationReport = req.body;
console.log(req.body);
res.sendStatus(200);
})
app.get("/private-aggregation", (req, res) => {
res.render('private-aggregation');
})
app.post("/.well-known/private-aggregation/debug/report-shared-storage", (req, res) => {
let timeStr = new Date().toISOString();
console.log( `Received Aggregatable Report on debug endpoint`);
let aggregationReport = req.body;
console.log(aggregationReport);
res.sendStatus(200);
})
app.get("/", async (req: Request, res: Response) => {
const title = DSP_DETAIL
res.render("index", { title, DSP_HOST, SHOP_HOST, EXTERNAL_PORT })
})
app.listen(PORT, function () {
console.log(`Listening on port ${PORT}`)
})