Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create cache for Private URLs #106

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions app/[short_id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
checkIfShortCodePublic,
getLongUrl,
publishUserAgent,
setPrivateShortCode,
} from "@/lib/services/redisPublicGenerate";
import { NextRequest } from "next/server";
import PrismaClientManager from "@/lib/services/pgConnect";
Expand All @@ -11,41 +12,40 @@ export async function GET(req: NextRequest) {
const path = req.nextUrl.pathname;
const shortCode = path.replace("/", "");

if (!checkIfShortCodePublic(shortCode)) {
// click analytics yet to be added
const prisma = PrismaClientManager.getInstance().getPrismaClient();
const link:any = await prisma.links.findFirst({
where:{
short_code:shortCode
}
})

if(!link){
return RESPONSE(
{
error: "Invalid input",
moreinfo: "Short link generated is invalid or expired",
},
HTTP_STATUS.BAD_REQUEST
);
}
const long_url = await getLongUrl(shortCode);

return Response.redirect(link?.long_url,301)
if (!!long_url) {
await publishUserAgent(req, shortCode);
return Response.redirect(long_url, 301);
}

const long_url = await getLongUrl(shortCode);

if (!long_url) {
if (!long_url && checkIfShortCodePublic(shortCode)) {
return RESPONSE(
{
error: "Invalid input",
moreinfo: "Short link generated is invalid or expired",
},
HTTP_STATUS.BAD_REQUEST
);
} else if (!long_url && !checkIfShortCodePublic(shortCode)) {
const prisma = PrismaClientManager.getInstance().getPrismaClient();
const link = await prisma.links.findFirst({
where: {
short_code: shortCode,
},
});

if (!link) {
return RESPONSE(
{
error: "Invalid input",
moreinfo: "Short link generated is invalid or expired",
},
HTTP_STATUS.BAD_REQUEST
);
}
await setPrivateShortCode(shortCode, link.long_url);
await publishUserAgent(req, shortCode);
return Response.redirect(link?.long_url, 301);
}

await publishUserAgent(req, shortCode);

return Response.redirect(long_url, 301);
}
8 changes: 5 additions & 3 deletions kafka/fixtures/kafka_consumer.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ CREATE TABLE click_analytics (
`device` LowCardinality(String),
`country` LowCardinality(String),
`region` String,
`city` String
`city` String,
`timestamp` Date DEFAULT toDate(now())
)
ENGINE = MergeTree()
ORDER BY (code, browser, os, device, country);
PARTITION BY toYYYYMM(timestamp)
ORDER BY (code, timestamp, browser, os, device, country);

-- Create a Materialized View named 'click_analytics_consumer' to transfer data from the Kafka table to the ClickHouse table
-- The view selects all columns from 'eurl_kafka' and inserts them into the 'click_analytics' table
CREATE MATERIALIZED VIEW click_analytics_consumer TO click_analytics AS
SELECT * FROM eurl_kafka;
SELECT * FROM eurl_kafka;
4 changes: 2 additions & 2 deletions kafka/services/userLocation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class UserLocationService {
ipAPI = "https://ipapi.co/json/?ip=";
ipAPI = "http://ip-api.com/json/";

async getUserLocation(ip) {
const response = await fetch(`${this.ipAPI}${ip}`);
const data = await response.json();
return {
country : data.country,
region : data.region,
region : data.regionName,
city : data.city,
}
}
Expand Down
169 changes: 84 additions & 85 deletions lib/services/privateLinkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,72 @@ import { base62_encode } from "@/lib/services/base62";

import incrementCounter from "@/lib/services/counter";
import { getServerSession } from "next-auth";
import z from 'zod'
import z from "zod";
import { ISessionType } from "@/interfaces/url";
import authOptions from "@/lib/authOptions";
import validateURLCreateReq from "@/lib/validations/url_create";
import PrismaClientManager from "@/lib/services/pgConnect";
import { HTTP_STATUS } from "@/lib/constants";
import { linkType } from "@/interfaces/types";
import {
setPrivateShortCode,
updatePrivateShortCode,
} from "./redisPublicGenerate";

const alphabetOnlySchema = z.string().regex(/^[a-zA-Z]+$/);

export async function createPrivateLink(formdata : FormData) {

export async function createPrivateLink(formdata: FormData) {
const posgresInstance = PrismaClientManager.getInstance();
const prisma = posgresInstance.getPrismaClient();
const { title,long_url, status } = await validateURLCreateReq(formdata);
const { title, long_url, status } = await validateURLCreateReq(formdata);
const session: ISessionType | null = await getServerSession(authOptions);

let custom_short_code:string = formdata.get('short_code') as string
let custom_short_code: string = formdata.get("short_code") as string;

// if custom short code exists
if(custom_short_code){

// check if duplicate
const link: linkType | null = await prisma.links.findFirst({
where: {
short_code:custom_short_code
}
})

if(link) return {
status: HTTP_STATUS.CONFLICT
}

// checking its regex
const res = alphabetOnlySchema.safeParse(custom_short_code);

if(!res.success || custom_short_code.startsWith("app")){
return {
status: HTTP_STATUS.BAD_REQUEST
}
}
if (custom_short_code) {
// check if duplicate
const link: linkType | null = await prisma.links.findFirst({
where: {
short_code: custom_short_code,
},
});

if (link)
return {
status: HTTP_STATUS.CONFLICT,
};

// checking its regex
const res = alphabetOnlySchema.safeParse(custom_short_code);

if (!res.success || custom_short_code.startsWith("app")) {
return {
status: HTTP_STATUS.BAD_REQUEST,
};
}
}

if (!status) {
return {
status: HTTP_STATUS.BAD_REQUEST
}
status: HTTP_STATUS.BAD_REQUEST,
};
}

if (!session?.user) {
return {
status: HTTP_STATUS.UNAUTHORIZED
}
status: HTTP_STATUS.UNAUTHORIZED,
};
}

const shortIdLength:number = await incrementCounter();
const shortIdLength: number = await incrementCounter();

if(!custom_short_code)
custom_short_code = base62_encode(shortIdLength);
if (!custom_short_code) custom_short_code = base62_encode(shortIdLength);

try {
await prisma.links.create({
data: {
title : title as string,
title: title as string,
long_url: long_url as string,
short_code: custom_short_code,
created_at: new Date(),
Expand All @@ -76,82 +78,79 @@ export async function createPrivateLink(formdata : FormData) {
},
},
});
await setPrivateShortCode(custom_short_code, long_url as string);
return {
short_url: custom_short_code,
status: HTTP_STATUS.CREATED
}

short_url: custom_short_code,
status: HTTP_STATUS.CREATED,
};
} catch (e) {
return {
status: HTTP_STATUS.INTERNAL_SERVER_ERROR
}

status: HTTP_STATUS.INTERNAL_SERVER_ERROR,
};
}
}

export async function updatePrivateLink(formdata : FormData){
const prisma = PrismaClientManager.getInstance().getPrismaClient();

const title: string = formdata.get('title') as string
const shortcode: string = formdata.get('short_code') as string
const linkId: number = Number.parseInt(formdata.get('linkId') as string)
export async function updatePrivateLink(formdata: FormData) {
const prisma = PrismaClientManager.getInstance().getPrismaClient();

try{
const link = await prisma.links.findFirst({
where:{
short_code:shortcode
}
})
const title: string = formdata.get("title") as string;
const shortcode: string = formdata.get("short_code") as string;
const linkId: number = Number.parseInt(formdata.get("linkId") as string);

if(link && link.id != linkId){
return {status: HTTP_STATUS.CONFLICT}
}
try {
const link = await prisma.links.findFirst({
where: {
short_code: shortcode,
},
});

await prisma.links.update({
where:{
id: linkId
},
data:{
title:title,
short_code:shortcode
}
})
if (link && link.id != linkId) {
return { status: HTTP_STATUS.CONFLICT };
}

return {
status: HTTP_STATUS.OK
}
await prisma.links.update({
where: {
id: linkId,
},
data: {
title: title,
short_code: shortcode,
},
});
await updatePrivateShortCode(link!.short_code, shortcode, link!.long_url);

}
catch(e){
return {
status: HTTP_STATUS.INTERNAL_SERVER_ERROR
}
}
return {
status: HTTP_STATUS.OK,
};
} catch (e) {
return {
status: HTTP_STATUS.INTERNAL_SERVER_ERROR,
};
}
}

export async function getTutorialStatus(){
export async function getTutorialStatus() {
const prisma = PrismaClientManager.getInstance().getPrismaClient();
const session: ISessionType | null = await getServerSession(authOptions);

const TutorialStatus = {
createLink : false,
clickLink : false,
checkAnalytics:false
}
createLink: false,
clickLink: false,
checkAnalytics: false,
};

if (!session?.user) {
return {
status: HTTP_STATUS.UNAUTHORIZED
}
status: HTTP_STATUS.UNAUTHORIZED,
};
}

const link:linkType | null = await prisma.links.findFirst({});
const link: linkType | null = await prisma.links.findFirst({});

if(link){
if (link) {
TutorialStatus.createLink = true;
}

// If engagement then check
// If checked Analytics thenc check

}
}
24 changes: 24 additions & 0 deletions lib/services/redisPublicGenerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ const invokeRedis = async (long_url: string): Promise<string> => {
return shortCode;
};

const setPrivateShortCode = async (
shortCode: string,
longUrl: string
): Promise<string> => {
await redis.set(shortCode, longUrl, "EX", 604800);
return shortCode;
};

const updatePrivateShortCode = async (
oldShortCode: string,
newShortCode: string,
longUrl: string
): Promise<string> => {
const checkIfUrlExists = await redis.get(oldShortCode);
if (checkIfUrlExists) {
await redis.del(oldShortCode);
return await setPrivateShortCode(newShortCode, longUrl);
} else {
return await setPrivateShortCode(newShortCode, longUrl);
}
};

const getLongUrl = async (shortCode: string): Promise<string | null> => {
return await redis.get(shortCode);
};
Expand Down Expand Up @@ -63,4 +85,6 @@ export {
getRecords,
checkIfShortCodePublic,
publishUserAgent,
setPrivateShortCode,
updatePrivateShortCode
};
Loading