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

Lloyd R. Prentice
  • 4,329
  • 3
  • 21
  • 31

1 Answers1

1

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!

David Weldon
  • 63,632
  • 11
  • 148
  • 146
  • Hi @david, I've tried your suggested code. but I get an error suggesting that there's a bug in my static_resource.erl module. The error is: =ERROR REPORT==== 4-Aug-2011::00:37:27 === webmachine error: path="/static" {error,function_clause} Is there a way to test the functions in this module from the Erlang shell? The error message gives no clues. Many thanks, LRP – Lloyd R. Prentice Aug 04 '11 at 04:40
  • That error means that there was no matching `init/1` clause. Your `dispatch.conf` file probably has something like `{root, "/tmp/fs"}` as the last element in the tuple, whereas your resource has an `init/1` that only accepts an empty list. Fix one or the other and you should be good - e.g. `init(_) ->`. Note that I think you need to restart the running node if you change `dispatch.conf`. – David Weldon Aug 04 '11 at 05:15
  • Hi @david,Thanks for the suggestion, but I still get an error. I'm sure I'll get many as I move forward, so the real issue for me is how to debug these functions. At risk of breaking the thread, let me post a new question re: debugging. Code examples should be easier to see as well. Thanks again, LRP – Lloyd R. Prentice Aug 04 '11 at 15:05
  • Well that's disappointing. I suppose that error could be caused by any clause not matching. For example, if your context/state variable was set to a list in `init/1` but matched to a record in `to_html/2`. One way to approach the problem is to start with a working resource and add features until it fails. Feel free to email me if you want me to take a quick look at the code. – David Weldon Aug 04 '11 at 18:35