Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,648 Bytes
75f825b cd5701b 75f825b cd5701b 75f825b cd5701b 75f825b 34d2b84 75f825b |
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 |
// src/routes/api/bandwidth/+server.ts
import { json } from '@sveltejs/kit';
import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb';
import { randomUUID } from 'crypto';
const REGION = process.env.AWS_REGION || 'us-east-1'; // Fallback region if none is set
const TABLE_NAME = process.env.DYNAMODB_TABLE;
if (!TABLE_NAME) {
console.error(
'DYNAMODB_TABLE environment variable is not set. Please set DYNAMODB_TABLE to your DynamoDB table name.'
);
}
const ddbClient = new DynamoDBClient({ region: REGION });
export async function POST({ request }) {
try {
const { bandwidth, latency, location, progress } = await request.json();
if (
typeof bandwidth !== 'number' ||
typeof latency !== 'number' ||
typeof location !== 'string' ||
typeof progress !== 'number'
) {
return json({ error: 'Invalid data' }, { status: 400 });
}
if (!TABLE_NAME) {
return json({ error: 'failed to save measurements' }, { status: 500 });
}
const item = {
id: { S: randomUUID() }, // primary key
bandwidth: { N: bandwidth.toString() },
latency: { N: latency.toString() },
location: { S: location },
timestamp: { S: new Date().toISOString() },
progress: { N: progress.toString() }
};
const putParams = {
TableName: TABLE_NAME,
Item: item
};
await ddbClient.send(new PutItemCommand(putParams));
//console.log('Received bandwidth data:', { bandwidth, latency, location, progress });
return json({ success: true });
} catch (error) {
console.error('Error processing bandwidth data:', error);
return json({ error: 'Unable to process the request' }, { status: 500 });
}
}
|