mirror of
https://github.com/modelec/urlsh-qrcode.git
synced 2026-01-18 16:57:25 +01:00
log
This commit is contained in:
12
index.ts
12
index.ts
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user