Skip to content

Commit

Permalink
Re-organizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpmoraes committed Mar 6, 2025
1 parent e5e6b2d commit d43a3cb
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions web/setupProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down

0 comments on commit d43a3cb

Please sign in to comment.