Create a bit.ly like shorturl static website


I decided to create my own shorturl website last week for personal use and ended up developing a python project (deecubes)that can be used by anyone to do the same. This is a post to explain what/why/how about it.

Why my own shorturl website?

So far I had been using sites like bit.ly or TinyURL whenever I needed to generate a shorturl (e.g. for giving to someone for easily remember, noting down on paper, putting links on resume, etc) but I had concerns that:

  • The site could go out of operation anytime.
  • They seem a bit unprofessional and also, there are a lot of scammy links floating around on these sites that many folks are wary of clicking on them.
  • I lose the SEO juice to external sites
  • Custom shorturl keywords were mostly taken
  • Keep history of all links I convert

I could satisfy a few of these points by creating an account and using my custom domain with bit.ly or something but my developer itch sealed the deal by giving me something to code :P.

Requirements for the shorturl website

These were the requirements that I listed out while beginning the project:

  • Should be cheap af. (I ended up at FREE! ;) )
  • Should be maintenance free and run itself (I didn’t want another headache to tune/apply patches for and what not)
  • Should allow addition of links from anywhere (whether on my PC or not)
  • Should allow random links as well as custom ones
  • Should allow to remove previously generated shorturls
  • Should allow me to easily see all generated shorturls, preferably along with history
  • Allow previewing the link a shorturl points to without redirecting, if needed

These lead me to decide on a couple of important things:

  • The site had to be static (Host on s3 for cheap or github pages for free. No patches/maintenance. Super performance)
  • Had to be version controlled (Easy history/viewing/editing from anywhere.)

deecubes

Thus, deecubes was born. (The link in the previous line is shortened using deecubes itself, by the way)

The name comes from the short form ‘DSSS’ of Damn Simple Static url Shortener (so, a D and a cube of S’s. Idiotic, I know). You can read more about how to use it at the above link in the README but in summary:

  • It’s written in pure python
  • It takes a long form url and creates a static website shorturl that can be auto-generated or custom
  • It’s deterministic, so you can be sure of generating the same website from the same inputs
  • Preview can be seen at /preview.html (e.g. https://shgl.in/deecubes/preview.html )
  • Published on pypi, so can install with pip
  • Several ways to use/deploy (as simple as ftp or bash/batch scripts or manual git commits/push or commit directly through github website etc )

Take a look at my generated website repository as an example at deecubesdemo

My deployment strategy

I ended up with the following stack to add links rather painlessly from anywhere and not do any maintenance:

  • Github for hosting the website using github pages
  • Cloudflare to provide that shiny https and some statistics/caching etc
  • Travis to automatically build and deploy the site so I dont need to always generate the shorturl and can commit from anywhere

Check the repository I linked above to see how it is done. Most of the magic is done in a few lines of the .travis.yml file. Essentially it follows this path:

  • I commit a shorturl .txt file directly in the “raw” directory through github web on “source” branch
  • Travis picks up the commit and runs deecubes’ “sync” command to generate the website portions
  • Travis deploys the “output” folder contents to the “master” branch of the same repo.
  • Github is configured to serve the website from “docs” folder of master branch through my custom domain shgl.in (“docs” folder is already created and checked-in inside output folder along with CNAME file)
  • Cloudflare is configured to redirect all requests to shgl.in to my github repo

In a more traditional way, I could also do this:

  • Generate a shorturl locally using an “add” or “generate” command. You could be done at this point and publish the output folder to your website any way you want.
  • I Commit the generated files including the output into the repo and push to github.
  • Travis will still run and publish the website as earlier.

Things to come?

I want to add the following things in next few weeks to the project:

  • A collision handling mechanism which is also deterministic
  • Allow google analytics addition for collecting metrics
  • Allow custom templates for redirect and preview urls
  • Your suggestions?

See also