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 @@ +