erlangwebmachine

Webmachine: Who sets static_resource:init/1?


I'm looking at two versions static_resource:init/1 in two Webmachine applications:

https://github.com/basho/wriaki/blob/master/apps/wriaki/src/session_resource.erl
http://lambder.com/2009/06/providing-static-content-in-webmachine/

In both cases the parameter passed into static_resource:init/1 is DocRoot. But I can't find where this function is set or DocRoot is defined.

Can anyone point me toward an answer?

Many thanks,

LRP


Solution

  • I had a look at the webmachine_demo_fs_resource from here, and it appears that the value passed to init/1 is set in the dispatch.conf. As you can see here the last parameter of the config for webmachine_demo_fs_resource is [{root, "/tmp/fs"}], which specifies the doc root. Of course you can label the properties however you like, so long as you read them out in the same way in init/1.

    This explains how the demos work, however that may or may not be the right thing to do from the perspective of your application. Maybe you want to read an environment variable or a boot arg to figure out where your doc root should be. For example:

    init([]) ->
        DocRoot =
            case init:get_argument(doc_root) of
                {ok, [[DR]]} -> DR;
                error -> "/tmp/fs"
            end,
        {ok, #context{root=DocRoot}}.
    

    You can set the Context value to be whatever you like, so you can choose your own adventure!