This guide provides a comprehensive, step-by-step walkthrough for deploying the entire SnapURL project, including the core URL shortener, the API worker, the web UI, and the required Cloudflare Access configuration.
The project consists of three main components:
- Core Redirector Worker: A simple, high-performance worker that handles incoming short links and redirects users to the long URL.
- API Worker (
snapurl-web-ui-server): A backend worker that provides a REST API for managing URL mappings in Cloudflare KV. - UI Worker (
snapurl-web-ui): A worker that serves the static assets for the React-based management interface.
All management interfaces (the UI and API workers) are secured by a single Cloudflare Access application.
Before you begin, ensure you have the following:
- An active Cloudflare account.
- A domain registered and managed by Cloudflare DNS.
- At least two subdomains for that domain (e.g.,
s.yourdomain.comfor the redirector andmanage.yourdomain.comfor the UI). - Node.js and npm installed on your local machine.
- The
wranglerCLI installed (npm install -g wrangler).
First, we need to create a Key-Value (KV) namespace to store the URL mappings.
- Log in to your Cloudflare dashboard.
- In the sidebar, navigate to Workers & Pages > KV.
- Click Create a namespace and give it a name, for example,
SNAPURL_KV. - After creation, copy the ID of the new namespace. You will need it in the following steps.
This worker handles the actual URL redirection.
- In the Cloudflare dashboard, go to Workers & Pages and click Create Application.
- Select Create Worker.
- Give it a name (e.g.,
snapurl-redirector) and click Deploy. - Click Quick Edit.
- Copy the entire content of the
src/index.jsfile from this project and paste it into the editor, replacing the default code. - Navigate to the worker's Settings > Variables tab.
- Add KV Namespace Binding:
- Variable name:
SNAPURL_KV - KV namespace: Select the
SNAPURL_KVnamespace you created earlier.
- Variable name:
- Add Environment Variable:
- Variable name:
ROOT_REDIRECT_URL - Value: The URL you want users to be redirected to if they visit the root of your shortener domain (e.g.,
https://your-main-website.com).
- Variable name:
- Click Save and Deploy.
This worker provides the backend API for the management UI.
- Navigate to the
web-ui/web-ui-server-workerdirectory in your local project. - Open the
wrangler.tomlfile. - Find the
[[kv_namespaces]]section and replace the placeholderidwith the KV Namespace ID you copied in Step 1. - In your terminal, from the
web-ui/web-ui-server-workerdirectory, run the deployment command:npx wrangler deploy
This worker serves the React management interface.
- First, navigate to the
web-ui/clientdirectory to build the React application:cd web-ui/client - Install the dependencies:
npm install - Create a new file named
.envand add the following content, replacing the placeholder values:# The domain of your Cloudflare Access login page (e.g., my-team.cloudflareaccess.com) VITE_AUTH_DOMAIN=your_auth_domain_from_step_4 # The full base URL for your deployed API worker (e.g., https://api.yourdomain.com/api) VITE_API_BASE_URL=https://your_api_worker_domain/api - Build the React application. This will create a
distdirectory containing the static assets.npm run build - Navigate back to the parent
web-uidirectory:cd .. - From the
web-uidirectory, deploy the worker:npx wrangler deploy
Now, assign your custom subdomains to the deployed workers.
- In the Cloudflare dashboard, navigate to your main domain's settings.
- Go to Workers & Pages.
- For each of the three workers (
snapurl-redirector,snapurl-web-ui-server,snapurl-web-ui), do the following:- Click on the worker.
- Go to the Triggers tab.
- Under Custom Domains, click Add Custom Domain and assign the appropriate subdomain (e.g.,
s.yourdomain.comfor the redirector,api.yourdomain.comfor the API, andmanage.yourdomain.comfor the UI).
This is the final and most critical step to secure your management interface.
- In the Zero Trust Dashboard, go to Access -> Applications.
- Click Add an application and select Self-hosted.
- Configure the application:
- Application name:
SnapURL Management(or your preferred name). - Application domain: Add the custom domains for both your UI and API workers (e.g.,
manage.yourdomain.comandapi.yourdomain.com).
- Application name:
- Create a login policy:
- On the next page, create an
Allowpolicy to grant access to yourself and other authorized users (e.g., by email address).
- On the next page, create an
- Configure CORS and Cookie Settings:
- After creating the application, click Edit and go to the Settings tab.
- Scroll down to Advanced settings.
- Enable the Bypass OPTIONS requests to origin toggle.
- Under Cross-Origin Resource Sharing (CORS) settings:
- Allow origins: Add the full URL of your UI worker (e.g.,
https://manage.yourdomain.com). - Allow methods: Select
GET,POST,PUT, andDELETE. - Allow headers: Add
Content-Type,Authorization, andCf-Access-Jwt-Assertion.
- Allow origins: Add the full URL of your UI worker (e.g.,
- Save the application.
- Navigate to the custom domain you set for the UI (e.g.,
https://manage.yourdomain.com). - You should be redirected to the Cloudflare Access login page.
- Log in with the identity you authorized in your Access policy.
- Once logged in, the SnapURL management interface should load and be fully functional.
Your SnapURL project is now fully deployed and secured!