Browse Source

[magnet] Added convenience get method, made changes, removed deps

pull/2/head
Muthu Kumar 6 years ago
parent
commit
dfcfb5c5f5
  1. 4
      app.js
  2. 2
      bin/www
  3. 12
      package.json
  4. 11
      public/index.html
  5. 14
      public/stylesheets/style.css
  6. 2
      routes/index.js
  7. 34
      routes/magnet.js

4
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);

2
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);
}

12
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",

11
public/index.html

@ -6,8 +6,15 @@
</head>
<body>
<h1>Express</h1>
<p>Welcome to Express</p>
<h1>⚡ :magnet:</h1>
<p>POST to <code>/api</code> with this request body:
<pre>
{
uri: magnet:?,
title: 'Awesome magnet links!'
}
</pre>
</p>
</body>
</html>

14
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 {

2
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))
})

34
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)) {
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;
Loading…
Cancel
Save