From df3ae0d3f27e97fa421b77c806b29427d0db8af2 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Tue, 5 May 2020 19:12:44 -0400 Subject: [PATCH] add loc.sh routes and systems --- src/api/loc.sh/main.ts | 6 ++++++ src/api/loc.sh/routes/root.ts | 22 ++++++++++++++++++++++ src/api/server.ts | 1 + 3 files changed, 29 insertions(+) create mode 100644 src/api/loc.sh/main.ts create mode 100644 src/api/loc.sh/routes/root.ts create mode 100644 src/api/server.ts diff --git a/src/api/loc.sh/main.ts b/src/api/loc.sh/main.ts new file mode 100644 index 0000000..65f7092 --- /dev/null +++ b/src/api/loc.sh/main.ts @@ -0,0 +1,6 @@ +import { Server, ServerManagement } from '../../class'; + +export default function (management: ServerManagement) { + const server = new Server(management, 3890, `${__dirname}/routes`); + return server; +} diff --git a/src/api/loc.sh/routes/root.ts b/src/api/loc.sh/routes/root.ts new file mode 100644 index 0000000..01f478b --- /dev/null +++ b/src/api/loc.sh/routes/root.ts @@ -0,0 +1,22 @@ +import { Route, Server } from '../../../class'; +import { RedirectInterface } from '../../../models'; + +export default class Root extends Route { + constructor(server: Server) { + super(server, { path: '/' }); + } + + public bind() { + this.router.get('/:key', async (req, res) => { + try { + // @ts-ignore + const link: RedirectInterface = await this.server.client.db.redirect.findOne({ key: req.params.key }).lean().exec(); + if (!link) return res.status(404).json({ code: this.constants.codes.NOT_FOUND, message: this.constants.messages.NOT_FOUND }); + return res.redirect(link.to); + } catch (err) { + this.server.client.util.handleError(err); + return res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR }); + } + }); + } +} diff --git a/src/api/server.ts b/src/api/server.ts new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/api/server.ts @@ -0,0 +1 @@ +