Erlang - Simple webapps in Erlang with Cowboy and ErlyDTL

March 28, 2017    erlang

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:

https://github.com/shaneutt/cowboy_erlydtl_rebar3_template

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

git clone https://github.com/shaneutt/cowboy_erlydtl_rebar3_template ~/.config/rebar3/templates/cowboy_erlydtl_rebar3_template/
And now you can use new to build a base for your new app:
rebar3 new cowboy_erlydtl your_app_here
And that’s it, you should now have a base for your app deployed:
your_app_here/
├── config
│   ├── sys.config
│   └── vm.args
├── Makefile
├── priv
│   ├── static
│   ├── templates
│   │   └── index.dtl
│   ├── templates-compiled
│   └── www
├── rebar.config
└── src
    ├── your_app_here_app.erl
    ├── your_app_here.app.src
    ├── your_app_here_default_router.erl
    ├── your_app_here.erl
    └── your_app_here_sup.erl
This provides a default DTL template that works right out of the box.

Just run:

cd your_app_here/
rebar3 shell
Which will start the web server on http://localhost:8080:
$ curl http://localhost:8080
Hello from Cowboy!
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!