Added config object

This commit is contained in:
2021-08-12 20:24:26 +02:00
parent 68b9b43891
commit 7cfcb54195

View File

@@ -1,6 +1,17 @@
const fastify = require('fastify')({ logger: true }) const fastify = require('fastify')({ logger: true })
var uniqid = require('uniqid'); var uniqid = require('uniqid');
let config = {
domain: process.env.DOMAIN || "localhost:3000",
https: false,
getBaseUrl(){
if(config.https){
return `https://${config.domain}`;
}
return `http://${config.domain}`;
}
}
const knex = require('knex')({ const knex = require('knex')({
client: 'sqlite3', client: 'sqlite3',
connection: { connection: {
@@ -66,7 +77,7 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
if (exists.length != 0) { if (exists.length != 0) {
shortcode = exists[0].shortcode; shortcode = exists[0].shortcode;
return { return {
url: `http://localhost:3000/${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
shortcode, shortcode,
target target
} }
@@ -86,7 +97,7 @@ fastify.post('/new', { newUrlSchema }, async (req, res) => {
await knex('urls').insert({target, shortcode}); await knex('urls').insert({target, shortcode});
return { return {
url: `http://localhost:3000${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
shortcode, shortcode,
target target
} }