fixed analytics request

This commit is contained in:
Jonas_Jones 2024-12-23 03:41:14 +01:00
parent 8a52ed518e
commit 53e9dd02fc

View file

@ -50,8 +50,12 @@ export default {
const record = { timestamp, domain, method, path, country }; const record = { timestamp, domain, method, path, country };
// Store the new record in the database try {
await this.storeRecordInDB(record, env); // Store the new record in the database
await this.storeRecordInDB(record, env);
} catch (error) {
return new Response('Error storing record in DB', { status: 500 });
}
return new Response('Recorded', { status: 200 }); return new Response('Recorded', { status: 200 });
} catch (error) { } catch (error) {
@ -80,8 +84,12 @@ export default {
const record = { timestamp, domain, method, path, country }; const record = { timestamp, domain, method, path, country };
// Store the new record in the database try {
await this.storeRecordInDB(record, env); // Store the new record in the database
await this.storeRecordInDB(record, env);
} catch (error) {
return new Response('Error storing record in DB', { status: 500 });
}
const origin = request.headers.get('Origin'); const origin = request.headers.get('Origin');
const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost'); const isAllowedOrigin = ALLOWED_ORIGINS.includes(origin) || ALLOWED_ORIGINS.includes('localhost');
@ -151,13 +159,9 @@ export default {
// Store the new record in the database // Store the new record in the database
async storeRecordInDB(record, env) { async storeRecordInDB(record, env) {
try {
await env.DB.prepare('INSERT INTO requests (timestamp, domain, method, path, country) VALUES (?, ?, ?, ?, ?)') await env.DB.prepare('INSERT INTO requests (timestamp, domain, method, path, country) VALUES (?, ?, ?, ?, ?)')
.bind(record.timestamp, record.domain, record.method, record.path, record.country) .bind(record.timestamp, record.domain, record.method, record.path, record.country)
.run(); .run();
} catch (error) {
console.error('Error storing record in DB:', error);
}
}, },
// Retrieve filtered records from the database // Retrieve filtered records from the database