Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
mayankchoubey / node_app.mjs
Created November 17, 2023 06:29
Node.js - JWT sign & verify
import jwt from "jsonwebtoken";
import { readFileSync } from "node:fs";
const emails = JSON.parse(
readFileSync(
"/Users/mayankc/Work/source/perfComparisons/testdata/emails.json",
),
);
let i = 1, idx = 0;
@mayankchoubey
mayankchoubey / api.rs
Created October 26, 2023 23:35
Rust - URL shortener service in PostgreSQL
use crate::repository::database::Database;
use crate::{
types::types::ShortenUrl, types::types::UrlShortenerError, types::types::UrlShortenerRequest,
types::types::UrlShortenerResponse,
};
use actix_web::{delete, get, post, put, web, HttpResponse};
use chrono::{DateTime, Utc};
use nanoid::nanoid;
const baseUrl: &str = "http://test.short/";
@mayankchoubey
mayankchoubey / ShortenedUrl.java
Created October 25, 2023 06:05
Quarkus - URL shortener service in PostgreSQL
package com.example;
import java.time.LocalDateTime;
import io.vertx.mutiny.sqlclient.Row;
public record ShortenedUrl(
String id,
String srcurl,
LocalDateTime created,
LocalDateTime lastaccessed) {
@mayankchoubey
mayankchoubey / ShortenedUrl.java
Last active October 22, 2023 06:00
Spring Boot - URL shortener service in PostgreSQL
package com.example.shorten;
import java.time.LocalDateTime;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@mayankchoubey
mayankchoubey / db.mjs
Created October 20, 2023 06:01
Node.js - Fastify Clustered URL shortener service in PostgreSQL
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,
@mayankchoubey
mayankchoubey / db.mjs
Created October 20, 2023 05:46
Node.js - Express Clustered URL shortener service in PostgreSQL
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,
@mayankchoubey
mayankchoubey / controller.go
Last active October 19, 2023 01:43
Go - Gin URL shortener service using PostgreSQL
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
type ShortenUrlRequestBody struct {
SrcUrl string `json:"srcUrl"`
@mayankchoubey
mayankchoubey / controller.js
Created October 15, 2023 03:23
Bun - Elysia URL shortener service in PostgreSQL
import { shorten } from "./service.js";
export async function handleRequest(ctx) {
if (!(ctx.body && ctx.body.srcUrl)) {
ctx.set.status = 400;
return { errMsg: "Parameter 'srcUrl' is missing" };
}
const srcUrl = ctx.body.srcUrl;
if (srcUrl.length > 250) {
@mayankchoubey
mayankchoubey / controller.js
Created October 15, 2023 03:22
Deno - Hono URL shortener service in PostgreSQL
import { shorten } from "./service.js";
import { HTTPException } from "https://deno.land/x/hono/mod.ts";
export async function handleRequest(ctx) {
const ctHdr = ctx.req.header("content-type");
if (!(ctHdr && ctHdr.includes("application/json"))) {
throw new HTTPException(415, { message: "Content type JSON is required" });
}
let reqBody;
@mayankchoubey
mayankchoubey / controller.py
Last active October 18, 2023 04:21
Python - FastAPI URL shortener service in PostgreSQL
from service import shorten
from fastapi import FastAPI, Response, Request, HTTPException
from fastapi.responses import JSONResponse
async def handleRequest(reqBody: any):
if not reqBody.srcUrl:
raise HTTPException(status_code=400, detail="Parameter 'srcUrl' is missing")
srcUrl = reqBody.srcUrl