Skip to content

Instantly share code, notes, and snippets.

@pragmaticobjects
Created October 28, 2011 14:56
Show Gist options
  • Select an option

  • Save pragmaticobjects/1322467 to your computer and use it in GitHub Desktop.

Select an option

Save pragmaticobjects/1322467 to your computer and use it in GitHub Desktop.
Routing in cowboy
start(_Type, _Args) ->
Dispatch = [
{'_', [
{[<<"users">>, id, <<"messages">>], users_handler, []},
{'_', default_handler, []}
]}
],
cowboy:start_listener(http, 100,
cowboy_tcp_transport, [{port, 8080}],
cowboy_http_protocol, [{dispatch, Dispatch}]
),
Then in users_handler.erl,
handle(Req, State) ->
{Value, Req2} = cowboy_http_req:binding(id, Req),
case Value of
<<"messages">> ->
{ok, Req3} = cowboy_http_req:reply(200, [], <<"Hello world!">>, Req),
{ok, Req3, State},
_ ->
...
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment