Playing With Fire

Exploring the web one Elixir at a time

Setting up Elixir and eWebmachine

Thanks for showing an interest in this.

This is not an exhaustive discussion on the ins and outs of setting this up and I do not profess to be an expert. I am also not an expert in Elixir, Erlang or Webmachine, so I might not be able to answer any questions or offer support.

All I can say is that this worked for me.

Pre-requisites:

  • Elixir
    • at time of writing this was at version 1.0.0-rc2
  • Erlang
    • at time of writing Elixir is dependant on Erlang/OTP version 17, and I’m using the version provided by Erlang Solutions.
  • Net access to github
  • A text editor, most things will do - I use VIM

We’re are going to be using eWebmachine, which is an Elixir wrapper around Webmachine.

Ok, first off make sure that you have the above installed. How you do that is documented elsewhere, but a quick Google search will solve that one for you.

$ iex -v
Erlang/OTP 17 [erts-6.1] [source-d2a4c20] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Elixir 1.0.0-rc2

navigate to the location where you want to put your Elixir project. I have a projects directory, how you manage your system is up to you.

$ cd ~/Projects/ewebmachine

In here you then create (using Mix) the Elixir project

$ mix new myProject

Change to this directory and open the mix.exs file.

$ cd myProject
$ vim mix.exs

Change the application function to:

def application do
    [applications: [:logger, :ewebmachine],
     mod: {MySup, []}]
end

MySup is the module that contains our supervisor - more on that later.

You also need to add in the dependency for Ewebmachine, so change the private deps function to:

defp deps do
    [{:ewebmachine,"1.0.0",[github: "awetzel/ewebmachine"]}]
end

Once these are done, close this file and return to the command prompt.

You then need to tell Elixir to get these dependencies

$ mix deps.get

This pulls down Ewebmachine, but also Webmachine and its dependencies. Depending on your connection to the internet this might take some time.

Ok, now we’re ready to write some code.

Firstly, lets put the supervisor in place. In a change to the documentation that you might find at https://github.com/awetzel/ewebmachine, the supervisor example is out of date as Supervisor.Behaviour has been deprecated and we need to use the Application module to make things work. So my example is:

$ vim lib/mysup.ex
defmodule MySup do
  use Application

  def start(_type, _args), do: MySup.Sup.start_link

  defmodule Sup do
    use Supervisor
    def start_link, do: Supervisor.start_link(__MODULE__,[])
    def init([]) do
      supervise([
        supervisor(Ewebmachine.Sup,[[modules: [MyProject],port: 18080]])
      ], strategy: :one_for_one)
    end
  end
end

Here I’m using port 18080, but any port will do.

With the supervisor now in place, and it watching the MyProject module, it is time to put a resource in place to handle requests.

Ewebmachine uses the macro resource to handle the creation of resources at compile time. This includes the routing.

Mix has already put a module in place for us to use: lib/myproject.ex, we’ll edit this file to add in a default response.

$ vim lib/myproject.ex
defmodule MyProject do
  use Ewebmachine

  resource [] do
    to_html do: "<html><body>This is a test</body></html>"
  end
end

To run this then save the change and return to the command prompt.

Do:

$ iex -S mix

This compiles the project and runs the webmachine start.sh script.

Open up a web browser and then navigate to http://localhost:18080 and your page should display.

Done.

Next time, I’ll look at routing and templating.