views:

533

answers:

4

Hi, I programmed in Ruby and Rails for quite a long time, and then I fell in love with the simplicity of the Sinatra framework which allowed me to build one page web applications. Is there a web framework like Sinatra available for Erlang? I tried Erlyweb but it seems far too heavyweight

+1  A: 

You might be interested in Rusty Klophaus' nitrogen framework. It's really lightweight and is ideal for really dynamic single page sites.

Jeremy Wall
Hi. Yes I have looked at nitrogen, but its still very heavyweight and seems more like fully fledged Rails, and even needs Yaws, INet, or Mochiweb installed first. I'm looking for something that allows me to serve just basic HTML (no Ajax needed) and can in few lines provide me with an application and routing ability. Sinatra lets you serve any URLs from one file, and as I understand (correct me if I'm wrong), but Nitrogen has no support for this, or at least I could not find this from the documentation on the web.
Zubair
+4  A: 

You could achieve something minimal with mochiweb:

start() ->
  mochiweb_http:start([{'ip', "127.0.0.1"}, {port, 6500},
                       {'loop', fun ?MODULE:loop/1}]).
                           % mochiweb will call loop function for each request

loop(Req) ->
  RawPath = Req:get(raw_path),
  {Path, _, _} = mochiweb_util:urlsplit_path(RawPath),   % get request path

  case Path of                                           % respond based on path
    "/"  -> respond(Req, <<"<p>Hello World!</p>">>);
    "/a" -> respond(Req, <<"<p>Page a</p>">>);
    ...
    _    -> respond(Req, <<"<p>Page not found!</p>">>)
  end.

respond(Req, Content) ->
  Req:respond({200, [{<<"Content-Type">>, <<"text/html">>}], Content}).

If you need advanced routing, you will have to use regex's instead of a simple case statement.

Zed
Brilliant, this is just the sort of answer I needed. Thanks! :)
Zubair
+2  A: 

Hi,

May be this example (see REST SUPPORT) using misultin, looks like sinatra :

Samuel
+3  A: 

Have a look at webmachine. It has a very simple but powerful dispatch mechanism. You simply have to write a resource module, point your URIs to it and your service is automatically HTTP compliant.

Tom Sharding