linkylinky/src/server.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-08-12 17:10:36 +00:00
const fastify = require('fastify')({ logger: true })
2021-08-12 18:17:10 +00:00
var uniqid = require('uniqid');
2021-08-12 18:24:26 +00:00
let config = {
domain: process.env.DOMAIN || "localhost:3000",
2021-08-12 18:26:33 +00:00
https: (process.env.SSL === 'true') || false,
2021-08-12 18:24:26 +00:00
getBaseUrl(){
if(config.https){
return `https://${config.domain}`;
}
return `http://${config.domain}`;
}
}
2021-08-12 18:13:46 +00:00
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./dev.sqlite3"
}
});
2021-08-12 17:10:36 +00:00
//Basic home route
2021-08-12 17:10:36 +00:00
fastify.get('/', async (request, reply) => {
2021-08-12 17:22:14 +00:00
return { hello: 'world' }
2021-08-12 17:10:36 +00:00
})
2021-08-12 17:22:14 +00:00
//Automagic Amazn redirects on /a/
fastify.get('/a/:id', async (req, res) => {
2021-08-12 17:10:36 +00:00
res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
})
2021-08-12 17:22:14 +00:00
//Automagic Youtube redirects on /yt/
fastify.get('/yt/:id', async (req, res) => {
res.redirect(302, `https://youtu.be/${req.params.id}`)
2021-08-12 17:10:36 +00:00
})
//Normal shorturls
2021-08-12 18:13:46 +00:00
fastify.get('/:shortcode', async (req, res) => {
const shortcode = req.params.shortcode;
if (!shortcode) {
2021-08-12 17:22:14 +00:00
return 404;
}
2021-08-12 18:13:46 +00:00
const target = await knex.select('target')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (!target[0]) {
return 404
}
res.redirect(302, target[0].target)
2021-08-12 17:22:14 +00:00
})
2021-08-12 17:10:36 +00:00
//Create new urls
2021-08-12 17:39:10 +00:00
const newUrlSchema = {
body: {
type: 'object',
properties: {
target: { type: 'string' },
shortcode: { type: 'string' },
}
}
};
fastify.post('/new', { newUrlSchema }, async (req, res) => {
const target = req.body?.target;
2021-08-12 17:34:36 +00:00
let shortcode = req.body?.shortcode;
2021-08-12 17:39:10 +00:00
if (!target) {
res.statusCode = 400;
return "Missing target";
}
2021-08-12 18:13:46 +00:00
if (!shortcode) {
2021-08-12 18:13:46 +00:00
const exists = await knex.select('shortcode')
.from('urls')
.where('target', '=', target)
.limit(1);
if (exists.length != 0) {
shortcode = exists[0].shortcode;
return {
2021-08-12 18:24:26 +00:00
url: `${config.getBaseUrl()}/${shortcode}`,
2021-08-12 18:13:46 +00:00
shortcode,
target
}
}
2021-08-12 18:17:10 +00:00
shortcode = uniqid();
}
2021-08-12 17:39:10 +00:00
else {
2021-08-12 18:13:46 +00:00
const exists = await knex.select('shortcode')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (exists.length != 0) {
res.statusCode = 400;
return "Shortcode already exists, please choose another code";
}
}
2021-08-12 18:13:46 +00:00
await knex('urls').insert({target, shortcode});
return {
2021-08-12 18:24:26 +00:00
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
2021-08-12 18:13:46 +00:00
}
2021-08-12 17:10:36 +00:00
})
// Run the server!
const start = async () => {
2021-08-12 17:22:14 +00:00
try {
await fastify.listen(3000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
2021-08-12 17:10:36 +00:00
}
start()