Added a registration method

This commit is contained in:
Ben Hood 2009-07-12 22:27:06 +01:00
parent 6a58df7c23
commit ab60eee1f8
3 changed files with 25 additions and 10 deletions

View File

@ -10,5 +10,8 @@
]},
{registered, []},
{mod, {mod_http_app, []}},
{env, []},
{env, [
{docroot, "priv/www"},
{port, 8000}
]},
{applications, [kernel, stdlib, crypto]}]}.

View File

@ -1 +0,0 @@

View File

@ -1,21 +1,17 @@
%% @author author <author@example.com>
%% @copyright YYYY author.
%% @doc Web server for mod_http.
-module(mod_http_web).
-author('author <author@example.com>').
-export([start/1, stop/0, loop/2]).
-export([install_static/2]).
%% External API
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
{ok, DocRoot} = application:get_env(docroot),
{ok, Port} = application:get_env(port),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
mochiweb_http:start([{name, ?MODULE}, {port, Port}, {loop, Loop}]).
stop() ->
mochiweb_http:stop(?MODULE).
@ -37,6 +33,23 @@ loop(Req, DocRoot) ->
Req:respond({501, [], []})
end.
%% The idea here is for mod_http to put all static content into this
%% directory when an application deploys a zip file containing static content
%% and to key it based on the name of the app
install_static(ModuleName, Archive) when is_list(Archive) ->
{ok, DocRoot} = application:get_env(mod_http, docroot),
%% TODO This should be cleaned down before any new stuff is deployed
%% Might be an idea to do this on application startup though
Parent = filename:join(DocRoot, ModuleName),
case filelib:is_dir(Parent) of
true -> ok;
false ->
ok = filelib:ensure_dir(Parent),
ok = file:make_dir(Parent)
end,
zip:unzip(Archive, [{cwd, Parent}]).
%% Internal API
get_option(Option, Options) ->