flowchart TD
api([API Clients])
browser([Browser])
worker(<a href='https://developers.cloudflare.com/workers/'>Worker</a>):::cf
kv[(<a href='https://developers.cloudflare.com/workers/runtime-apis/kv/'>KV</a>)]:::cf
cache[(<a href='https://developers.cloudflare.com/workers/runtime-apis/cache/'>Cache</a>)]:::cf
classDef cf stroke:#FFC500,stroke-width:2px
subgraph Cloudflare
worker
kv
cache
end
kv <--> worker
cache <--> worker
worker <--> api
worker <--> browser
Designing a URL shortener such as TinyURL and Bitly is one of the most common System Design interview questions in software engineering.
While meddling around with Cloudflare Worker, it gave me an idea to build an actual URL shortener that can be used by anyone.
This is a proof of concept (POC) of how one builds an actual URL shortener service using serverless computing.
- Get a Cloudflare account
- Install Wrangler CLI for Cloudflare Workers deployment
Check out Steps 1 to 3 of this Get Started Guide to setup a Cloudflare account.
Read more about configuring wrangler.toml.
This project is initialized using the itty-router worker template.
npm ciSince we're using KV as our storage, we need to first create it.
wrangler kv:namespace create "URL_DB"
wrangler kv:namespace create "URL_DB" --previewFor creating these KV namespaces, remember to update your wrangler.toml file to include the namespace bindings accordingly.
-
This project uses Wrangler actions to automatically publish worker. To do so, add
CF_API_TOKENinto your GitHub repository secrets. You can create your API token using theEdit Cloudflare Workerstemplate. -
This project also uses semantic-release to automatically publish to NPM. To enable this, you will need to create a
NPM_TOKENvia npm create token and add it to your GitHub repository secrets.
To try out the project locally, simply run wrangler dev.
NOTE The deployment of this project is done using GitHub Actions CI/CD. For more details, check out ci.yml and this article.
-
Before publishing your code you need to edit
wrangler.tomlfile and add your Cloudflareaccount_id- more information about configuring and publishing your code can be found in the documentation. -
Create 2 KV and update your
wrangler.tomlfile to include the namespace bindings accordingly. -
To publish any new changes to your Cloudflare Worker, run
wrangler publish -e <env>where<env>couldstagingorproduction.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
- Fork this
- Create your feature branch (
git checkout -b jimmy/fooBar) - Code
- Commit your changes (
git commit -am 'feat: add some fooBar', make sure that your commits are semantic) - Push to the branch (
git push origin jimmy/fooBar) - Create a new Pull Request