Skip to content

Commit

Permalink
feat: make link shortener selfhostable
Browse files Browse the repository at this point in the history
  • Loading branch information
crnvl committed Oct 27, 2023
1 parent 1d4e2c2 commit 1bceabe
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 54 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DOMAIN=onlineonlineonlineonline.online
18 changes: 0 additions & 18 deletions README.md

This file was deleted.

6 changes: 4 additions & 2 deletions pages/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import axios from "axios";
export async function getServerSideProps(context: any) {
const key = context.query.id.toString();

const res = await axios.post("https://qwq.sh/api/redirect", { key: key });
const domain = process.env.DOMAIN;
const res = await axios.post(`https://${domain}/api/redirect`, { key: key });

if (res.status !== 200) {
return {
Expand All @@ -24,6 +25,7 @@ export async function getServerSideProps(context: any) {
}

const Redirect: NextPage = () => {
let domain = process.env.DOMAIN;
return (
<>
<div className="flex w-screen h-screen justify-center items-center bg-black text-white">
Expand All @@ -34,7 +36,7 @@ const Redirect: NextPage = () => {
might be invalid.
</p>
<p className="text-custom-400 font-bold py-8">
<Link href="/">qwq.sh</Link>
<Link href="/">{domain}</Link>
</p>
</div>
</div>
Expand Down
61 changes: 31 additions & 30 deletions pages/api/create.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import ShortUrl from '../../models/shortUrl';
import { connectDB } from '../../utils/connect';
import { generateKey } from '../../utils/keyUtils';
import type { NextApiRequest, NextApiResponse } from "next";
import ShortUrl from "../../models/shortUrl";
import { connectDB } from "../../utils/connect";
import { generateKey } from "../../utils/keyUtils";

type Data = SucceededData | FailedData
type Data = SucceededData | FailedData;

type SucceededData = {
url: string
}
url: string;
};
type FailedData = {
error_message: string
}
error_message: string;
};

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
req: NextApiRequest,
res: NextApiResponse<Data>
) {
res.setHeader('Access-Control-Allow-Origin', '*');
if (req.method !== "POST") {
res.status(405).json({ error_message: 'Method not allowed' });
return;
}
res.setHeader("Access-Control-Allow-Origin", "*");
if (req.method !== "POST") {
res.status(405).json({ error_message: "Method not allowed" });
return;
}

await connectDB()
const data = req.body;
await connectDB();
const data = req.body;

if (!data || !data.url) {
res.status(400).json({ error_message: 'Invalid URL' });
return;
}
if (!data || !data.url) {
res.status(400).json({ error_message: "Invalid URL" });
return;
}

let keyLength = 5;
let key = generateKey(keyLength);
let keyLength = 5;
let key = generateKey(keyLength);

while (await ShortUrl.findOne({ key: key })) {
keyLength++;
key = generateKey(keyLength);
}
while (await ShortUrl.findOne({ key: key })) {
keyLength++;
key = generateKey(keyLength);
}

const shortUrl = await ShortUrl.create({ url: data.url, key: key });
const shortUrl = await ShortUrl.create({ url: data.url, key: key });

res.status(200).json({ url: `https://qwq.sh/${shortUrl.key}` });
let domain = process.env.DOMAIN;
res.status(200).json({ url: `https://${domain}/${shortUrl.key}` });
}
5 changes: 3 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ const Home: NextPage = () => {

const [isZeroWidth, setIsZeroWidth] = useState(false);

let domain = process.env.DOMAIN;
const createShortUrl = async (e: any) => {
e.preventDefault();
const res = await axios.post("https://qwq.sh/api/create", {
const res = await axios.post(`https://${domain}/api/create`, {
url: (document.getElementById("input") as HTMLInputElement).value,
isZeroWidth: isZeroWidth,
});
Expand Down Expand Up @@ -130,7 +131,7 @@ const Home: NextPage = () => {
return (
<>
<Head>
<title>qwq.sh</title>
<title>{domain}</title>
<meta name="description" content="Easy to use & free URL Shortener." />
<link rel="icon" href="/favicon.ico" />
</Head>
Expand Down
5 changes: 3 additions & 2 deletions pages/tracking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const Tracking: NextPage = () => {
const [clicks, setClicks] = useState("N/A");
const [lastClicked, setLastClicked] = useState("N/A");

let domain = process.env.DOMAIN;
const getTrackingInfo = async () => {
const res = await axios.post("https://qwq.sh/api/track", {
const res = await axios.post(`https://${domain}/api/track`, {
key: (document.getElementById("input") as HTMLInputElement).value,
});
const json = await res.data;
Expand Down Expand Up @@ -77,7 +78,7 @@ const Tracking: NextPage = () => {
return (
<>
<Head>
<title>qwq.sh</title>
<title>{domain}</title>
<meta name="description" content="Track your URLs." />
<link rel="icon" href="/favicon.ico" />
</Head>
Expand Down

0 comments on commit 1bceabe

Please sign in to comment.