Skip to content

A fully serverless URL shortener with link expiry, view counts, & more. Built using Cloudflare Workers & KV.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENCE-APACHE
MIT
LICENCE-MIT
Notifications You must be signed in to change notification settings

Blooym/workerlink

Repository files navigation

Workerlink

A fully serverless URL shortener built on Cloudflare Workers & Cloudflare KV in under ~500 lines of Rust.

Project status: Completed & Maintained

Deployment

In order to deploy workerlink to Cloudflare Workers, you need to do the following:

  1. Clone this repository locally with git or by downloading the source archive.
  2. Download and install NodeJS and Rust v1.75.0+, or use the provided .devcontainer setup for an environment.
  3. Run npm install to install all dependencies needed to build/deploy.
  4. Setup a KV namespace on Cloudflare by following their documentation.
  5. Create a 'wrangler.toml' file with the following contents at the root of the repository:
    name = "workerlink"
    main = "build/worker/shim.mjs"
    compatibility_date = "2024-04-01"
    kv_namespaces = [
        { binding = "links", id = "<KV ID>" } # Replace <KV ID> with the ID of the KV namespace you setup earlier.
    ]
    
    [vars]
    AUTH_TOKEN = "" # Set this to the token you want to use for authentication.
    
    [build]
    command = "cargo install -q worker-build && worker-build --release"
  6. Run npm run deploy to deploy the worker to Cloudflare; You will be prompted to authenticate with Cloudflare during this process so the worker can be deployed using your account.

Examples

  • In a browser: Use a redirect.
    Navigate to https://<WORKER_URL>/<ID> and the browser will automatically direct.

  • In a browser: See where an ID redirects to.
    Navigate to https://<WORKER_URL>/<ID>/where and the redirect url will be shown in plaintext.

  • Using curl: Create/Update a new redirect.

    curl --request POST \
      --url 'https://<WORKER_URL>/<ID>' \
      --header 'Authorization: <AUTH_TOKEN>' \
      --header 'content-type: application/json' \
      --data '{
      "url": "<URL_TO_REDIRECT_TO>",
      "expiry_timestamp": unix_timestamp | null,
      "max_views": number | null,
      "overwrite": boolean,
      "disabled": boolean
    }'
  • Using curl: Delete an existing redirect.

    curl --request DELETE \
      --url 'https://<WORKER_URL>/<ID>' \
      --header 'Authorization: <AUTH_TOKEN>'
  • Using curl: Check the underlying JSON of a redirect.

    curl 'https://<WORKER_URL>/<ID>/details' \
        --header 'Authorization: <AUTH_TOKEN>'

Licence

This project is dual-licenced under both the MIT Licence and the Apache Licence (Version 2.0). See LICENCE-MIT and LICENCE-APACHE for more details.

About

A fully serverless URL shortener with link expiry, view counts, & more. Built using Cloudflare Workers & KV.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENCE-APACHE
MIT
LICENCE-MIT

Stars

Watchers

Forks