An Erlang Library For REST Interfaces
%Specify routes:[{Method:atom(),Route:string(),Handler:fun()}]
Routes=[{'GET',
"/awesomeoRoute",
fun(Req) -> Req:ok({"plain/text","Received Request to awesomeoRoute"}) end
}].
%Specify mochiweb server options
Options = [{ip,"127.0.0.1"},{port,8888}].
%Start Listening and responding to requests
restify_server:start(Options,Routes).
A library to make the creation of rest interfaces easier in erlang. To allow developers to focus on the implementation of the API, not the details of setting up the webserver.
##Windows
- Get the restify source from GitHub
- Open the command prompt
- Cd into the directory
- Ensure rebar is in your Path so that you can execute commands
- Execute rebar command
$ rebar get-deps compile
##Unix
- Get the restify source from GitHub
- Cd into the directory
- Make it
$ make
To use this library in an OTP application it needs to be added to the supervision tree. The best way to do this is as follows
- Add As Rebar Dependency
In your rebar.config file:
{deps, [{restify,
".*",
{git, "https://github.com/tjchaplin/restify.git"}}
]}.
- Create a module that will setup the rest server
rest_server.erl
-module(rest_server).
-export ([start_link/0]).
start_link() ->
%Specify routes:[{Method:atom(),Route:string(),Handler:fun()}]
Routes=[{'GET',
"/awesomeoRoute",
fun(Req) -> Req:ok({"plain/text","Received Request to awesomeoRoute"}) end
}],
%Specify mochiweb server options
Options = [{ip,"127.0.0.1"},{port,8888}],
%Start Listening for requests.
%Note: this will return a PID for the supervisior to monitor
restify_server:start(Options,Routes).
- Add the rest_server as a worker to the appliction supervisor
myapp_sup.erl
-module(myapp_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
init([]) ->
RestServer={rest_server,
{rest_server, start_link, []},
permanent, 5000, worker, dynamic},
SupervisorChildren =[RestServer],
{ok, { {one_for_one, 5, 10}, SupervisorChildren} }.
- Now when your OTP application is started the rest_server(restify) will be monitored
The library uses mochiweb as its lightweight webserver.