A fully serverless URL shortener built on Cloudflare Workers & Cloudflare KV in under ~500 lines of Rust.
Project status: Completed & Maintained
In order to deploy workerlink to Cloudflare Workers, you need to do the following:
- Clone this repository locally with git or by downloading the source archive.
- Download and install NodeJS and Rust v1.75.0+, or use the provided .devcontainer setup for an environment.
- Run
npm install
to install all dependencies needed to build/deploy. - Setup a KV namespace on Cloudflare by following their documentation.
- 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-09-02" 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"
- 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.
-
In a browser: Use a redirect.
Navigate tohttps://<WORKER_URL>/<ID>
and the browser will automatically direct. -
In a browser: See where an ID redirects to.
Navigate tohttps://<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>'
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.