-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
258 lines (214 loc) · 8.32 KB
/
Copy pathserver.js
File metadata and controls
258 lines (214 loc) · 8.32 KB
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
import express from 'express';
import { createClient } from '@supabase/supabase-js';
const app = express();
app.use(express.json());
// Initialize Supabase client
const supabase = createClient(
'https://pczzwgluhgrjuxjadyaq.supabase.co',
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InBjenp3Z2x1aGdyanV4amFkeWFxIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDAxNjY0MTQsImV4cCI6MjA1NTc0MjQxNH0.dpVupxUEf8be6aMG8jJZFduezZjaveCnUhI9p7G7ud0'
);
// Serve-ad endpoint
app.get('/api/serve-ad/:listingId', async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
console.log('[Express] Handling OPTIONS request');
return res.status(200).end();
}
const listingId = req.params.listingId;
const frame = req.query.frame;
const campaignId = req.query.campaignId;
console.log('[Express] Request URL:', req.url);
console.log('[Express] req.params:', req.params);
console.log('[Express] req.query:', req.query);
console.log('[Express] Listing ID:', listingId, 'Frame:', frame, 'Campaign ID:', campaignId);
if (!frame) {
console.log('[Express] Missing Frame Parameter:', { frame });
res.status(400).send('Missing frame parameter');
return;
}
console.log('[Express] Querying frames table with frame_id:', frame);
const { data: frameData, error: frameError } = await supabase
.from('frames')
.select('frame_id, campaign_id, uploaded_file, price_per_click, size')
.eq('frame_id', frame);
console.log('[Express] Frame Query Result:', { frameData, frameError });
if (frameError) {
console.error('[Express] Frame Query Error:', frameError);
res.status(500).send('Error fetching frame data');
return;
}
if (!frameData || frameData.length === 0) {
console.log('[Express] No frame data found for frame:', frame);
res.status(404).send('Frame not found');
return;
}
const frameRecord = frameData[0];
console.log('[Express] Frame Data:', frameRecord);
const { data: campaign, error: campaignError } = await supabase
.from('campaigns')
.select('id, campaign_details')
.eq('id', frameRecord.campaign_id)
.single();
console.log('[Express] Campaign Query Result:', { campaign, campaignError });
if (campaignError) {
console.error('[Express] Campaign Query Error:', campaignError);
res.status(404).send('Campaign not found');
return;
}
if (!campaign) {
console.log('[Express] No campaign data found for campaign_id:', frameRecord.campaign_id);
res.status(404).send('Campaign not found');
return;
}
console.log('[Express] Campaign Data:', campaign);
const endDate = new Date(
campaign.campaign_details.endDate.year,
campaign.campaign_details.endDate.month - 1,
campaign.campaign_details.endDate.day
);
const today = new Date();
console.log('[Express] End Date:', endDate, 'Today:', today);
if (endDate < today) {
console.log('[Express] Campaign has expired. End Date:', endDate, 'Today:', today);
res.status(400).send('Campaign has expired');
return;
}
let imageUrl;
if (frameRecord.uploaded_file.startsWith('http')) {
imageUrl = frameRecord.uploaded_file;
} else {
imageUrl = `https://pczzwgluhgrjuxjadyaq.supabase.co/storage/v1/object/public/ad-creatives/${frameRecord.uploaded_file}`;
}
const targetUrl = campaign.campaign_details.targetURL || "https://mashdrop.com";
console.log('[Express] Final Image URL:', imageUrl);
console.log('[Express] Target URL:', targetUrl);
// Track impression
try {
const response = await fetch('http://localhost:5173/api/track-impression', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ frame: frame, campaignId: campaignId || frameRecord.campaign_id })
});
console.log('[Express] Impression tracked:', await response.json());
} catch (error) {
console.error('[Express] Error tracking impression:', error.message);
}
res.status(200).setHeader('Content-Type', 'text/html').send(`
<!DOCTYPE html>
<html>
<head>
<title>Ad</title>
<script>
let isClicking = false;
document.addEventListener('click', function(e) {
if (e.target.tagName === 'IMG' && !isClicking) {
isClicking = true;
e.preventDefault();
console.log('Click event triggered for ad');
fetch('http://localhost:5173/api/track-click', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ frame: '${frame}', campaignId: '${campaignId || frameRecord.campaign_id}' })
}).then(response => {
console.log('Click tracked:', response);
return response.json();
}).then(data => {
console.log('Click response:', data);
window.open('${targetUrl}', '_blank');
isClicking = false;
}).catch(error => {
console.error('Error tracking click:', error);
window.open('${targetUrl}', '_blank');
isClicking = false;
});
}
});
</script>
</head>
<body>
<img src="${imageUrl}" style="border:none; max-width: 100%; max-height: 100%;" alt="Ad for Frame ${frame}" />
</body>
</html>
`);
});
// Track-impression endpoint
app.post('/api/track-impression', async (req, res) => {
console.log('[Express] Track Impression Request:', req.body);
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const { frame, campaignId } = req.body;
if (!frame || !campaignId) {
return res.status(400).json({ error: 'Missing frame or campaignId' });
}
try {
const { data: campaign, error: fetchError } = await supabase
.from('campaigns')
.select('impressions')
.eq('id', campaignId)
.single();
if (fetchError) {
console.error('[Express] Error fetching campaign:', fetchError);
return res.status(500).json({ error: 'Error fetching campaign' });
}
if (!campaign) {
return res.status(404).json({ error: 'Campaign not found' });
}
const newImpressions = (campaign.impressions || 0) + 1;
const { error: updateError } = await supabase
.from('campaigns')
.update({ impressions: newImpressions })
.eq('id', campaignId);
if (updateError) {
console.error('[Express] Error updating impressions:', updateError);
return res.status(500).json({ error: 'Error updating impressions' });
}
return res.status(200).json({ success: true, impressions: newImpressions });
} catch (error) {
console.error('[Express] Error in track-impression:', error);
return res.status(500).json({ error: 'Internal server error' });
}
});
// Track-click endpoint
app.post('/api/track-click', async (req, res) => {
console.log('[Express] Track Click Request:', req.body);
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const { frame, campaignId } = req.body;
if (!frame || !campaignId) {
return res.status(400).json({ error: 'Missing frame or campaignId' });
}
try {
const { data: campaign, error: fetchError } = await supabase
.from('campaigns')
.select('clicks')
.eq('id', campaignId)
.single();
if (fetchError) {
console.error('[Express] Error fetching campaign:', fetchError);
return res.status(500).json({ error: 'Error fetching campaign' });
}
if (!campaign) {
return res.status(404).json({ error: 'Campaign not found' });
}
const newClicks = (campaign.clicks || 0) + 1;
const { error: updateError } = await supabase
.from('campaigns')
.update({ clicks: newClicks })
.eq('id', campaignId);
if (updateError) {
console.error('[Express] Error updating clicks:', updateError);
return res.status(500).json({ error: 'Error updating clicks' });
}
return res.status(200).json({ success: true, clicks: newClicks });
} catch (error) {
console.error('[Express] Error in track-click:', error);
return res.status(500).json({ error: 'Internal server error' });
}
});
app.listen(5173, () => {
console.log('Local API server running on http://localhost:5173');
});