From d43a3cb2eb3c2c67e4bba4f54287b22393ebe4de Mon Sep 17 00:00:00 2001 From: Jonathan P Moraes Date: Thu, 6 Mar 2025 18:17:50 -0300 Subject: [PATCH] Re-organizing code --- web/setupProxy.js | 76 +++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/web/setupProxy.js b/web/setupProxy.js index 728c9056f7..c59193e72c 100644 --- a/web/setupProxy.js +++ b/web/setupProxy.js @@ -73,52 +73,56 @@ router.get('/healthcheck', (req, res) => { app.use(router) // Endpoint to log user info and increment counters -app.post('/api/loguserinfo', (req, res) => { - const { email = '', name, locale, zoneinfo } = req.body +app.post('/api/loguserinfo', async (req, res, next) => { + try { + const { email = '', name, locale, zoneinfo } = req.body - // Guard against invalid email values - if (typeof email !== 'string') { - return res.status(400).json({ error: 'Invalid email format' }) - } + // Guard against invalid email values + if (typeof email !== 'string') { + return res.status(400).json({ error: 'Invalid email format' }) + } - // Calculate the encoded email once - const encodedEmail = Buffer.from(email).toString('base64') + // Calculate the encoded email once + const encodedEmail = Buffer.from(email).toString('base64') - // Skip processing if the email is excluded - if (excludedEmails.has(encodedEmail)) { - return res.sendStatus(200) - } + // Skip processing if the email is excluded + if (excludedEmails.has(encodedEmail)) { + return res.sendStatus(200) + } - // Create userInfo from request data (without circular references) - const userInfo = { - email, - name, - locale, - zoneinfo, - email_verified: true - } + // Create userInfo from request data (without circular references) + const userInfo = { + email, + name, + locale, + zoneinfo, + email_verified: true + } - // Build enriched log data using the helper - const kafkaData = buildLogData(userInfo) + // Build enriched log data using the helper + const kafkaData = buildLogData(userInfo) - // Update metrics - metrics.incrementTotalLogins(email) - metrics.incrementUniqueLogins(email) + // Update metrics + metrics.incrementTotalLogins(email) + metrics.incrementUniqueLogins(email) - // Console log for local debugging - const logData = { - accessLog: { - email: encodedEmail, - dateTime: getFormattedDateTime() + // Console log for local debugging + const logData = { + accessLog: { + email: encodedEmail, + dateTime: getFormattedDateTime() + } } - } - console.log(JSON.stringify(logData)) + console.log(JSON.stringify(logData)) - // Send meta info to Kafka - sendLogToKafka(kafkaData) + // Send meta info to Kafka + sendLogToKafka(kafkaData) - // Response - res.sendStatus(200) + // Response + res.sendStatus(200) + } catch (error) { + next(error) + } }) // **Catch-All Route to Serve index.html for Client-Side Routing**