From 0918839212b745486f795c2b7f647c7efae8b350 Mon Sep 17 00:00:00 2001 From: samcm Date: Sat, 2 Dec 2017 12:16:41 +1100 Subject: [PATCH] Invite Link API --- package.json | 1 + webapp.js | 65 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 001e4299..f98f1815 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "dependencies": { "args-parser": "^1.1.0", + "body-parser": "^1.18.2", "cors": "^2.8.3", "jsonfile": "^4.0.0", "socket.io": "^2.0.3", diff --git a/webapp.js b/webapp.js index 97df0180..9c4bd3c7 100644 --- a/webapp.js +++ b/webapp.js @@ -23,29 +23,64 @@ var express = require('express'); var path = require('path'); var cors = require('cors') var jsonfile = require('jsonfile') -var file = 'ptinvites.json' +var bodyParser = require('body-parser') +var file = 'ptinvites.json' + var root = express() +root.use( bodyParser() ) +// root.use(bodyParser.urlencoded({ extended: true })) // Setup our web app -root.use('/ptweb/',express.static(path.join(__dirname, 'dist'))); +root.use('/ptweb/', express.static(path.join(__dirname, 'dist'))); // Merge everything together -root.get('/ptweb/invite/:id',function(req,res){ +root.get('/ptweb/invite/:id', (req,res) => { console.log('handling an invite') let shortObj = shortenedLinks[req.params.id] if (!shortObj){ return res.send('Invite expired') } console.log('Redirecting an invite link') - console.log(JSON.stringify(shortObj,null,4)) + console.log(JSON.stringify(shortObj, null, 4)) return res.redirect(shortObj.fullUrl) +}) +root.post('/ptweb/invite', (req, res) => { + res.setHeader('Content-Type', 'application/json') + if (!req.body) { + return res.send({ + success: false, + msg: 'ERR: You did not send any POST data' + }).end() + } + console.log('Generating Invite URL via the API.') + let data = {} + let fields = ['ptserver', 'ptroom', 'ptpassword', 'owner'] + for (let i = 0; i < fields.length; i++) { + if (!req.body[fields[i]]) { + return res.send({ + success: false, + msg: 'ERR: You did not specify ' + fields[i], + field: fields[i] + }).end() + } + data[fields[i]] = req.body[fields[i]] + } + return res.send({ + url: shortenObj(data), + success: true, + generatedAt: new Date().getTime(), + details: data + }).end() + + + }) root.use('/',express.static(path.join(__dirname, 'dist'))) -root.get('*',function(req,res){ +root.get('*',(req,res) => { console.log('Catch all') return res.redirect('/') }) @@ -56,7 +91,7 @@ root.use(cors()) var rootserver = require('http').createServer(root); -var webapp_io = require('socket.io')(rootserver,{path: '/ptweb/socket.io'}); +var webapp_io = require('socket.io')(rootserver, { path: '/ptweb/socket.io'} ) function getUniqueId(){ while (true){ @@ -91,31 +126,31 @@ function shortenObj(data){ } let query = '' for (let key in params) { - query += encodeURIComponent(key)+'='+encodeURIComponent(params[key])+'&'; + query += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&'; } returnable.fullUrl = accessIp + '/#/join?' + query shortenedLinks[returnable.id] = returnable - saveToFile(shortenedLinks,()=>{}) + saveToFile(shortenedLinks, () => {}) return returnable.shortUrl } -webapp_io.on('connection', function(socket){ +webapp_io.on('connection', (socket) => { console.log('New connection to the webapp socket') - socket.on('shorten',function(data){ + socket.on('shorten',(data) => { console.log('Creating a shortened link') - socket.emit('shorten-result',shortenObj(data)) + socket.emit('shorten-result', shortenObj(data)) }) }) function saveToFile(content,callback){ - jsonfile.writeFile(file, content, function (err) { + jsonfile.writeFile(file, content, (err) => { return callback(err) }) } function loadFromFile(callback){ - jsonfile.readFile(file, function(err, obj) { + jsonfile.readFile(file, (err, obj) => { if (err || !obj){ // File doesn't exist or an error occured return callback({}) @@ -127,7 +162,7 @@ function loadFromFile(callback){ function killOldInvites(){ let now = (new Date).getTime() - loadFromFile(function(data){ + loadFromFile((data) => { if (!data){ return } @@ -144,7 +179,7 @@ function killOldInvites(){ }) } killOldInvites() -setInterval(function() { +setInterval(() => { killOldInvites() }, 3600000)