Sign up for a Zeit and Fauna account if you don't have them already, and install the now
command-line utilities.
Sign up for an IP Geolocation account, and grab the API key - you'll need it in the next step.
Now run the following in your terminal:
git clone https://github.com/bendechrai/url-shortener.git
cd url-shortener
now secret add ipgeolocation_apikey <XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX>
now --prod
- Create a new Fauna Database called
url-shortener
; - Add the Fauna Integration into your Zeit account;
- Create a new Fauna Database Server Key for your new database and copy the key;
- Create two collections in the database, called
redirects
andclicks
; - In the
redirects
collection, create an index calledall_redirects
, set the Terms field toshortcode
d; and - Paste this key into Zeit's Fauna Integration setup when prompted, and link the Fauna database with the new Zeit project created during deployment.
Head to the redirects
collection, click on New Document, and enter something in the following format:
{
"shortcode": "twitter",
"dest": "https://twitter.com/bendechrai"
}
If you are migrating from another system, you can generate a script that looks like this:
Create(Collection("redirects"), { data: { "shortcode": "contact", "dest": "https://bendechrai.com/contact/" } });
Create(Collection("redirects"), { data: { "shortcode": "github", "dest": "https://github.com/bendechrai" } });
Create(Collection("redirects"), { data: { "shortcode": "linkedin", "dest": "https://www.linkedin.com/in/bendechrai/" } });
Create(Collection("redirects"), { data: { "shortcode": "twitter", "dest": "https://twitter.com/bendechrai" } });
Create(Collection("redirects"), { data: { "shortcode": "youtube", "dest": "https://www.youtube.com/channel/UCY5SDWGg5Wa1ptwFF1EXQPg" } });
Take this, and paste it into the Fauna Web Shell.
If you go to the base URL of your URL Shortener (i.e. https://url-shortener.yourname.now.sh/), this system will look for a shortcode of __default__
. Creating a record like this will let you set the destination for this scenario:
{
"shortcode": "__default__",
"dest": "https://bendechrai.com"
}
Currently, this redirector will fail if the shortcode cannot be found, or it is found more than once.