Created
October 20, 2023 06:01
-
-
Save mayankchoubey/bdfc93f19af958bd551afb1973d77ffa to your computer and use it in GitHub Desktop.
Node.js - Fastify Clustered URL shortener service in PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { DataTypes, Sequelize } from "sequelize"; | |
| const dbUser = process.env.dbUser; | |
| const dbUserPass = process.env.dbUserPass; | |
| const dbName = process.env.dbName; | |
| const sequelize = new Sequelize( | |
| `postgres://${dbUser}:${dbUserPass}@localhost:5432/${dbName}`, | |
| { | |
| logging: false, | |
| pool: { | |
| max: 10, | |
| min: 10, | |
| }, | |
| }, | |
| ); | |
| await sequelize.authenticate(); | |
| const ShortenedUrl = sequelize.define("shortenedurl", { | |
| id: { | |
| type: DataTypes.STRING, | |
| primaryKey: true, | |
| }, | |
| srcurl: DataTypes.STRING, | |
| created: DataTypes.DATE, | |
| lastaccessed: DataTypes.DATE, | |
| }, { | |
| timestamps: false, | |
| }); | |
| export async function save(id, srcUrl) { | |
| await ShortenedUrl.create({ | |
| id, | |
| srcurl: srcUrl, | |
| created: new Date(), | |
| lastaccessed: new Date(), | |
| }); | |
| return true; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { shorten } from "./service.mjs"; | |
| export async function handleRequest(req, rep) { | |
| if (!(req.body && req.body.srcUrl)) { | |
| return rep.code(400).send({ errMsg: "Parameter 'srcUrl' is missing" }); | |
| } | |
| const srcUrl = req.body.srcUrl; | |
| if (srcUrl.length > 250) { | |
| return rep.code(400).send({ | |
| errMsg: "Parameter 'srcUrl' must not be more than 250 characters", | |
| }); | |
| } | |
| if (!(srcUrl.startsWith("http://") || srcUrl.startsWith("https://"))) { | |
| return rep.code(400).send({ | |
| errMsg: "Parameter 'srcUrl' must start with http:// or https://", | |
| }); | |
| } | |
| const shortenedUrl = await shorten(srcUrl); | |
| if (!shortenedUrl) { | |
| return rep.code(500).send({ errMsg: "Failed to shorten" }); | |
| } | |
| rep.send({ srcUrl, shortenedUrl }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cluster from "node:cluster"; | |
| import Fastify from "fastify"; | |
| import { handleRequest } from "./fastifyController.mjs"; | |
| const numClusterWorkers = parseInt(process.argv[2] || 1); | |
| if (cluster.isPrimary) { | |
| for (let i = 0; i < numClusterWorkers; i++) { | |
| cluster.fork(); | |
| } | |
| cluster.on( | |
| "exit", | |
| (worker, code, signal) => console.log(`worker ${worker.process.pid} died`), | |
| ); | |
| } else { | |
| const app = Fastify({ | |
| logger: false, | |
| }); | |
| app.post("/shorten", handleRequest); | |
| app.listen({ port: 3000 }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { nanoid } from "nanoid"; | |
| import { save } from "./db.mjs"; | |
| const baseUrl = "http://test.short/"; | |
| export async function shorten(srcUrl) { | |
| if (!srcUrl) { | |
| return; | |
| } | |
| const urlId = nanoid(10); | |
| const shortenedUrl = `${baseUrl}${urlId}`; | |
| const dbStatus = await save(urlId, srcUrl); | |
| return dbStatus ? shortenedUrl : undefined; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment