[erlang-questions] Static files served through Webmachine

Tristan Sloughter tristan.sloughter@REDACTED
Tue Jul 19 03:27:58 CEST 2011

The Webmachine list seems pretty low traffic, so I thought I send this here
to see if anyone had some interesting ideas.

I've been mulling around in my head serving up static content in webapps I
write with Webmachine. The main pattern I'm reaching is heavily using
frontend tools like JQuery templates, Knockout.js, Spine, Backbone, etc and
having the Webmachine backend essentially serve only as a RESTful JSON
interface. No templating on the backend and things like that.

Webmachine fits that part perfectly! It has made web development for me more
than bearable but enjoyable for once. And I hope to release a sort of Erlang
Webframework (a set of applications) in case others also find this method to
be fitting to them as well.

But the problem lies in the inefficient serving of static files like html,
js and css. The call comes in, matches the last dispatch rule I have of '*'
and goes to a resource to server static content like this:

maybe_fetch_object(Ctx, Path) ->
    % if returns {true, NewCtx} then NewCtx has response_body
    case Ctx#ctx.response_body of
        undefined ->
            case file_exists(Ctx, Path) of
                {true, FullPath} ->
                    {ok, Value} = file:read_file(FullPath),
                    {true, Ctx#ctx{response_body=Value}};
                false ->
                    {false, Ctx}
        _Body ->
            {true, Ctx}

An obvious optimization here is to cache files in memory and check that
before doing a read_file.

But a real solution to me seems to be not using Webmachine for static
content at all somehow.

The problem for an example url:


I server up the file under /priv/user/new.html

for /user/user_id I serve up /priv/user/show.html (show.html then references
javascript that finds the user_id from the url and populates the content, so
no server-side templating is needed.

So the problem there is it relies on Webmachine dispatch rules and resource
logic to know [user, new] -> /priv/user/new.html

That means my two thoughts of nginx match on subdomain and sending like
api.domain.com to Webmachine and anything else it would serve itself.

Can anyone think of a way I can keep the nice URLs and serve the static html
files through nginx or another webserver.

Or am I just wasting time? :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110718/be0c0944/attachment.htm>

More information about the erlang-questions mailing list