From cc3f53051fdfdb031645aabdf3aa4ca14ba7dd1e Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 12 Mar 2021 00:12:25 +0530 Subject: [PATCH] init websocket support --- .gitignore | 2 +- env.example | 1 + package-lock.json | 70 ++++++++++++++++++++++++++++++++++++++++++++++- package.json | 4 ++- src/api/index.js | 31 ++++++++++++++++++--- src/index.js | 26 ++++++++++++++++-- src/ws/server.js | 5 ++++ 7 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 src/ws/server.js diff --git a/.gitignore b/.gitignore index 39024ba..3ec544c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules/ -.env.local \ No newline at end of file +.env \ No newline at end of file diff --git a/env.example b/env.example index dc86968..5a942fb 100644 --- a/env.example +++ b/env.example @@ -1,2 +1,3 @@ NODE_ENV=development PORT=5000 +CMS_TOKEN= \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 54ec48f..de844ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,12 +8,14 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "axios": "^0.21.1", "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", "helmet": "^4.1.0", "mongoose": "^5.11.19", - "morgan": "^1.10.0" + "morgan": "^1.10.0", + "ws": "^7.4.4" }, "devDependencies": { "nodemon": "^2.0.4" @@ -149,6 +151,14 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "node_modules/axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "dependencies": { + "follow-redirects": "^1.10.0" + } + }, "node_modules/balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -693,6 +703,25 @@ "node": ">= 0.8" } }, + "node_modules/follow-redirects": { + "version": "1.13.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", + "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", @@ -2077,6 +2106,26 @@ "typedarray-to-buffer": "^3.1.5" } }, + "node_modules/ws": { + "version": "7.4.4", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.4.tgz", + "integrity": "sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw==", + "engines": { + "node": ">=8.3.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xdg-basedir": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz", @@ -2199,6 +2248,14 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -2640,6 +2697,11 @@ "unpipe": "~1.0.0" } }, + "follow-redirects": { + "version": "1.13.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", + "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==" + }, "forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", @@ -3706,6 +3768,12 @@ "typedarray-to-buffer": "^3.1.5" } }, + "ws": { + "version": "7.4.4", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.4.tgz", + "integrity": "sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw==", + "requires": {} + }, "xdg-basedir": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz", diff --git a/package.json b/package.json index 0999686..454f223 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,14 @@ "author": "", "license": "MIT", "dependencies": { + "axios": "^0.21.1", "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", "helmet": "^4.1.0", "mongoose": "^5.11.19", - "morgan": "^1.10.0" + "morgan": "^1.10.0", + "ws": "^7.4.4" }, "devDependencies": { "nodemon": "^2.0.4" diff --git a/src/api/index.js b/src/api/index.js index 5261da6..1b2aa2f 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,11 +1,34 @@ const express = require('express') +const axios = require('axios') const router = express.Router() -router.get('/', (req, res) => { - res.json({ - message: 'API!' - }) +router.post('/', async (req, res) => { + try { + const token = req.body.token; + const r = await axios.post('https://cms.aplesports.com/api/collections/get/nuke', { + filter: { + token, + }, + }, { + headers: { + 'Cockpit-Token': process.env.CMS_TOKEN, + }, + }) + if (r.data.entries.length === 1) { + res.json({ + message: r.data.entries.length + }) + } else { + res.status(403).json({ + message: r.data.entries, + }) + } + } catch(e) { + res.status(403).json({ + message: 'Forbidden', + }) + } }) module.exports = router diff --git a/src/index.js b/src/index.js index f11ef2b..c7c795a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,29 @@ +const WebSocket = require('ws') +const url = require('url') const app = require('./app') const port = process.env.PORT || 5000; -app.listen(port, () => { - console.log(`Listening: http://localhost:${port}`) +wss = new WebSocket.Server({ + noServer: true, }) + +wss.on('connection', (ws) => { + ws.on('message', (msg) => { + console.log(`received ${msg}`); + }) + ws.send('something'); +}) + +const server = app.listen(port, () => { + console.log(`Express listening: http://localhost:${port}`) +}) + +server.on('upgrade', (req, socket, head) => { + const pathname = url.parse(req.url).pathname + if (pathname === '/ws') { + wss.handleUpgrade(req, socket, head, (socket) => { + wss.emit('connection', socket, req); + }) + } +}) \ No newline at end of file diff --git a/src/ws/server.js b/src/ws/server.js new file mode 100644 index 0000000..e7159de --- /dev/null +++ b/src/ws/server.js @@ -0,0 +1,5 @@ +const WebSocket = require('ws') + +let wss = null + +module.exports = wss; \ No newline at end of file