Cloudflare has recently released the Upstash Integration for the Cloudflare Workers. This makes it even easier to use Upstash products on Workers. In this blog post, we're going to use Upstash Redis integration to build a serverless URL shortener.
Getting Started
Here are the steps for building this project:
- Set up an Upstash database
- Create the Worker on Cloudflare
- Integrate Cloudflare Workers with Upstash
- Implement the project algorithm.
- Deploy your application
Creating the database
Navigate to Upstash console, and create yourself a Redis database by clicking the Create database button in the Redis section.
Just like that, your database is all set!
Creating the Cloudflare Worker
We're going to use the Cloudflare CLI tool to generate the Worker. It's a pretty simple process. Just open your terminal and type in the command below:
npm create cloudflare@latest
Executing this command will lead you through the options and configurations for the application. Finally, it will request you to log in to Cloudflare. Below is an example of my configuration and the resultant output:
We've created and deployed the application using a single line command. This command should have initialized a folder similar to this:
Enabling Upstash integration will be equally straightforward.
Cloudflare - Upstash Integration
Access to the Cloudflare Dashboard and login with the same account that you've created the url-shortener
application. Then, navigate to the Workers & Pages > Overview section of the sidebar. Here, you will find the url-shortener
application listed.
Clicking on the application will direct you to the application details page, where we will perform the integration process. Switch to the Settings tab in the application details, and proceed to Integrations section. You will see various Worker integrations listed. To proceed, click the Add Integration button associated with the Upstash Redis.
On the Integration page, connect to your Upstash account. Then, select the url-shortener
database from the dropdown menu. Finalize the process by pressing the Save button.
Note: Please don't make any changes in the secret names on the Configure secrets step. These credentials will be automatically picked up by Redis.fromEnv(env)
line in the code as UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
.
Implementing URL Shortener
Our implementation will be a rather simple one. It will have two functionalities:
-
When a URL is sent using the POST method, a randomly generated key will be assigned to that URL. This key-URL pair will be stored in Redis for retrieval. The key will be appended as a path to the Worker URL and sent to the user in the
<YOUR_WORKER_URL>/<KEY>
format. -
When a user accesses the Worker URL with a previously added key as a path, the corresponding value of the key will be retrieved from Redis. The user will then be redirected to this URL with a
307
response code.
We will implement the application in the src/worker.ts
file. Below is the basic implementation of our URL Shortener:
Now that our algorithm is ready, there's only one step left: deploying the application. Navigate to url-shortener
folder from the terminal, and run the command below:
Our Cloudflare Worker is now up and running! You can test it either in the application dashboard, or via a curl request. Here's an example request for you. You can replace the URL with your Worker URL, or test with my implementation:
Clicking on the response URL will redirect you to https://google.com.
Conclusion
Thank you for following along the tutorial!
As you've discovered, the combination of serverless functions and the power of Upstash Redis opens up a world of possibilities for your projects. The versatility and efficiency of this pair empower you to create solutions that are not only innovative but also remarkably responsive.
Remember, this guide is just the beginning. There's no limit to how you can expand and improve this project.
Should you have any questions or problems, feel free to reach out to me at fahreddin@upstash.com.
Happy coding!