Today the most used web server and framework to build web-based applications in Erlang world is Cowboy, which is awesome, but it doesn’t mean that it cannot be improved to make it even better. Furthermore, because Cowboy is an Open Source project, it is pretty easy to extend it and here is where cowboy-trails comes in.
First, let’s start explaining why we use cowboy-trails:
To add extra information to Cowboy routes, which can be used later to interact with the server in a higher abstraction level.
Suppose that you want to add additional information (metadata) to cowboy routes related to the semantics of each HTTP method:
We wanted to make it easier to compile Cowboy routes, for example: there is a common case when you work with Cowboy in which you normally compile all routes under a single host. In this case, cowboy-trails provides a simple function to make it easier trails:single_host_compile/1.
We also wanted to allow you to define routes on each handler instead of a single place. This could be one of the most important reasons, because it helps you organize your code better, since each handler will be responsable for its own routes.
Normally, when you work with cowboy you have to define all routes in one place, for example, like this:
With trails you’re able to define the routes on each resource handler, and your handler must implement the callback trails/0 and return the specific routes for that handler. Once you have implemented the trails/0 callback on your handlers, you can do something like this:
Doing it this way, each handler keeps its own routes, as it should, allowing you then to merge them easily.
We also added other helper functions such as:
cowboy routes storage:
Finally, to learn more about cowboy-trails please check the Project and Example.