-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make link shortener selfhostable
- Loading branch information
Showing
6 changed files
with
42 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DOMAIN=onlineonlineonlineonline.online |
This file contains 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
This file contains 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
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}` }); | ||
} |
This file contains 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
This file contains 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