Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions netlify/edge-functions/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { config } from "https://deno.land/x/dotenv@v3.2.2/mod.ts";

export function getConfig() {
const env = config();

return {
SUPABASE_URL: env.SUPABASE_URL || Deno.env.get("SUPABASE_URL") || "",
SUPABASE_PASSWORD:
env.SUPABASE_PASSWORD || Deno.env.get("SUPABASE_PASSWORD") || "",
SUPABASE_ANON_KEY:
env.SUPABASE_ANON_KEY || Deno.env.get("SUPABASE_ANON_KEY") || "",
URLSHORT_URL_BASE:
env.URLSHORT_URL_BASE || Deno.env.get("URLSHORT_URL_BASE") || "",
URLSHORT_TRACK_CLICKS:
env.URLSHORT_TRACK_CLICKS ||
Deno.env.get("URLSHORT_TRACK_CLICKS") ||
false,
URLSHORT_RESOLVE_HOSTNAME:
env.URLSHORT_RESOLVE_HOSTNAME ||
Deno.env.get("URLSHORT_RESOLVE_HOSTNAME") ||
false,
};
}

export default getConfig;
10 changes: 6 additions & 4 deletions netlify/edge-functions/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import handler from "./utils.ts";
const { fetchFromSupabase, logClick, validateIpAddress } = handler();

const trackClicks = Deno.env.get("URLSHORT_TRACK_CLICKS") || false;
import { getConfig } from "./config.ts";
const { URLSHORT_TRACK_CLICKS, URLSHORT_RESOLVE_HOSTNAME } = getConfig();
console.log({ URLSHORT_TRACK_CLICKS, URLSHORT_RESOLVE_HOSTNAME });
/**
* Handles redirection for shortened URLs, logs the click, IP address, and hostname.
*
Expand Down Expand Up @@ -33,14 +34,14 @@ export default async (
const urlId = data[0].id;
const longUrl = data[0].long_url;

if (trackClicks) {
if (URLSHORT_TRACK_CLICKS) {
// Extract IP address from connInfo
const addr = connInfo.remoteAddr as Deno.NetAddr;
const ip = addr?.hostname || "";
let ipAddress = ip || request.headers.get("x-forwarded-for") || "";

let hostname = "";
if (validateIpAddress(ipAddress)) {
if (URLSHORT_RESOLVE_HOSTNAME && validateIpAddress(ipAddress)) {
try {
hostname = await getHostnameFromIp(ipAddress);
} catch (error) {
Expand Down Expand Up @@ -93,6 +94,7 @@ export async function getHostnameFromIp(ip: string): Promise<string> {
return hostnames[0];
} else {
//throw new Error(`No hostname found for IP: ${ip}`);
return "unknown";
console.error(`No hostname found for IP: ${ip}`);
}
} catch (error) {
Expand Down
10 changes: 5 additions & 5 deletions netlify/edge-functions/shorten.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
import { isURL } from "https://deno.land/x/is_url/mod.ts";

import { headers } from "./headers.ts";
import handler from "./utils.ts";
const { generateShortUrl } = handler();

const urlBase = Deno.env.get("URLSHORT_URL_BASE") || "";
import { getConfig } from "./config.ts";
const { URLSHORT_URL_BASE } = getConfig();

/**
* Shortens a given long URL and stores it in Supabase.
Expand Down Expand Up @@ -48,9 +47,10 @@ export default async (request: Request): Promise<Response> => {
headers,
});
}

console.log("url", url);
let shortUrl = await generateShortUrl(url);
shortUrl = urlBase + shortUrl;
console.log("shortUrl", shortUrl);
shortUrl = URLSHORT_URL_BASE + shortUrl;

return new Response(JSON.stringify({ shortUrl }), { status: 200, headers });
} catch (error) {
Expand Down
13 changes: 7 additions & 6 deletions netlify/edge-functions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const SUPABASE_URL = Deno.env.get("SUPABASE_URL");
const SUPABASE_KEY = Deno.env.get("SUPABASE_ANON_KEY");
import { getConfig } from "./config.ts";
const { SUPABASE_URL, SUPABASE_ANON_KEY } = getConfig();

/**
* Fetches data from Supabase.
Expand All @@ -22,8 +22,8 @@ export async function fetchFromSupabase(
...options,
headers: {
...options.headers,
apikey: SUPABASE_KEY,
Authorization: `Bearer ${SUPABASE_KEY}`,
apikey: SUPABASE_ANON_KEY,
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
},
});

Expand Down Expand Up @@ -60,8 +60,8 @@ export async function writeToSupabase(endpoint: string, options: RequestInit) {
...options,
headers: {
...options.headers,
apikey: SUPABASE_KEY,
Authorization: `Bearer ${SUPABASE_KEY}`,
apikey: SUPABASE_ANON_KEY,
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
},
});

Expand Down Expand Up @@ -104,6 +104,7 @@ export async function generateShortUrl(longUrl: string): Promise<string> {

let shortUrl: string;
let isCollision = true;

while (isCollision) {
shortUrl = generateUuidShort();
const collisionData = await fetchFromSupabase(
Expand Down