Search code examples
erlangyaws

Routing in Yaws


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).


Solution

  • 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.