Wednesday, March 26, 2008

Crary - A new Erlang lightweight web server

Beside of featherlight iserve and battle proved mochiweb (at MochiMedia), there is now an additional choice for lightweight Erlang HTTP servers: Crary, which shares similar design goals but provides a different interface. According to its author Scott Parish:
Crary, a HTTP server for the REST of us.

The intention is that its small enough and flexible enough to be used for most any HTTP server need. While it isn't a stand-alone system, nor does it have its own web-framework, these things can easily be built on top of crary. Crary should be easily embedded into the release of a larger application to provide a web interface or to create a web service interface.
Below a tiny little snippet illustrating how to write a hello world program with crary:
start() ->
crary:start(8080, {?MODULE, handler, []}).

handler(#crary_req{method = "GET", uri = #uri{path = "/favicon.ico"}} = Req) ->
crary_dir_listing:write_file(Req, filename:join([my_doc_root(), "favicon.ico"]));
handler(#crary_req{method = "GET", uri = #uri{path = "/test"}} = Req) ->
crary:r(Req, ok, [{"content-type", "text/html"}], <<"hello world">);
handler(#crary_req{method = "GET"} = Req) ->
crary:not_found(Req);
handler(Req) ->
crary:not_implemented(Req).
To get your copy of crary just clone it from its git repository, or install it via erlware.

2 comments:

Pichi said...

It looks great. I like KISS things and Carry looks perfectly fit KISS principles. It is perfectly enough for REST if there will be added some good resources uri matching module which can be perfectly plugged in this simple web server. So did you any performance suggestions? Did you try compare it with YAWS? I know, that YAWS have different (and more but mostly for REST useless) features, but is there any reason to think Carry should be faster than YAWS?

Roberto Saccon said...

Pichi, I don't know about performance compared to yaws. For me it doesn't really matter, but what makes a difference is that you can easily adapt crary to your needs, yaws I see more as Apache replacement, working perfectly out of the box for many tasks. Crary is however a bit slower than mochiweb, because crary does not use some undocumented Erlang TCP/IP HTTP features which might cause portability problems.