From dfcfb5c5f5adb5a959300ea585119965cf1b9319 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sun, 13 May 2018 10:48:03 +0530 Subject: [PATCH] [magnet] Added convenience get method, made changes, removed deps --- app.js | 4 ---- bin/www | 2 +- package.json | 12 +++++++----- public/index.html | 11 +++++++++-- public/stylesheets/style.css | 14 +++++++++++++- routes/index.js | 2 +- routes/magnet.js | 40 ++++++++++++++++++++++++++-------------- 7 files changed, 57 insertions(+), 28 deletions(-) diff --git a/app.js b/app.js index 8660954..ec1b2e3 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,5 @@ const express = require('express'); const path = require('path'); -const bodyParser = require('body-parser'); -const cookieParser = require('cookie-parser'); const logger = require('morgan'); const router = require('./routes'); @@ -11,8 +9,6 @@ const app = express(); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); -app.use(bodyParser.json()); -app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', router); diff --git a/bin/www b/bin/www index 3962fef..79011d1 100755 --- a/bin/www +++ b/bin/www @@ -87,5 +87,5 @@ function onListening() { ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); - console.log('Listening on ' + bind); + console.log('magnet:server is Listening on ' + bind); } diff --git a/package.json b/package.json index 51f8d68..34dcacc 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,15 @@ { - "name": "magnet", - "version": "0.0.0", - "private": true, + "name": "@codefeathers/magnet", + "version": "0.5.0", + "license": "MIT", + "author": "Muthu Kumar <@MKRhere> (https://mkr.pw)", + "bin": { + "magnet": "./bin/www" + }, "scripts": { "start": "node ./bin/www" }, "dependencies": { - "body-parser": "^1.18.2", - "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", "leveldown": "^3.0.2", diff --git a/public/index.html b/public/index.html index ab1ad8a..f6836a0 100644 --- a/public/index.html +++ b/public/index.html @@ -6,8 +6,15 @@ -

Express

-

Welcome to Express

+

⚡ :magnet:

+

POST to /api with this request body: +

+      {
+        uri: magnet:?,
+        title: 'Awesome magnet links!'
+      }
+    
+

diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 9453385..356898e 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -1,6 +1,18 @@ body { padding: 50px; - font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; + font: 14px "Roboto", Helvetica, Arial, sans-serif; +} + +code { + display: inline; + padding: 6px; + background: #FEE; + color: red; +} + +pre { + background: #EEE; + padding: 40px; } a { diff --git a/routes/index.js b/routes/index.js index b2a7a07..424231a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -18,7 +18,7 @@ router.get('/:shortlink', (req, res, next) => { const r = String(record); const [ magnet, title ] = r.split('@@title@@'); const meta = { - title: title ? title : 'magent ⚡️' + title: title ? '⚡ ' + title : '⚡ :magnet:' } res.send(template(magnet, meta)) }) diff --git a/routes/magnet.js b/routes/magnet.js index 7ef37f9..9b01af6 100644 --- a/routes/magnet.js +++ b/routes/magnet.js @@ -3,35 +3,47 @@ const router = express.Router(); const db = require('../modules/db'); -/* POST magnet link. */ -router.post('/', function(req, res, next) { +const createMagnet = (uri, title, baseUrl, ctx) => { const key = Math.random().toString(36).slice(4); - const { uri, title } = req.body; - const baseUrl = req.protocol + '://' + req.get('host'); - - if(!uri.match(/magnet:\?xt=urn:.*/i)) { + if (!uri.match(/magnet:\?xt=urn:.*/i)) { console.log(`[ERR!] Invalid request ${uri}`); - res.status(400); - res.json = { + ctx.res.status(400); + ctx.res.json({ err: 'Invalid magnet URI' - } + }); return; } const value = title ? uri + '@@title@@' + title : uri; db.put(key, value) - .then(() => res.json({ + .then(() => ctx.res.json({ status: 'OK', uri: baseUrl + '/' + key })) .catch(e => { console.log(`[ERR!] Occured while creating new shortlink`, e.stack); - res.status(520); - res.json = { + ctx.res.status(520); + ctx.res.json({ err: 'Unknown err! Please try again' - } + }); }) +} + +router.get('/:body', (req, res, next) => { + const body = req.originalUrl.split('/api/')[1]; + const [ uri, title ] = body.split('@@title@@'); + const baseUrl = req.protocol + '://' + req.get('host'); + + return createMagnet(uri, title, baseUrl, { req, res }); +}) + +/* POST magnet link. */ +router.post('/', (req, res, next) => { + const { uri, title } = req.body; + const baseUrl = req.protocol + '://' + req.get('host'); + + return createMagnet(uri, title, baseUrl, { req, res }); }); -module.exports = router; +module.exports = router; \ No newline at end of file