/eastlake

A URL shortener based on Cloudflare Workers

Primary LanguageTypeScript

East Lake Short URL System

A serverless URL shortener based on Cloudflare Workers and Vercel.

A fork of Likenttt/eastlake-cloudflare-worker-short-url. Read the original README file. 阅读原始的“README”文件。

Features

Frontend Admin Page (Next.js Project deployed on Vercel)

  • Authentication
  • List shortened URL data on the KV database
  • Click counter for a specific URL

Backend API Server and KV (Cloudflare Worker/KV)

  • Correctly and swiftly redirect a short URL to its original long URL.
  • Monitor click history per link
    • Optional: if enabled, it will consume more read/write times for Cloudflare KV. If you are on a paid plan, disregard since the plan has unlimited read/write times.
  • Expiration time
  • Password protection
  • 404 Not Found fallback page

Prerequisites

You must have:

  1. A Vercel account.
  2. A Cloudflare account.
  3. A domain on Cloudflare.

Deployment

Fork this repository and clone it to your local machine. Then start the deployment process.

Frontend Admin

The frontend code is in /fe, which is a Next.js project. Vercel is highly recommended.

Deploy with Vercel

Add the env variable as shorturl also backend server endpoint Change the default root Redeployment

Backend Server

Initialize Cloudflare KV namespace

  • Create a Cloudflare KV namespace named LINKS.
  • Populate the namespace ID in wrangler.toml below.
[[kv_namespaces]]
binding = "LINKS" # Don't change
id = "uuid" # Change

Create namespace Get namespace id

Create a worker service

Create a worker service

Add environment variables.

Create Environment Variables

Don't expose your variables in the wrangler.toml file. Exclude this file in future commits.

USERNAME = "li2niu" # Change it and don't expose it in this file
PASSWORD = "li2niu" # Change it and don't expose it in this file
JWT_SECRET = "li2niu" # Change it and don't expose it in this file
DEFAULT_PAGE = "https://blog.li2niu.com" # Change it
RECORD_CLICKS = true
FE_ADMIN_DOMAIN = "https://cf-url-admin.li2niu.com" # Replace it with your admin domain, important for cross-origin allowlist

Clone your repo to your local machine

git clone <forked repository>

Install Wrangler CLI Locally and Log in to Cloudflare

npm install -g wrangler
wrangler login

Deploy

If you have yarn installed globally, meaning you have a file located at ~/.pnp.cjs, rename the file to something else temporarily as Babel will complain about Plug'n'Play when wrangler runs the build script.

cd worker
wrangler publish

Cloudflare Free Plan Limits

Cloudflare free plan has limits for KV, especially write times. Even in a paid plan, the write speed for the same key is limited to 1 time/second, potentially causing inaccuracies in high concurrency. Click history is not recommended for free plan users.

Support the Original Creator

Buy Me A Coffee