-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
301 lines (275 loc) · 9.82 KB
/
server.js
File metadata and controls
301 lines (275 loc) · 9.82 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
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
const express = require("express");
const cors = require("cors");
const fetch = require("cross-fetch");
require("dotenv").config({ path: ".env.local" });
const app = express();
const PORT = process.env.PORT || 3001;
// CORS configuration
app.use(
cors({
origin: function (origin, callback) {
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
const allowedOrigins = [
// Production domain
"https://vertonet.vercel.app",
// Development
"http://localhost:3000",
// Allow any Vercel preview deployment (pattern matching)
/^https:\/\/vertonet.*\.vercel\.app$/,
/^https:\/\/.*--vertonet.*\.vercel\.app$/,
];
// Check if origin matches any allowed pattern
const isAllowed = allowedOrigins.some((allowedOrigin) => {
if (typeof allowedOrigin === "string") {
return origin === allowedOrigin;
} else if (allowedOrigin instanceof RegExp) {
return allowedOrigin.test(origin);
}
return false;
});
if (isAllowed) {
callback(null, true);
} else {
console.log("CORS blocked origin:", origin);
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
})
);
app.use(express.json());
// PredinctHQ API proxy endpoint
app.get("/api/events", async (req, res) => {
console.log("Received request for events:", req.query);
try {
const { limit = 6, language = "en" } = req.query;
const apiKey = process.env.PREDINCTHQ_API_KEY;
if (!apiKey) {
console.warn("PredinctHQ API key not found, using fallback events");
return res.json({ events: getFallbackEvents(language, limit) });
}
try {
// Use REST API directly
const params = new URLSearchParams({
country: "IS", // ISO country code for Iceland
"start.gte": new Date().toISOString().split(".")[0] + "Z", // From today, without milliseconds
limit: limit.toString(),
category: "conferences,expos",
sort: "start", // Sort by start date
});
const response = await fetch(
`https://api.predicthq.com/v1/events?${params.toString()}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: "application/json",
},
timeout: 30000, // 30 second timeout
}
);
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`HTTP error! status: ${response.status}, body: ${errorText}`
);
}
const data = await response.json();
if (data.results && data.results.length > 0) {
console.log("PredictHQ API returned", data.results.length, "events");
const formattedEvents = data.results.slice(0, limit).map((event) => {
const formatted = {
id: event.id,
title: event.title,
location: getEventLocation(event),
date: formatEventDate(event.start, language),
image: event.image || null,
url: event.url || null,
description: event.description || null,
};
console.log(
"Formatted event:",
formatted.title,
"URL:",
formatted.url,
"Description:",
formatted.description?.substring(0, 100) + "..."
);
return formatted;
});
return res.json({ events: formattedEvents });
}
return res.json({ events: getFallbackEvents(language, limit) });
} catch (error) {
console.error("Error using PredictHQ REST API:", error);
return res.json({ events: getFallbackEvents(language, limit) });
}
} catch (error) {
console.error(
"Error fetching events from PredinctHQ API, using fallback events:",
error
);
const { language = "en", limit = 6 } = req.query;
return res.json({ events: getFallbackEvents(language, limit) });
}
});
// Fallback events function
function getFallbackEvents(language = "en", limit = 6) {
const events =
language === "is"
? [
{
id: "fallback-1",
title: "Fyrirtækjaheimsókn til Gangverk",
location: "Gangverk, Ármúli 6, 108 Reykjavík",
date: formatEventDate("2025-01-15T18:00:00", language),
image: "/resources/images/vidburdir1.png",
description:
"Mánaðarlegur tæknifundur fyrir forritara og tæknifólk",
},
{
id: "fallback-2",
title: "Fyrirtækjaheimsókn til Advania",
location: "Advania, Guðrúnartún 10, 105 Reykjavík",
date: formatEventDate("2025-02-20T17:00:00", language),
image: "/resources/images/vidburdir2.png",
description: "Árlegur ráðstefna um konur í tækni",
},
{
id: "fallback-3",
title: "Fyrirtækjaheimsókn til Kolibri",
location: "Kolibri, Borgartún 26, Reykjavík",
date: formatEventDate("2025-03-18T17:00:00", language),
image: "/resources/images/vidburdir3.png",
description: "54 klukkustunda startup keppni og netvæðing",
},
{
id: "fallback-4",
title: "Fyrirtækjaheimsókn til Aranja",
location: "Aranja, Nóatún 17, 105 Reykjavík",
date: formatEventDate("2025-04-12T14:00:00", language),
image: "/resources/images/vidburdir4.png",
description: "Hands-on verkefni um gervigreind og vélanám",
},
{
id: "fallback-5",
title: "Fyrirtækjaheimsókn til Overcast",
location: "Overcast, Höfðabakka 9D, 110 Reykjavík",
date: formatEventDate("2025-05-10T17:00:00", language),
image: "/resources/images/vidburdir5.png",
description: "Summit um stafræna umbreytingu og nýsköpun",
},
{
id: "fallback-6",
title: "Tæknifundur Reykjavík",
location: "Innovation House, Grandagarður 1, Reykjavík",
date: formatEventDate("2025-06-15T18:00:00", language),
image: "/resources/images/vidburdir6.png",
description: "Ráðstefna um öryggi á netinu og bestu venjur",
},
]
: [
{
id: "fallback-1",
title: "Company Visit to Gangverk",
location: "Gangverk, Ármúli 6, 108 Reykjavík",
date: formatEventDate("2025-01-15T18:00:00", language),
image: "/resources/images/vidburdir1.png",
description:
"Monthly tech meetup for developers and tech enthusiasts",
},
{
id: "fallback-2",
title: "Company Visit to Advania",
location: "Advania, Guðrúnartún 10, 105 Reykjavík",
date: formatEventDate("2025-02-20T17:00:00", language),
image: "/resources/images/vidburdir2.png",
description: "Annual conference celebrating women in technology",
},
{
id: "fallback-3",
title: "Company Visit to Kolibri",
location: "Kolibri, Borgartún 26, Reykjavík",
date: formatEventDate("2025-03-18T17:00:00", language),
image: "/resources/images/vidburdir3.png",
description: "54-hour startup competition and networking event",
},
{
id: "fallback-4",
title: "Company Visit to Aranja",
location: "Aranja, Nóatún 17, 105 Reykjavík",
date: formatEventDate("2025-04-12T14:00:00", language),
image: "/resources/images/vidburdir4.png",
description: "Hands-on workshop on AI and ML technologies",
},
{
id: "fallback-5",
title: "Company Visit to Overcast",
location: "Overcast, Höfðabakka 9D, 110 Reykjavík",
date: formatEventDate("2025-05-10T17:00:00", language),
image: "/resources/images/vidburdir5.png",
description:
"Summit focusing on digital transformation and innovation",
},
{
id: "fallback-6",
title: "Tech Meetup Reykjavík",
location: "Innovation House, Grandagarður 1, Reykjavík",
date: formatEventDate("2025-06-15T18:00:00", language),
image: "/resources/images/vidburdir6.png",
description:
"Conference on cybersecurity best practices and trends",
},
];
return events.slice(0, limit);
}
// Helper functions
function formatEventDate(dateString, language = "en") {
try {
const date = new Date(dateString);
if (language === "is") {
return date.toLocaleDateString("is-IS", {
day: "numeric",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
});
} else {
return date.toLocaleDateString("en-US", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
});
}
} catch (error) {
console.error("Error formatting date:", error);
return dateString;
}
}
function getEventLocation(event) {
const parts = [];
if (event.venue_name) {
parts.push(event.venue_name);
}
if (event.venue_address) {
parts.push(event.venue_address);
}
if (event.city_name) {
parts.push(event.city_name);
}
if (event.country_name && event.country_name !== "Iceland") {
parts.push(event.country_name);
}
return parts.length > 0 ? parts.join(", ") : "Location TBA";
}
app.listen(PORT, () => {
console.log(`Express server running on port ${PORT}`);
console.log(
`Test the API: http://localhost:${PORT}/api/events?limit=1&language=en`
);
});