/cf-workers-url-shortener

Social URL links redirect service powered by Cloudflare Workers and KV. Features customizable landing page, 404 status page for missing redirects, and one-click deploy!

Primary LanguageHandlebarsBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

workers-url-shortener

Commitizen Friendly License FOSSA Status

Maintainability Known Vulnerabilities

Overview

A Cloudflare Workers script to use as a simple redirect system.

The data.json file can be used as dummy input or git enabled backup for your production environment.

The motivation for doing such a thing is GUI or IAM enabled acces to these variables from other CloudFlare tools, essentially promoting observability.

Folder structure

├──── workers-url-shortener
│  ├── .github/
│  ├── src/
│  ├── static/
│  ├── .editorconfig
│  ├── .eslintrc.js
│  ├── .gitattributes
│  ├── .gitignore
│  ├── .lintstagedrc.js
│  ├── .npmrc
│  ├── CODE_OF_CONDUCT.md
│  ├── LICENSE
│  ├── npm-shrinkwrap.json
│  ├── package.json
│  ├── README.md
│  └── wrangler.toml

Deploy as Cloudflare Worker

I use this service for my profile at github.com. Currently, the service is hosted on a free tier of Cloudflare Workers and limited at 100K requests per day. Make sure to make the appropriate changes in wrangler.toml first.

Deploy to Cloudflare Workers

Requirements

In order to run the project locally you need node>=14 and npm>=6.

Install @cloudflare/wrangler

Make sure you have the latest version of wrangler as described in the wrangler docs.

Updating Wrangler with NPM:

npm uninstall -g @cloudflare/wrangler && 
  npm install -g @cloudflare/wrangler 

Install with cargo

cargo install wrangler --force

Get a valid CF_API_TOKEN

Make sure you have a valid deployment token by doing:

wrangler login 

or:

wrangler config 

Generate a new repository

Create a new GitHub repository with the green button or clone:

# with git
git clone https://github.com/0-vortex/workers-url-shortener.git

or with github-cli:

# with github-cli
gh repo clone  0-vortex/workers-url-shortener

Usage

Configure KV

To successfully run the redirect worker we need to set up some KV namespaces.

Generate new namespace_ids for the KV keys it wrangler.toml and follow the instructions:

# dev environment
wrangler kv:namespace create "REDIRECTS"
wrangler kv:namespace create "REDIRECTS" --preview
# production environment
wrangler kv:namespace create "REDIRECTS" --env production
wrangler kv:namespace create "REDIRECTS" --env production --preview

After you are done editing check if the changes are correct:

wrangler kv:namespace list

Test data

Upload some data to the REDIRECTS namespace:

# dev environment
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json --preview
# production environment
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json --env production
wrangler kv:bulk put --binding="REDIRECTS" ./src/data.json --env production --preview

Local development

To develop locally just run:

npm start

To deploy to dev just run:

npm run deploy

Monitoring

To monitor any of the deployed environments run:

wrangler tail

License

This library is released under BSD-3 license clause.

FOSSA Status