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.
├──── 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
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.
In order to run the project locally you need node>=14
and npm>=6
.
Make sure you have the latest version of wrangler
as described in the wrangler docs.
npm uninstall -g @cloudflare/wrangler &&
npm install -g @cloudflare/wrangler
cargo install wrangler --force
Make sure you have a valid deployment token by doing:
wrangler login
or:
wrangler config
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
To successfully run the redirect worker we need to set up some KV namespaces.
Generate new namespace_id
s 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
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
To develop locally just run:
npm start
To deploy to dev
just run:
npm run deploy
To monitor any of the deployed environments run:
wrangler tail
This library is released under BSD-3 license clause.