Permalink
Browse files

Finish pluginizing.

  • Loading branch information...
1 parent 95bb7f4 commit dce2680c015fc68034ccaf39221b4f677fa76d12 @tonyg committed Mar 17, 2010
Showing with 53 additions and 150 deletions.
  1. +3 −5 ebin/rabbithub.app
  2. +1 −1 priv/www/static/application.xsl.xml
  3. +28 −19 src/rabbithub.erl
  4. +0 −54 src/rabbithub_app.erl
  5. +4 −46 src/rabbithub_sup.erl
  6. +17 −25 src/rabbithub_web.erl
View
@@ -3,17 +3,15 @@
{vsn, "0.0.1"},
{modules, [
rabbithub,
- rabbithub_app,
rabbithub_auth,
rabbithub_consumer,
rabbithub_pseudo_queue,
- rabbihub_subscription,
- rabbihub_subscription_sup,
+ rabbithub_subscription,
+ rabbithub_subscription_sup,
rabbithub_sup,
rabbithub_web,
simple_httpc
]},
{registered, []},
- {mod, {rabbithub_app, []}},
{env, [{default_username, "guest"}]},
- {applications, [kernel, stdlib, crypto]}]}.
+ {applications, [kernel, stdlib, crypto, rabbit_mochiweb]}]}.
@@ -7,7 +7,7 @@
<html>
<head>
<title>Application "<xsl:value-of select="/application/name"/>" (<xsl:value-of select="/application/class"/>)</title>
- <link type="text/css" rel="stylesheet" href="/static/style.css"></link>
+ <link type="text/css" rel="stylesheet" href="/rabbithub/static/style.css"></link>
</head>
<body>
<div id="outerContainer">
View
@@ -1,6 +1,6 @@
-module(rabbithub).
--export([start/0, stop/0]).
+-export([setup_schema/0]).
-export([instance_key/0, sign_term/1, verify_term/1]).
-export([b64enc/1, b64dec/1]).
-export([canonical_scheme/0, canonical_host/0, canonical_basepath/0]).
@@ -9,38 +9,47 @@
-export([respond_xml/5]).
-export([deliver_via_post/3, error_and_unsub/2]).
+-rabbit_boot_step({?MODULE,
+ [{description, "RabbitHub"},
+ {mfa, {rabbithub, setup_schema, []}},
+ {mfa, {rabbit_sup, start_child, [rabbithub_sup]}},
+ {mfa, {rabbithub_web, start, []}},
+ {mfa, {rabbithub_subscription, start_subscriptions, []}},
+ {requires, routing_ready},
+ {enables, networking_listening}]}).
+
-include_lib("xmerl/include/xmerl.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").
-include_lib("rabbit_common/include/rabbit_framing.hrl").
-include("rabbithub.hrl").
-ensure_started(App) ->
- case application:start(App) of
- ok ->
- ok;
- {error, {already_started, App}} ->
- ok
- end.
-
-start() ->
- ensure_started(crypto),
- application:start(rabbithub).
+setup_schema() ->
+ ok = create_table(rabbithub_lease,
+ [{attributes, record_info(fields, rabbithub_lease)},
+ {disc_copies, [node()]}]),
+ ok = create_table(rabbithub_subscription_pid,
+ [{attributes, record_info(fields, rabbithub_subscription_pid)}]),
+ ok = mnesia:wait_for_tables([rabbithub_lease,
+ rabbithub_subscription_pid],
+ 5000),
+ ok.
-stop() ->
- Res = application:stop(rabbithub),
- application:stop(crypto),
- Res.
+create_table(Name, Params) ->
+ case mnesia:create_table(Name, Params) of
+ {atomic, ok} -> ok;
+ {aborted, {already_exists, Name}} -> ok
+ end.
get_env(EnvVar, DefaultValue) ->
- case application:get_env(EnvVar) of
+ case application:get_env(rabbithub, EnvVar) of
undefined ->
DefaultValue;
{ok, V} ->
V
end.
instance_key() ->
- case application:get_env(instance_key) of
+ case application:get_env(rabbithub, instance_key) of
undefined ->
KeyBin = crypto:sha(term_to_binary({node(),
now(),
@@ -126,7 +135,7 @@ from_urlsafe(Acc, N, [C | Rest]) ->
canonical_scheme() -> get_env(canonical_scheme, "http").
canonical_host() -> get_env(canonical_host, "localhost").
-canonical_basepath() -> get_env(canonical_basepath, "/").
+canonical_basepath() -> get_env(canonical_basepath, "rabbithub").
default_username() -> get_env(default_username, undefined).
View
@@ -1,54 +0,0 @@
--module(rabbithub_app).
--behaviour(application).
-
--include("rabbithub.hrl").
-
--export([start/2,stop/1]).
-
-start(_Type, _StartArgs) ->
- ok = contact_rabbitmq(),
- ok = setup_schema(),
- rabbithub_sup:start_link().
-
-stop(_State) ->
- ok.
-
-contact_rabbitmq() ->
- RabbitNode = case application:get_env(rabbitmq_node) of
- undefined ->
- [_NodeName, NodeHost] = string:tokens(atom_to_list(node()), "@"),
- A = list_to_atom("rabbit@" ++ NodeHost),
- application:set_env(rabbithub, rabbitmq_node, A),
- A;
- {ok, A} ->
- A
- end,
- {contacting_rabbitmq, RabbitNode, pong} =
- {contacting_rabbitmq, RabbitNode, net_adm:ping(RabbitNode)},
- error_logger:info_report({contacted_rabbitmq, RabbitNode}),
- ok.
-
-setup_schema() ->
- mnesia:stop(),
- case mnesia:create_schema([node()]) of
- ok -> ok;
- {error, {_, {already_exists, _}}} -> ok
- end,
- ok = mnesia:start(),
- ok = create_table(rabbithub_lease,
- [{attributes, record_info(fields, rabbithub_lease)},
- {disc_copies, [node()]}]),
- ok = create_table(rabbithub_subscription_pid,
- [{attributes, record_info(fields, rabbithub_subscription_pid)}]),
- ok = mnesia:wait_for_tables([rabbithub_lease,
- rabbithub_subscription_pid],
- 5000),
- ok.
-
-create_table(Name, Params) ->
- case mnesia:create_table(Name, Params) of
- {atomic, ok} ->
- ok;
- {aborted, {already_exists, Name}} ->
- ok
- end.
View
@@ -1,56 +1,14 @@
-%% @doc Supervisor for the rabbithub application.
-
-module(rabbithub_sup).
-behaviour(supervisor).
-%% External exports
--export([start_link/0, upgrade/0]).
-
-%% supervisor callbacks
+-export([start_link/0]).
-export([init/1]).
-%% @spec start_link() -> ServerRet
-%% @doc API for starting the supervisor.
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-%% @spec upgrade() -> ok
-%% @doc Add processes if necessary.
-upgrade() ->
- {ok, {_, Specs}} = init([]),
-
- Old = sets:from_list(
- [Name || {Name, _, _, _} <- supervisor:which_children(?MODULE)]),
- New = sets:from_list([Name || {Name, _, _, _, _, _} <- Specs]),
- Kill = sets:subtract(Old, New),
-
- sets:fold(fun (Id, ok) ->
- supervisor:terminate_child(?MODULE, Id),
- supervisor:delete_child(?MODULE, Id),
- ok
- end, ok, Kill),
-
- [supervisor:start_child(?MODULE, Spec) || Spec <- Specs],
- ok.
-
-%% @spec init([]) -> SupervisorTree
-%% @doc supervisor callback.
init([]) ->
- Ip = case os:getenv("MOCHIWEB_IP") of false -> "0.0.0.0"; Any -> Any end,
- Port = case os:getenv("MOCHIWEB_PORT") of false -> 8000; P -> list_to_integer(P) end,
- %% rabbit_mochiweb:register_static_context(ContextRoot, ?MODULE, "priv/www"),
- WebConfig = [{ip, Ip},
- {port, Port},
- {docroot, rabbithub_deps:local_path(["priv", "www"])}],
-
- SubSup = {rabbithub_subscription_sup,
- {rabbithub_subscription_sup, start_link, []},
- permanent, 5000, supervisor, [rabbithub_subscription_sup]},
-
- Web = {rabbithub_web,
- {rabbithub_web, start, [WebConfig]},
- permanent, 5000, worker, dynamic},
-
- Processes = [SubSup, Web],
- {ok, {{one_for_one, 10, 10}, Processes}}.
+ {ok, {{one_for_one, 10, 10}, [{rabbithub_subscription_sup,
+ {rabbithub_subscription_sup, start_link, []},
+ permanent, 5000, supervisor, [rabbithub_subscription_sup]}]}}.
View
@@ -1,38 +1,30 @@
-%% @doc Web server for rabbithub.
-
-module(rabbithub_web).
--export([start/1, stop/0, loop/2]).
+-export([start/0, handle_req/1]).
-include("rabbithub.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").
--define(APPLICATION_XSLT, (rabbithub:canonical_basepath() ++ "static/application.xsl.xml")).
+-define(APPLICATION_XSLT, ("/" ++ rabbithub:canonical_basepath() ++ "/static/application.xsl.xml")).
-define(DEFAULT_SUBSCRIPTION_LEASE_SECONDS, (30 * 86400)).
-define(SUBSCRIPTION_LEASE_LIMIT, 1000 * 365 * 86400). %% Around a thousand years
-%% External API
-
-start(Options) ->
- {DocRoot, Options1} = get_option(docroot, Options),
- Loop = fun (Req) ->
- ?MODULE:loop(Req, DocRoot)
- end,
- ok = rabbithub_subscription:start_subscriptions(),
- mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
+start() ->
+ application:start(rabbit_mochiweb),
+ rabbit_mochiweb:register_context_handler(rabbithub:canonical_basepath(),
+ fun (Req) -> ?MODULE:handle_req(Req) end).
-stop() ->
- mochiweb_http:stop(?MODULE).
-
-loop(Req, DocRoot) ->
- {Path, Query, _Fragment} = mochiweb_util:urlsplit_path(Req:get(raw_path)),
+handle_req(Req) ->
+ {FullPath, Query, _Fragment} = mochiweb_util:urlsplit_path(Req:get(raw_path)),
+ %% plus one for the "/", plus one for the 1-based indexing for substr:
+ Path = string:substr(FullPath, length(rabbithub:canonical_basepath()) + 2),
ParsedQuery = mochiweb_util:parse_qs(Query),
case re:split(Path, "/", [{parts, 4}]) of
[<<>>, <<"static">> | _] ->
- handle_static(Path, DocRoot, Req);
+ handle_static(Path, Req);
[<<>>, <<>>] ->
- handle_static(Path, DocRoot, Req);
+ handle_static(Path, Req);
[<<>>, Facet, ResourceType, Name] ->
case check_resource_type(ResourceType) of
{ok, ResourceTypeAtom} ->
@@ -68,9 +60,12 @@ check_facet('POST', "subscribe", "subscribe", _) -> {auth_required, [read]};
check_facet('POST', "subscribe", "unsubscribe", _) -> {auth_required, []};
check_facet(_Method, _Facet, _HubMode, _ResourceType) -> invalid_operation.
-handle_static("/" ++ StaticFile, DocRoot, Req) ->
+handle_static("/" ++ StaticFile, Req) ->
case Req:get(method) of
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
+ {file, Here} = code:is_loaded(?MODULE),
+ ModuleRoot = filename:dirname(filename:dirname(Here)),
+ DocRoot = filename:join(ModuleRoot, "priv/www"),
Req:serve_file(StaticFile, DocRoot);
'POST' ->
case StaticFile of
@@ -80,7 +75,7 @@ handle_static("/" ++ StaticFile, DocRoot, Req) ->
_ ->
Req:respond({501, [], "Invalid HTTP method"})
end;
-handle_static(_OtherPath, _DocRoot, Req) ->
+handle_static(_OtherPath, Req) ->
Req:respond({400, [], "Invalid path"}).
param(ParsedQuery, Key, DefaultValue) ->
@@ -667,6 +662,3 @@ perform_request(Method, Facet, HubMode, _ResourceType, Resource, ParsedQuery, Re
handle_hub_post(Req) ->
Req:respond({200, [], "You posted!"}).
-
-get_option(Option, Options) ->
- {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.

0 comments on commit dce2680

Please sign in to comment.