const express = require('express');
const router = express.Router();
const db = require('../modules/db');
const magnet = require('./magnet');
const foundTemplate = (magnet, meta) => (`
${meta.title}
${meta.title}
${magnet}
`)
const notFoundTemplate = () => (`
Not Found
⚡ Oops! Nothing found here.
`)
router.get('/:shortlink', (req, res) => {
db.get(req.params.shortlink)
.then(record => {
const r = String(record);
const [magnet, title] = r.split('@@title@@');
const meta = {
title: title ? '⚡ ' + title : '⚡ :magnet:'
}
res.send(foundTemplate(magnet, meta))
})
.catch(e => {
console.log(`[ERR!] Occured while retrieving shortlink`, e.stack);
res.send(notFoundTemplate())
});
});
/* GET home page. */
router.get('/', function (req, res) {
res.render('index', { title: ':magnet: ⚡️' });
});
router.use('/api', magnet);
module.exports = router;