From 79947c31a8a9ba8343837b7f499365d2a4b21669 Mon Sep 17 00:00:00 2001 From: Justin Horner Date: Mon, 12 Aug 2024 17:03:11 -0400 Subject: [PATCH 1/4] fix param names was getting 422 errors when passing up the defined param names (i.e. 'windspeedmph'), because this line of code had some incorrect param names --- send-cwop-rest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-cwop-rest.js b/send-cwop-rest.js index 5adb65c..425fd9e 100644 --- a/send-cwop-rest.js +++ b/send-cwop-rest.js @@ -47,7 +47,7 @@ export async function handleRequest(request) { if (body.packet) { // default to provided packet, if any packet = body.packet + '—viacwop.rest'; - } else if (body.time && body.id && body.lat && body.long && body.tempf && body.windspeed && body.windgust && body.winddir) { // otherwise, check for required params to build our own + } else if (body.time && body.id && body.lat && body.long && body.tempf && body.windspeedmph && body.windgustmph && body.winddir) { // otherwise, check for required params to build our own packet = buildPacket(body); } else { return new Response('Missing required packet or readings parameters in payload', { "status": 422 }); From 0d7b97beb44137814402f9495ce3e3a0f9e2f579 Mon Sep 17 00:00:00 2001 From: Justin Horner Date: Mon, 12 Aug 2024 17:06:30 -0400 Subject: [PATCH 2/4] fix for 0 values use a null check, to support sending 0 values where possible --- send-cwop-rest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-cwop-rest.js b/send-cwop-rest.js index 425fd9e..e85298f 100644 --- a/send-cwop-rest.js +++ b/send-cwop-rest.js @@ -47,7 +47,7 @@ export async function handleRequest(request) { if (body.packet) { // default to provided packet, if any packet = body.packet + '—viacwop.rest'; - } else if (body.time && body.id && body.lat && body.long && body.tempf && body.windspeedmph && body.windgustmph && body.winddir) { // otherwise, check for required params to build our own + } else if (body.time && body.id && body.lat && body.long && body.tempf != null && body.windspeedmph != null && body.windgustmph != null && body.winddir != null) { // otherwise, check for required params to build our own packet = buildPacket(body); } else { return new Response('Missing required packet or readings parameters in payload', { "status": 422 }); From d6d68cfe6a30c1c42593e354e85215c66f134033 Mon Sep 17 00:00:00 2001 From: Justin Horner Date: Mon, 12 Aug 2024 17:07:56 -0400 Subject: [PATCH 3/4] more logging, extensible & cleanup added some logging; updates to make the code more adaptable to different domain names; general cleanup --- send-cwop-rest.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/send-cwop-rest.js b/send-cwop-rest.js index e85298f..34de3b8 100644 --- a/send-cwop-rest.js +++ b/send-cwop-rest.js @@ -1,5 +1,8 @@ import { connect } from 'cloudflare:sockets'; const cache = caches.default; +const host_domain_name = '' +const host_url = `https://${host_domain_name}`; +const packet_sender_name = 'cwop.rest'; export default { async fetch(request) { @@ -70,7 +73,7 @@ export async function handleRequest(request) { // possibly valid! has this id sent recently? const id = packet.split('>')[0]; - const cacheKey = 'https://send.cwop.rest/id=' + id; + const cacheKey = `${host_url}/id=${id}`; const lastSentTimeResponse = await cache.match(cacheKey); if (lastSentTimeResponse) { const lastSentTime = await lastSentTimeResponse.text(); @@ -84,7 +87,7 @@ export async function handleRequest(request) { if (validationCode) server = 'rotate.aprs.net'; // http://www.wxqa.com/servers2use.html if (manuallySpecifiedServer) server = manuallySpecifiedServer; - if (url.host !== 'send.cwop.rest') { // for testing + if (url.host !== host_domain_name) { // for testing return new Response('APRS packet "' + packet + '" would have been sent to ' + server, { "status": 200 }); } @@ -97,7 +100,10 @@ export async function handleRequest(request) { await cache.put(cacheKey, new Response(Date.now().toString())); - return new Response('APRS packet "' + packet + '" sent to ' + server, { "status": 200 }); + let msg = `APRS packet '${packet}' sent to '${server}'`; + console.log(msg); + + return new Response(msg, { "status": 200 }); } @@ -199,7 +205,7 @@ function buildPacket(observation) { } } - packet += 'cwop.rest'; + packet += packet_sender_name; return packet; @@ -305,6 +311,5 @@ export async function sendPacket(packet, server, port, validationCode = '-1') { console.log('Closing connection to ' + server + ':' + port); - return new Response(serverResponse, { "headers": { "Content-Type": "text/plain" } }); - + return new Response(serverResponse, { "headers": { "Content-Type": "text/plain" } }); } \ No newline at end of file From d1be330b662f643f512cc777c76e95d34029712f Mon Sep 17 00:00:00 2001 From: Justin Horner Date: Mon, 12 Aug 2024 17:24:20 -0400 Subject: [PATCH 4/4] added default domain --- send-cwop-rest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/send-cwop-rest.js b/send-cwop-rest.js index 34de3b8..37c8194 100644 --- a/send-cwop-rest.js +++ b/send-cwop-rest.js @@ -1,6 +1,6 @@ import { connect } from 'cloudflare:sockets'; const cache = caches.default; -const host_domain_name = '' +const host_domain_name = 'send.cwop.rest' const host_url = `https://${host_domain_name}`; const packet_sender_name = 'cwop.rest';