Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code for restricting put on GUN
/* You'll need this on your client
Gun.on('opt', function (ctx) {
if (ctx.once) {
return
}
ctx.on('out', function (msg) {
var to = this.to
// Adds headers for put
msg.headers = {
token: 'thisIsTheTokenForReals'
}
to.next(msg) // pass to next middleware
})
})
*/
const port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 8000
const Gun = require('gun')
function isValidPut (msg) {
return msg && msg && msg.headers && msg.headers.token && msg.headers.token === 'thisIsTheTokenForReals'
}
// Restrict put
Gun.on('opt', function (ctx) {
if (ctx.once) {
return
}
ctx.on('in', function (msg) {
var to = this.to
if (msg.put) {
if (isValidPut(msg)) {
to.next(msg)
}
} else {
to.next(msg)
}
})
})
const server = require('http').createServer((req, res) => {
// filters gun requests!
if (Gun.serve(req, res)) {
return
}
require('fs').createReadStream(require('path').join(__dirname, req.url)).on('error', function () { // static files!
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(require('fs')
.readFileSync(require('path')
.join(__dirname, 'index.html') // or default to index
))
}).pipe(res) // stream
})
Gun({
file: 'data.json',
web: server
})
server.listen(port)
console.log('Server started on port ' + port + ' with /gun')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.