Created
February 3, 2019 23:06
-
-
Save aaronbrown1988/07e21842f0b0217f48b23ea38e0865ef to your computer and use it in GitHub Desktop.
WSPR spot service cloud Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Super simple cloudfunction to catch WSPR spots and pass them on. | |
*/ | |
var request = require('request') | |
const Datastore = require('@google-cloud/datastore'); | |
const datastore = new Datastore(); | |
function validate(query) { | |
if (query == null || !query) { | |
return false | |
} | |
if (query.date == null || query.rcall == null) { | |
return false | |
} | |
if(query.function != "wspr") { | |
return false | |
} | |
return true | |
} | |
exports.wsprGET = (req, res) => { | |
if (validate(req.query)) { | |
console.log(JSON.stringify(req.query)) | |
const key = datastore.key(['Spot']); | |
datastore.save({ | |
key: key, | |
data: req.query | |
}, (err) => { | |
if (!err) { | |
// Record saved successfully. | |
request({ | |
url: 'http://wsprnet.org/post?', | |
qs: req.query | |
}, (err, response, body) => { | |
if (err) { | |
res.status(500).send(err); | |
return console.log(err); | |
} | |
console.log(body.url); | |
console.log(body.explanation); | |
res.send(`${JSON.stringify(req.query)} -> ${JSON.stringify(response)}`); | |
}); | |
} else { | |
res.status(500).send(JSON.stringify(err)); | |
} | |
}); | |
} else { | |
res.send(`${JSON.stringify(req.query)} is not valid`); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment