What is it?

In an effort to learn Elixir, I created a small URL shortener written in the language. This was a great first project, because it brought in a few different aspects of the programming language; most notably Ecto, the built-in database wrapper. The full codebase for the project can be found on GitHub.

When starting this project, I had the option to use Phoenix, a framework that I would say is Elixir's equivalent to Ruby on Rails. I chose not to use Phoenix for two reasons: 1) it is much too large a framework for an application as small as this one, and 2) learning a language is much harder when learning a framework at the same time. I wanted to make sure I had a firm grasp of the language and it's features before integrating a framework into the workflow, because, while frameworks save time in the long road, not understanding the underlying functionality and leaving it as a black box only leads to headaches later on. Instead, I opted to use a much more lightweight stack.

I used Cowboy as a server and implemented Elixir's built-in plug functionality to manage routes within the application, and used Ecto to communicate with the database. The project structure and functionality is fairly simple. The user visits the website, enters a long url, and Essence spits out a shortened version of the url. When the url is visited, the user is routed through the Essence server, and pointed back to the original url that was shortened.

How it works

In it's simplest form, Essence takes the url input from the user, generates a random url safe base64 encoded string, and saves the url and string to the database. The only explicitly defined route in the application is the root route, '/'. When visiting this route, the user is prompted to enter a url they wish to convert. The page looks like the following:

To make things easy for the user, I just make an AJAX request when the user hits the submit button, and add the shortened url beneath the input box, along with a copy to clipboard button. At this point, the converted link is ready to be sent out/shared.

Every other request that is not directed towards the root route is assumed to be a shortened url, which takes the form of http://localhost:4000/cvGo48Pf, as an example. The controller then looks up the cvGo48Pf base64 string in the database and returns the corresponding long url. The only issue I ran into while making this project, is that plug/cowboy doesn't seem to have a standard redirect function (or any at all, for that matter). To rectify this issue, I had to create a manual 301 redirect (which is the proper way of redirecting a shortened url) by altering the HTTP response headers. This way, the user of a shortened url would automatically get redirected to the proper place, as expected.

Completing this project left me with a solid understanding of the functional nature of Elixir, which is something I had never experienced before, as up to this point I had only worked with object-oriented programming. My first impression of functional programming is that it is powerful and expressive. I love how Elixir is written, the syntax is very ruby-like, and it runs very well. Elixir seems to be the functional version of ruby. With that said, I do think I'll be incorporating more Elixir into my programming projects.