Skip to content
/ shrtn Public

A dead-simple Cloudflare Workers URL shortener / redirector

Notifications You must be signed in to change notification settings

rpj/shrtn

Repository files navigation

shrtn

A dead simple URL shortener built with Cloudflare Worker & KV Store.

Deployment

  1. Create a new CF project: npm create cloudflare@latest -- <YOUR_PROJECT_NAME>
  2. Clone this project into that new directory.
  3. Create three new KV namespaces named shrtn-config, shrtn-redirects & shrtn-redirects-rev: npx wrangler kv namespace create <NAMESPACE_NAME>.
    • If using authorization, create a fourth named shrtn-auth.
  4. Copy the three-line [[kv_namespaces]] blocks for each of the above into your wrangler.toml.
  5. Deploy! npm run deploy

Usage

Upon successful code creation, the returned Content-type is: text/html for GET or application/json for POST.

GET can still receive JSON by setting Accept to application/json.

GET

/+..., where ... is the URL to shorten.

With authorization enabled add the authorization credentials (base64-encoded user:pass) as the first path parameter: /<authorizationCredentials>/+....

You can also pass the traditional Authorization header instead.

POST

URLs with fragments (#...) cannot be created with above method: instead POST to /add with the URL to be shortened as the body (plaintext).

The Authorization header is required with authorization enabled.

Configurables

These may be set in the shrtn-config namespace and if they are, their values will override the defaults found at the top of config.ts.

  • ID_ALPHABET: the character alphabet to use for IDs
  • ID_SMALLEST_SIZE: the minimum length, in characters, of IDs
  • CODE_CREATE_SUCCESS_MUSTACHE_HTML: Mustache template for the HTML shown upon successful code creation & the right Accept header.

Authorization

Toggle AUTH_ENABLED in index.ts to enable/disable authorization. Doing so requires redeployment.

The shrtn-auth KV namespace should have usernames as keys and passwords as values.

About

A dead-simple Cloudflare Workers URL shortener / redirector

Topics

Resources

Stars

Watchers

Forks