Routing in Yaws

574 views Asked by At

How can one do routing in Yaws? Like routing in ASP.NET MVC or Rails.

Of-course Erlang is a functional language without notions from oo world; but one could rout http://[domain].[com]/controller/action/arg1/arg2/ as a GET request to a module named 'controller' with an 'action/2' function (or is there already such a lib).

2

There are 2 answers

0
Steve Vinoski On BEST ANSWER

Yaws provides a few ways of doing this:

  1. Register an appmod on the path "/" as shown in this answer, but as the question there hints, this approach doesn't allow you to get rid of the ".yaws" suffixes on your URLs.
  2. Use a rewrite module, as shown in section 7.1.2 of the Yaws PDF documentation. This would allow your clients to use URLs without ".yaws" suffixes — your rewrite module can add them back where needed.
  3. Use a dispatch module as described on page 52 of the Yaws PDF documentation. Note though that this approach bypasses a lot of useful Yaws dispatching machinery, including the handling of .yaws pages, so use it only if you really really know what you're doing.

Of these 3 choices, I believe the rewrite module is the best one for this particular problem.

2
Hynek -Pichi- Vychodil On

I don't know if there is such module but I don't see benefit of module of doing this:

out(Arg) ->
    Uri = yaws_api:request_url(Arg),
    Path = string:tokens(Uri#url.path, "/"),
    Method = (Arg#arg.req)#http_request.method,
    out(Arg, Method, Path).

out(_Arg, 'GET', [Module, Function | Args]) ->
    apply(Module, Function, Args).

With some error handling and so. BTW, nice way how to get hacked.