Skip to content

ujayden/cf-url-shortener

Repository files navigation

Thanks to this project for the original implementation, which served as a great starting point for this enhanced version.

Features

  • Fast and reliable: Powered by Cloudflare's global edge network
  • 🔒 Secure: HTTPS by default with Cloudflare
  • 🔄 Edge caching: Optimized performance with built-in caching
  • 🎨 Clean UI: Modern, responsive web interface
  • 🔧 Customizable slugs: Create custom short URLs
  • ⏱️ TTL support: Set expiration time for links
  • 🌐 Open source: Modify and extend as needed

How It Works

This URL shortener uses Cloudflare Workers as the serverless runtime, Hono.js for API routing, and Cloudflare KV for storing URL mappings. When a user visits a shortened URL, the service looks up the original URL in KV storage and redirects the user with a 301 redirect.

Installation

Prerequisites

  • Node.js (v14 or newer)
  • Wrangler CLI - Cloudflare Workers CLI
  • A Cloudflare account with Workers subscription

Setup

  1. Clone this repository:

    git clone https://github.com/yourusername/modern-cf-shortener.git
    cd modern-cf-shortener
  2. Install dependencies:

    npm install
  3. Create a KV namespace for storing shortened URLs:

    wrangler kv:namespace create SHORTEN
  4. Update your wrangler.toml with the KV namespace ID (see wrangler_example.toml):

    name = "cf-url-shortener"
    main = "worker.js"
    compatibility_date = "2023-06-06"
    
    kv_namespaces = [
      { binding = "SHORTEN", id = "your-namespace-id-from-step-3" }
    ]

Development

Start local development server:

npm run start

This will start a local development server at http://localhost:8787 where you can test your URL shortener.

Deployment

Deploy to Cloudflare Workers:

npm run deploy

Usage

Web Interface

Access the web interface at: https://your-worker-domain.workers.dev/addLink

API Usage

Create Short URL

// Example API call to create a short URL
const response = await fetch('https://your-worker-domain.workers.dev/createShortURL', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    url: 'https://example.com/very-long-url',
    slug: 'custom-slug',  // Optional
    ttl: 86400           // Optional: TTL in seconds, or "never"
  })
});

const data = await response.json();
console.log(data.shortenedURL);

Configuration

The worker.js file contains several configuration options:

// Default settings
const DEFAULTS = {
  ttl: 86400, // 1 day in seconds
  slugLength: 6,
};

// Server Configuration
const SERVERCONFIG = {
  webGUIpath: 'addLink', // e.g. yourdomain.com/addLink
};

Explaining of Configuration

  • ttl: Default time-to-live for shortened URLs in seconds (default is 1 day).
  • slugLength: Length of the generated slug for short URLs (default is 6 characters).

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages