Simple webapps in Erlang with Cowboy and ErlyDTL

Of all the programming languages I’ve written web applications in, Erlang is by far my favorite.

Perhaps the functional nature of Erlang, or the modular simplicity of apps and releases, or the easy primitives for concurrency make it most appealing.

Whatever the reason I’ve gotten in the habit of building new web applications with Cowboy for the web server, and ErlyDTL to provide Django-like templates.

As such I built a Rebar3 template to make it easy to build these kinds of apps:

To use this template with your local copy of Rebar3, simple clone the repository to the Rebar3 template directory:

And now you can use new to build a base for your new app:

And that’s it, you should now have a base for your app deployed:

This provides a default DTL template that works right out of the box.

Just run:

Which will start the web server on http://localhost:8080:

And that’s it, the sky is the limit!

From here you can add a Database like PostGreSQL with a client like ePGSQL, build your HTTP API using JSX for JSON, maybe even queue up work for another service with RabbitMQ.

Happy coding!

Rust – for Rust

While looking for a way to provide code coverage with for Rust, I stumbled upon a solution while reading through Iron.

The solution involves a tool called kcov which is a Linux/OSX code coverage tester for compiled languages.

This tool can be implemented inside a .travis.yml file to push coverage data from a Travis CI  build.

Here is an example .travis.yml file:

Basically all you have to do is run:

Changing “riak-*” for your own project, and everything before that is just to compiled kcov inside the Travis CI environment.

This configuration was used for my Riak Rust Client.

Since the above example uses master.tar.gz, I recommend you fork the original repo and use your fork for stability.

Happy Coding!

Rust – std::convert::Into and std::convert::From

I’ve been spending time recently learning the Rust Programming Language by writing a Riak Client using Riak’s Protocol Buffers API.

Through Ownership & Borrowing Rust enables safety that generally comes at the cost of flexibility. Through features like Traits Rust is able to give some flexibility back to the programmer.

Some traits I’ve started to use recently include std::convert::Into and std::convert::From.

When the Into trait is applied to a type, it allows it to consume itself to convert to another type.

The String type provides conversion into Vec<u8>:

One of the things I’ve been striving for while writing the Rust Riak Client has been clean interfaces where a Vec<u8> is ultimately needed under the hood, but I want to make it easy for the end-users to provide string types where applicable.

By using Into in a Generic one can make a very flexible interface to provide a Vec<u8>:

This function can now take String, &String, &str making the conversion of a string type to bytes (for the purposes of sending via TCP to Riak) a matter of function definition, which is super sleek.

I really enjoy writing code in Rust. I expect I’ll have a lot more write-ups to come as I discover things and learn better ways to do things I had already been doing.

If you’ve never checked out rust, try the Rust Playground!

Happy coding!

Riak in Docker

Riak was designed to be deployed and run on dedicated infrastructure, as opposed to containers or virtualization platforms.

Nonetheless, being able to quickly deploy a Riak cluster for testing and development purposes can be very useful.

I’ve created a repository today that enables the user to build Docker images for most versions of Riak KV, TS, and CS ever produced:

The dockerfile is relatively simple:

The script used to start the Riak service takes care of some basic configuration and then tails the logs:

Using these images, creation of a node becomes a breeze:

Then you can simply add enough nodes to make a five node cluster (and wait for them to come up):

And then join all the nodes together, plan, and commit!

Now you have a Riak test environment to test your apps against, woohoo!

Happy Coding!


Booting up a new blog!

Some time ago I actively blogged my programming pursuits. I had since stopped doing that for one reason or another.

I’ve decided I want get back into blogging about software engineering, programming as a hobby, maker projects, and all the things that I enjoy.

So I’m starting up a new blog and will make some post soon, Happy coding!