diff options
author | Shav Kinderlehrer <[email protected]> | 2023-10-15 02:55:58 -0400 |
---|---|---|
committer | Shav Kinderlehrer <[email protected]> | 2023-10-15 02:55:58 -0400 |
commit | 504f845f110118574db416150e94ed5735b24c0f (patch) | |
tree | f7b21fa2ff3f08fd203e92f57ded3ecd215dc3a4 | |
parent | 96982b85e23af2a24841c3c44e598ae71f78abf6 (diff) | |
download | url-shortener-504f845f110118574db416150e94ed5735b24c0f.tar.gz url-shortener-504f845f110118574db416150e94ed5735b24c0f.zip |
Implement post
-rw-r--r-- | page/create.html | 26 | ||||
-rw-r--r-- | src/get.ts | 18 | ||||
-rw-r--r-- | src/index.ts | 7 | ||||
-rw-r--r-- | src/post.ts | 55 | ||||
-rw-r--r-- | src/reserved.ts | 3 |
5 files changed, 106 insertions, 3 deletions
diff --git a/page/create.html b/page/create.html new file mode 100644 index 0000000..33fbe71 --- /dev/null +++ b/page/create.html @@ -0,0 +1,26 @@ +<!DOCTYPE html> +<html> + +<head> + <title>trkt</title> +</head> + +<body> + <p> create link</p> + + <form action="/c" method="POST"> + <label for="url">Url:</label> + <input required type="url" id="url" name="url"> + + <br /> + + <label for="id">ID (optional):</label> + <input type="text" id="id" name="id"> + + <br /> + + <input type="submit" value="Submit"> + </form> +</body> + +</html> @@ -33,6 +33,14 @@ export async function get_id( if (!db_res.length) { return new Response(`url for id '${id}' not found.`, { status: 404 }); + } else { + await sql` + INSERT INTO tracking (id,clicks) + VALUES(${id},1) + ON CONFLICT ON CONSTRAINT tracking_id_key + DO UPDATE + SET clicks=tracking.clicks+1 + `; } const url = new URL(db_res[0]['url']); @@ -56,6 +64,14 @@ export async function get_id( res.headers.set("X-Message", "Okay I Like It, Picasso"); } - console.log(res); + return res; +} + +export async function get_create() { + const file = Bun.file("page/create.html"); + let res = new Response(await file.text()); + + res.headers.set("Content-Type", "text/html"); + return res; } diff --git a/src/index.ts b/src/index.ts index 119ea91..f20fee6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,18 @@ import { Elysia } from "elysia"; import postgres from "postgres"; -import { get_home, get_id } from "./get"; +import { get_home, get_id, get_create } from "./get"; +import { post_c } from "./post"; const sql = postgres(); const app = new Elysia() .get("/", ({ headers }) => get_home({ headers: headers })) - .get("/:id", async ({ params, headers, }) => get_id( + .get("/:id", ({ params, headers, }) => get_id( { params: params, headers: headers }, sql) ) + .get("/c", () => get_create()) + .post("/c", ({ body }) => post_c(body, sql)) app.listen(3000) diff --git a/src/post.ts b/src/post.ts new file mode 100644 index 0000000..538ff00 --- /dev/null +++ b/src/post.ts @@ -0,0 +1,55 @@ +import type { Sql } from "postgres"; + +import { reserved_ids } from "./reserved" + +export async function post_c(body: any, sql: Sql) { + let custom_id = true; + + let form = { + url: body['url'], + id: body['id'].trim() + } + + try { + form.url = new URL(form.url); + } catch (_) { + return new Response(`'${form.url}' is not a valid URL.`, { status: 400 }); + } + + if (!form.id.trim()) { + custom_id = false; + const hasher = new Bun.CryptoHasher("md5"); + hasher.update(form.url.href); + form.id = hasher.digest("hex").slice(0, 11); + } + + const valid_re = /^[A-z0-9_-]+$/; + if (!form.id.match(valid_re) || form.id.match(valid_re).length != 1) { + return new Response(`'${form.id}' must be of A-Z|a-z|0-9|_|-`, { status: 400 }); + } + + let db_id_exists = await sql` + SELECT * FROM urls + WHERE id=${form.id} + `; + + if (db_id_exists.length > 0 && custom_id) { + return new Response( + `custom id already exists.\ntrkt.in/${form.id} -> ${db_id_exists[0]["url"]}`, + { status: 409 } + ); + } + + if (reserved_ids.includes(form.id)) { + return new Response(`'${form.id}' is a reserved link.`, { status: 403 }); + } + + if (db_id_exists.length == 0) { + await sql` + INSERT INTO urls(id, url) + VALUES(${form.id}, ${form.url.href}) + `; + } + + return new Response(`trkt.in/${form.id} -> ${form.url.href}`); +} diff --git a/src/reserved.ts b/src/reserved.ts new file mode 100644 index 0000000..43ee3b7 --- /dev/null +++ b/src/reserved.ts @@ -0,0 +1,3 @@ +export const reserved_ids = [ + "c" +] |