The Webmachine list seems pretty low traffic, so I thought I send this here to see if anyone had some interesting ideas.<div><br></div><div>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.</div>

<div><br></div><div>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.</div>

<div><br></div><div>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:</div>

<div><br></div><div><div>maybe_fetch_object(Ctx, Path) -></div><div>    % if returns {true, NewCtx} then NewCtx has response_body</div><div>    case Ctx#ctx.response_body of</div><div>        undefined -></div><div>

            case file_exists(Ctx, Path) of</div><div>                {true, FullPath} -></div><div>                    {ok, Value} = file:read_file(FullPath),</div><div>                    {true, Ctx#ctx{response_body=Value}};</div>

<div>                false -></div><div>                    {false, Ctx}</div><div>            end;</div><div>        _Body -></div><div>            {true, Ctx}</div><div>    end.</div></div><div><br></div><div>An obvious optimization here is to cache files in memory and check that before doing a read_file.</div>

<div><br></div><div>But a real solution to me seems to be not using Webmachine for static content at all somehow. </div><div><br></div><div>The problem for an example url: </div><div><br></div><div>/user/new</div><div><br>
</div><div>I server up the file under /priv/user/new.html</div><div><br></div><div>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.</div>
<div><br></div><div>So the problem there is it relies on Webmachine dispatch rules and resource logic to know [user, new] -> /priv/user/new.html</div><div><br></div><div>That means my two thoughts of nginx match on subdomain and sending like <a href="http://api.domain.com">api.domain.com</a> to Webmachine and anything else it would serve itself. </div>
<div><br></div><div>Can anyone think of a way I can keep the nice URLs and serve the static html files through nginx or another webserver.</div><div><br></div><div>Or am I just wasting time? :)</div><div><br></div><div>Thanks,</div>
<div>Tristan</div>