This commit is contained in:
ackimixs
2024-04-13 17:47:50 +02:00
parent f38608f1fb
commit fd2c850231

View File

@@ -6,20 +6,24 @@ db.query("CREATE TABLE IF NOT EXISTS Url (url TEXT)").run();
Bun.serve({
port: Bun.env.PORT || 8080,
async fetch(req) {
async fetch(req, server) {
const url = new URL(req.url);
console.log(new Date().toISOString(), req.method, " | ", url.pathname, " | ", server.requestIP(req)?.address)
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) {
if (!isUrl(u)) {
return new Response(JSON.stringify({err: "Invalid URL"}), {
status: 400
});
}
console.log("User add : ", u)
const query = db.query("INSERT INTO Url (url) VALUES ($u) RETURNING *");
const results = query.get({
@@ -68,6 +72,8 @@ Bun.serve({
const { u, password } = await req.json() as { u: string, password: string };
if (u && password === Bun.env.ADMIN_PASSWORD) {
console.log("User remove : ", u)
if (!isUrl(u)) {
return new Response(JSON.stringify({err: "Invalid URL"}), {
status: 400
@@ -112,8 +118,6 @@ process.on("exit", async () => {
db.close();
});
const isUrl = (url: string) : boolean => {
try {
new URL(url);