From ad3cd851d9c8bbc51122a1f8fe4b877431e44320 Mon Sep 17 00:00:00 2001 From: Jipson Minibhavan Date: Mon, 15 Jan 2024 14:50:47 +0100 Subject: [PATCH] add resolver --- .DS_Store | Bin 6148 -> 6148 bytes resolver/package-lock.json | 23 ++++++++++++----------- resolver/package.json | 2 +- resolver/src/models/Link.ts | 4 ++-- resolver/src/routes/linkRoutes.ts | 19 +++++++++++++++++++ resolver/src/server.ts | 4 ++-- 6 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 resolver/src/routes/linkRoutes.ts diff --git a/.DS_Store b/.DS_Store index b58b51eb1bcce3b292f0b283b09be84d2d433f4f..f12a0dc60a92e13afb3d3b169915a255e125a9e7 100644 GIT binary patch delta 150 zcmZoMXfc=|&e%S&P;8=}q9_Xk0|O%ig8&0VDnlMaN=k8ZPE!8Fjp~*l33i4ghD3&B zhHPZX{2ZVR0|!GILlHwhP)`Y(= 0.4" diff --git a/resolver/package.json b/resolver/package.json index fd68d9c..015ead9 100644 --- a/resolver/package.json +++ b/resolver/package.json @@ -5,7 +5,7 @@ "main": "dist/server.js", "scripts": { "dev": "concurrently \"tsc --watch\" \"nodemon ./dist/server.js\"", - "start": "node ./dist/index.js", + "start": "node ./dist/server.js", "build": "tsc", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/resolver/src/models/Link.ts b/resolver/src/models/Link.ts index 7cff635..0e811f3 100644 --- a/resolver/src/models/Link.ts +++ b/resolver/src/models/Link.ts @@ -1,12 +1,12 @@ import mongoose, { Schema, Document } from "mongoose"; export interface ILink extends Document { - slug: string; + path: string; url: string; } const linkSchema = new Schema({ - slug: { type: String, required: true, unique: true }, + path: { type: String, required: true, unique: true }, url: { type: String, required: true }, }); diff --git a/resolver/src/routes/linkRoutes.ts b/resolver/src/routes/linkRoutes.ts new file mode 100644 index 0000000..0d72490 --- /dev/null +++ b/resolver/src/routes/linkRoutes.ts @@ -0,0 +1,19 @@ +import express from "express"; +import Link from "../models/Link"; + +const router = express.Router(); +router.get("/:path", async (req, res) => { + try { + const path = req.params.path; + const link = await Link.findOne({ path: path }); + + if (link) { + res.redirect(link.url); + } else { + res.status(404).send("URL not found"); + } + } catch (error) { + res.status(500).send("Server error"); + } +}); +export default router; diff --git a/resolver/src/server.ts b/resolver/src/server.ts index c366d67..7b28e2d 100644 --- a/resolver/src/server.ts +++ b/resolver/src/server.ts @@ -1,5 +1,5 @@ import App from "./app"; - -const app = new App([]); +import linkRoutes from "./routes/linkRoutes"; +const app = new App([linkRoutes]); app.listen();