Skip to content

Commit

Permalink
Removing redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpmoraes committed Mar 6, 2025
1 parent 6ed3635 commit e5e6b2d
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions web/setupProxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const express = require('express')
const { createProxyMiddleware } = require('http-proxy-middleware')
const path = require('path')
Expand All @@ -9,8 +8,8 @@ const { excludedEmails } = require('./services/helpers/excludedEmails')
const { connectProducer } = require('./services/kafkaProducer')
const { buildLogData } = require('./services/helpers/logFormatter')

const app = express();
const router = express.Router();
const app = express()
const router = express.Router()
const distPath = path.join(__dirname, 'dist')

// Initialize Metrics
Expand All @@ -26,6 +25,7 @@ app.get('/metrics', async (req, res) => {
}
})

// Connect Kafka producer
(async () => {
try {
await connectProducer()
Expand Down Expand Up @@ -70,48 +70,33 @@ app.use('/api/v2beta', createProxyMiddleware(apiOptions))
router.get('/healthcheck', (req, res) => {
res.send('OK')
})

app.use(router)

// **Catch-All Route to Serve index.html for Client-Side Routing**
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'))
})

app.listen(port, () => {
console.log(`App listening on port ${port}!`)
})

// Endpoint to log user info and increment counters
app.post('/api/loguserinfo', (req, res) => {
const {
email = '',
name,
locale,
zoneinfo
} = req.body
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' })
}

// 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)
Expand All @@ -120,24 +105,30 @@ app.post('/api/loguserinfo', (req, res) => {
metrics.incrementTotalLogins(email)
metrics.incrementUniqueLogins(email)

if (excludedEmails.has(encodedEmail)) {
return; // skip everything for excluded emails
}
// Console log for local debugging
const logData = {
accessLog: {
email: encodedEmail,
dateTime: getFormattedDateTime()
}
};
}
console.log(JSON.stringify(logData))

// Send meta info to Kafka
sendLogToKafka(kafkaData)

// Response
res.sendStatus(200)
});
})

module.exports = app
// **Catch-All Route to Serve index.html for Client-Side Routing**
app.get('*', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'))
})

// Start the server (only one call to app.listen)
app.listen(port, () => {
console.log(`App listening on port ${port}!`)
})

module.exports = app

0 comments on commit e5e6b2d

Please sign in to comment.