feat: ability to use / for URLS_ROUTE

This commit is contained in:
diced 2022-12-03 14:31:13 -08:00
parent ace474eb2c
commit cfdcf05135
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1

View file

@ -99,8 +99,10 @@ async function start() {
favicon.pipe(res);
});
router.on('GET', `${config.urls.route}/:id`, async (req, res, params) => {
const urlsRoute = async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer);
else if (params.id === 'dashboard' && !config.features.headless)
return nextServer.render(req, res as ServerResponse, '/dashboard');
const url = await prisma.url.findFirst({
where: {
@ -131,12 +133,9 @@ async function start() {
}
return redirect(res, url.destination);
});
};
router.on(
'GET',
config.uploader.route === '/' ? '/:id' : `${config.uploader.route}/:id`,
async (req, res, params) => {
const uploadsRoute = async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer);
else if (params.id === 'dashboard' && !config.features.headless)
return nextServer.render(req, res as ServerResponse, '/dashboard');
@ -158,8 +157,29 @@ async function start() {
postFile(image, prisma);
}
};
// makes sure to handle both in one route as you cant have two handlers with the same route
if (config.urls.route === '/' && config.uploader.route === '/') {
router.on('GET', '/:id', async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer);
else if (params.id === 'dashboard' && !config.features.headless)
return nextServer.render(req, res as ServerResponse, '/dashboard');
const url = await prisma.url.findFirst({
where: {
OR: [{ id: params.id }, { vanity: params.id }, { invisible: { invis: decodeURI(params.id) } }],
},
});
if (url) return urlsRoute(req, res, params);
else return uploadsRoute(req, res, params);
});
} else {
router.on('GET', config.urls.route === '/' ? '/:id' : `${config.urls.route}/:id`, urlsRoute);
router.on('GET', config.uploader.route === '/' ? '/:id' : `${config.uploader.route}/:id`, uploadsRoute);
}
);
router.on('GET', '/r/:id', async (req, res, params) => {
if (params.id === '') return notFound(req, res, nextServer);