add noDb version

This commit is contained in:
ackimixs
2024-01-29 19:18:31 +01:00
parent 8d298e1587
commit c7f2a050ea
3 changed files with 36 additions and 5 deletions

View File

@@ -5,10 +5,10 @@ Bun.serve({
port: Bun.env.PORT || 8080,
async fetch(req) {
const url = new URL(req.url);
if (req.method == "POST" && url.pathname == "/admin") {
if (req.method === "POST" && url.pathname === "/admin") {
if (req.body) {
const { urlData, password } = await req.json() as { urlData: string, password: string };
if (urlData && password == Bun.env.ADMIN_PASSWORD) {
if (urlData && password === Bun.env.ADMIN_PASSWORD) {
const u = await prisma.url.create({
data: {
url: urlData
@@ -21,7 +21,7 @@ Bun.serve({
}
}
}
else if (req.method == "GET") {
else if (req.method === "GET") {
const urlRes = await prisma.url.findFirst({
orderBy: {
updatedAt: "desc"

31
noDb.ts Normal file
View File

@@ -0,0 +1,31 @@
let urlToRedirect = Bun.env.URL_TO_REDIRECT || "https://instagram.com/modelec_isen";
Bun.serve({
port: Bun.env.PORT || 8080,
async fetch(req) {
const url = new URL(req.url);
if (req.method === "POST" && url.pathname === "/admin") {
if (req.body) {
const { u, password } = await req.json() as { u: string, password: string };
if (u && password === Bun.env.ADMIN_PASSWORD) {
urlToRedirect = u;
return new Response(JSON.stringify({body: u}), {
status: 200
})
}
}
}
else if (req.method === "GET") {
return new Response(null, {
status: 302,
headers: {
Location: urlToRedirect
}
})
}
return new Response("err", {
status: 400
})
}
})

View File

@@ -13,8 +13,8 @@
"@prisma/client": "5.8.1"
},
"scripts": {
"dev": "bun run index.ts",
"build": "bun build",
"dev": "bun run noDb.ts",
"dev:db": "bun run index.ts",
"prisma:migrate:dev": "bunx prisma migrate dev",
"prisma:migrate:generate": "bunx prisma generate",
"prisma:studio": "bunx prisma studio"