Skip to content

coderxi1/cloudflare-worker-url-shortner

Repository files navigation

cloudflare-worker-url-shortner

cloudflare-worker-url-shortner is a serverless URL shortening service built using Cloudflare Workers. It provides a simple and efficient way to shorten long URLs and manage redirects.

Important

This project was developed using Vue out of consideration for its flexibility, reactivity, and robust ecosystem. If you prefer a more lightweight version, please refer to the lite branch.

preview

Features

  • 😀 Simple and clean style
  • 🗄️ Uses Cloudflare KV for persistent storage
  • 🔗 Custom short URLs support
  • 📡 Simple API for creating short links

Prerequisites

  • 🌐 A Cloudflare account
  • 🗄️ Install Wrangler and login
    npm install -g wrangler
    wrangler login

Installation

  1. Create KV namespace step1
  2. Remember KV namespace ID step2
  3. Clone this repo and
    git clone https://github.com/coderxi1/cloudflare-worker-url-shortner
  4. Install dependencies
    pnpm install
  5. Change PASSWORD and and KV namespace 's ID in wrangler.jsonc
    {
      "vars": {
        "PASSWORD": "<YOUR_PASSWORD>", //custom your password *
        "PASSWORD_SECRET_KEY": "<YOUR_SECRET_KEY>", //custom your secret key *
      },
      "kv_namespaces": [
          {
            "binding": "URLS",
            "id": "<YOUR_KV_NAMESPACE_ID>"  //change to your KV namespace ID *
          }
        ]
    }
  6. Deploy to cloudflare
    pnpm run deploy

Environment variables

Key Default Value Description
PASSWORD <YOUR_PASSWORD> The password for authentication.
PASSWORD_SECRET_KEY <YOUR_SECRET_KEY> The secret key for jwt.
PASSWORD_REQUIRE true If set to true, password must be entered to use all features.
HOST_WHITELIST ["example.com", "coderxi.com"] A list of allowed hostnames. Only requests from these domains are accepted unless a valid password is provided.
KEY_MIN_LENGTH 6 The minimum length required for the key.
KEY_REMOVE false If set to true, the URL record will be deleted when expired. If false, the URL will only be marked as expired

API Usage

Create a Short URL

Request:

POST /
Content-Type: application/json
{
  "url": "https://coderxi.com/posts/url-shortener-with-cloudflare-worker",
  "password": "<YOUR_PASSWORD>",
  "key": "doc",
  "expireDay": 1
}

Response:

{
  "key": "doc"
}

About

A serverless URL shortening service built using Cloudflare Workers. 利用Cloudflare Workers部署的短链接服务.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors