Skip to content

Commit

Permalink
Merge pull request #4 from justinhorner/cloudflare-worker
Browse files Browse the repository at this point in the history
Parameter Name Fixes, #3.
  • Loading branch information
leoherzog authored Aug 20, 2024
2 parents 6be83cf + d1be330 commit 738c0e3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions send-cwop-rest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { connect } from 'cloudflare:sockets';
const cache = caches.default;
const host_domain_name = 'send.cwop.rest'
const host_url = `https://${host_domain_name}`;
const packet_sender_name = 'cwop.rest';

export default {
async fetch(request) {
Expand Down Expand Up @@ -47,7 +50,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 != 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 });
Expand All @@ -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();
Expand All @@ -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 });
}

Expand All @@ -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 });

}

Expand Down Expand Up @@ -199,7 +205,7 @@ function buildPacket(observation) {
}
}

packet += 'cwop.rest';
packet += packet_sender_name;

return packet;

Expand Down Expand Up @@ -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" } });
}

0 comments on commit 738c0e3

Please sign in to comment.