Setting up Elixir and eWebmachine
Posted on September 08, 2014 by Clive in Elixir, 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: 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. 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. In here you then create (using Mix) the Elixir project Change to this directory and open the mix.exs file. Change the application function to: 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: Once these are done, close this file and return to the command prompt. You then need to tell Elixir to get these dependencies 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: 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. To run this then save the change and return to the command prompt. Do: 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.
$ 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
$ cd ~/Projects/ewebmachine
$ mix new myProject
$ cd myProject
$ vim mix.exs
def application do
[applications: [:logger, :ewebmachine],
mod: {MySup, []}]
end
defp deps do
[{:ewebmachine,"1.0.0",[github: "awetzel/ewebmachine"]}]
end
$ mix deps.get
$ 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
$ vim lib/myproject.ex
defmodule MyProject do
use Ewebmachine
resource [] do
to_html do: "<html><body>This is a test</body></html>"
end
end
$ iex -S mix