Skip to content

Commit

Permalink
Added a static resource handler written by Daniel Kwiecinski
Browse files Browse the repository at this point in the history
  • Loading branch information
khellan committed Jun 2, 2010
1 parent c09f554 commit 5e8d73e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions etc/pillow.ini
Expand Up @@ -3,6 +3,7 @@
[pillow]
port = 8000
databases = test
document_root = priv/

[routing]
routing_table = http://localhost:5984/
Expand Down
5 changes: 4 additions & 1 deletion src/pillow_sup.erl
Expand Up @@ -137,4 +137,7 @@ init(ChildSpecs) ->
%% Returns: The dispatch map
%%--------------------------------------------------------------------
dispatch_map() ->
[{['*'], pillow_couch, []}].
[
{['static', '*'], static_resource, [couch_config:get("pillow", "document_root")]},
{['*'], pillow_couch, []}
].
58 changes: 58 additions & 0 deletions src/static_resource.erl
@@ -0,0 +1,58 @@
%% @author author <daniel.kwiecinski@lambder.com>
%% @copyright Daniel Kwiecinski.
%% @doc Static webmachine resource.

-module(static_resource).
-export([init/1, allowed_methods/2,
content_types_provided/2, resource_exists/2, last_modified/2, provide_content/2]).

-include_lib("webmachine/include/webmachine.hrl").
-include_lib("kernel/include/file.hrl").
-record(context, {docroot,fullpath,fileinfo}).

init(DocRoot) ->
{ok, #context{docroot=DocRoot}}.

resource_exists(ReqData, Context) ->
case get_full_path(Context#context.docroot, wrq:disp_path(ReqData)) of
undefined -> {false, ReqData, Context};
Path ->
case filelib:is_regular(Path) of
true ->
case file:read_file_info(Path) of
{ok, FileInfo} ->
{true, ReqData, Context#context{fileinfo=FileInfo}};
{error, _} ->
{false, ReqData, Context}
end;
_ -> {false, ReqData, Context}
end
end.

content_types_provided(ReqData, Context) ->
Path = get_full_path(Context#context.docroot, wrq:disp_path(ReqData)),
{[{webmachine_util:guess_mime(Path), provide_content}], ReqData, Context#context{fullpath=Path}}.

allowed_methods(ReqData, Context) -> {['HEAD', 'GET'], ReqData, Context}.

last_modified(ReqData, Context) ->
{(Context#context.fileinfo)#file_info.mtime, ReqData, Context}.

provide_content(ReqData, Context) ->
{ok, Value} = file:read_file(Context#context.fullpath),
{Value, ReqData, Context}.
% ------------------ PRIVATE ------------------------


get_full_path(DocRoot, Path) ->
case mochiweb_util:safe_relative_path(Path) of
undefined -> undefined;
RelPath ->
FullPath = filename:join([DocRoot, RelPath]),
case filelib:is_dir(FullPath) of
true ->
filename:join([FullPath, "index.html"]);
false ->
FullPath
end
end.

0 comments on commit 5e8d73e

Please sign in to comment.