1. 程式人生 > >Building REST Server with Elixir @ Alex Pliutau's Blog

Building REST Server with Elixir @ Alex Pliutau's Blog

I always wanted to try Elixir because of it’s nice Erlang ecosystem and because it’s similar to Go in a lot of points. I was thinking what I can start with, and I decided to build, yes… a REST server. It took me around 1 hour to install Elixir, and build a simple REST server using maru RESTful framework.

I built a small items API using JSON and Agent as storage. Let me go through all actions to build.

Prerequisites

You should only install Elixir, if you’re using brew: brew install elixir.

Create new Mix project

Mix is installed together with Elixir, so you just need to run one command to create a project, Mix will create all initial files for you: config, deps, lib, tests, README.

mix new elixirrest

Define dependencies

As we decided to use maru package, we have to add it to mix.exs file.

To get and compile dependencies you have to run this command in elixirrest folder:

mix do deps.get, compile

Configuration

Mix created a file config/config.exs, where we can put all configuration we need, for different environments you can create a separate file, but in our example let’s have only one with port 3030

:

API

I created a folder lib/elixirrest with 2 files: api.ex, agent.ex. In the first we will define our API, and second will be used for as storage worker.

API contains only 2 endpoints: GET to list all items and POST to create new item, they use Agent as storage.

Supervisor

To start Agent we just add it to supervision tree in lib/elixirrest.ex:

Run and Test

Run this command in elixirrest folder:

iex -S mix

If you don’t see any errors you can test API endpoints:

curl -XPOST -d "name=test" http://localhost:3030/items
curl http://localhost:3030/items

I pushed project into this repository in case you want to check or contribute.