From 97ce6575fc4f113c1a1c05c66de93026b851d5f0 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Thu, 11 Apr 2024 22:51:06 +0200 Subject: [PATCH] random --- index.ts | 71 ++++++++++++++++++++++++++++++++++++++++++-------------- noDb.ts | 51 ++++++++++++++++++++++++++++++++-------- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/index.ts b/index.ts index 127f40a..374f053 100644 --- a/index.ts +++ b/index.ts @@ -5,29 +5,64 @@ 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 { urlData, password } = await req.json() as { urlData: string, password: string }; - if (urlData && password === Bun.env.ADMIN_PASSWORD) { - const u = await prisma.url.create({ - data: { - url: urlData - } - }) + if (req.method === "POST") { + if (url.pathname === "/admin") { + if (req.body) { + const { urlData, password } = await req.json() as { urlData: string, password: string }; + if (urlData && password === Bun.env.ADMIN_PASSWORD) { + const u = await prisma.url.create({ + data: { + url: urlData + } + }) - return new Response(JSON.stringify({body: u}), { - status: 200 - }) + return new Response(JSON.stringify({body: u}), { + status: 200 + }) + } + } + } else if (url.pathname == "/admin/clear") { + if (req.body) { + const { password } = await req.json() as { password: string }; + if (password === Bun.env.ADMIN_PASSWORD) { + await prisma.url.deleteMany(); + return new Response(JSON.stringify({body: "ok"}), { + status: 200 + }) + } + } + } else if (url.pathname == "/admin/list") { + if (req.body) { + const {password} = await req.json() as { password: string }; + if (password === Bun.env.ADMIN_PASSWORD) { + const urls = await prisma.url.findMany(); + return new Response(JSON.stringify({body: urls}), { + status: 200 + }) + } + } + } else if (url.pathname == "/admin/remove") { + if (req.body) { + const { u, password } = await req.json() as { u: string, password: string }; + if (u && password === Bun.env.ADMIN_PASSWORD) { + await prisma.url.deleteMany({ + where: { + url: u + } + }) + return new Response(JSON.stringify({body: u}), { + status: 200 + }) + } } } } else if (req.method === "GET") { - const urlRes = await prisma.url.findFirst({ - orderBy: { - updatedAt: "desc" - } - }); - + // random + const urls = await prisma.url.findMany(); + + const urlRes = urls[Math.floor(Math.random() * urls.length)]; + if (urlRes) { return new Response(null, { status: 302, diff --git a/noDb.ts b/noDb.ts index e4e1dc9..8dbd01c 100644 --- a/noDb.ts +++ b/noDb.ts @@ -1,18 +1,49 @@ -let urlToRedirect = "https://instagram.com/modelec_isen"; +let listOfUrl = ["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; + if (req.method === "POST") { + if (url.pathname === "/admin/add") { + if (req.body) { + const { u, password } = await req.json() as { u: string, password: string }; + if (u && password === Bun.env.ADMIN_PASSWORD) { + listOfUrl.push(u); - return new Response(JSON.stringify({body: u}), { - status: 200 - }) + return new Response(JSON.stringify({body: u}), { + status: 200 + }) + } + } + } else if (url.pathname == "/admin/clear") { + if (req.body) { + const { password } = await req.json() as { password: string }; + if (password === Bun.env.ADMIN_PASSWORD) { + listOfUrl = []; + return new Response(JSON.stringify({body: "ok"}), { + status: 200 + }) + } + } + } else if (url.pathname == "/admin/list") { + if (req.body) { + const {password} = await req.json() as { password: string }; + if (password === Bun.env.ADMIN_PASSWORD) { + return new Response(JSON.stringify({body: listOfUrl}), { + status: 200 + }) + } + } + } else if (url.pathname == "/admin/remove") { + if (req.body) { + const { u, password } = await req.json() as { u: string, password: string }; + if (u && password === Bun.env.ADMIN_PASSWORD) { + listOfUrl = listOfUrl.filter((url) => url !== u); + return new Response(JSON.stringify({body: u}), { + status: 200 + }) + } } } } @@ -20,7 +51,7 @@ Bun.serve({ return new Response(null, { status: 302, headers: { - Location: urlToRedirect + Location: listOfUrl[Math.floor(Math.random() * listOfUrl.length)] } }) }