Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move webmachine from riak_core. #34

Merged
merged 3 commits into from
Sep 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
{eunit_opts, [verbose]}.
{deps, [
{riak_pb, "2.0.0.3", {git, "git://github.com/basho/riak_pb.git", {tag, "2.0.0.3"}}},
{webmachine, "1.10.4", {git, "git://github.com/basho/webmachine.git", {tag, "1.10.4p1"}}},
{riak_core, ".*", {git, "git://github.com/basho/riak_core.git", {branch, "develop"}}}
]}.
44 changes: 29 additions & 15 deletions src/riak_api_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
-define(CHILD(I, Type, Args), {I, {I, start_link, Args}, permanent, 5000, Type, [I]}).
-define(LNAME(IP, Port), lists:flatten(io_lib:format("~p:~p", [IP, Port]))).
-define(LISTENER(IP, Port), {?LNAME(IP, Port),
{riak_api_pb_listener, start_link, [IP, Port]},
permanent, 5000, worker, [riak_api_pb_listener]}).
-define(LNAME(IP, Port), lists:flatten(io_lib:format("pb://~p:~p", [IP, Port]))).
-define(PB_LISTENER(IP, Port), {?LNAME(IP, Port),
{riak_api_pb_listener, start_link, [IP, Port]},
permanent, 5000, worker, [riak_api_pb_listener]}).
%% @doc Starts the supervisor.
-spec start_link() -> {ok, pid()} | {error, term()}.
start_link() ->
Expand All @@ -47,18 +47,32 @@ start_link() ->
MaxT :: pos_integer(),
ChildSpec :: supervisor:child_spec().
init([]) ->
Listeners = riak_api_pb_listener:get_listeners(),
Helper = ?CHILD(riak_api_pb_registration_helper, worker),
Registrar = ?CHILD(riak_api_pb_registrar, worker),
NetworkProcesses = if Listeners /= [] ->
[?CHILD(riak_api_pb_sup, supervisor)] ++
listener_specs(Listeners);
true ->
lager:info("No PB listeners were configured,"
" PB connections will be disabled."),
[]
end,
PBProcesses = pb_processes(riak_api_pb_listener:get_listeners()),
WebProcesses = web_processes(riak_api_web:get_listeners()),
NetworkProcesses = PBProcesses ++ WebProcesses,
{ok, {{one_for_one, 10, 10}, [Helper, Registrar|NetworkProcesses]}}.

listener_specs(Pairs) ->
[ ?LISTENER(IP, Port) || {IP, Port} <- Pairs ].
%% Generates child specs from the HTTP/HTTPS listener configuration.
%% @private
web_processes([]) ->
lager:info("No HTTP/HTTPS listeners were configured, HTTP connections will be disabled."),
[];
web_processes(Listeners) ->
lists:flatten([ web_listener_spec(Scheme, Binding) ||
{Scheme, Binding} <- Listeners ]).

web_listener_spec(Scheme, Binding) ->
riak_api_web:binding_config(Scheme, Binding).

%% Generates child specs from the PB listener configuration.
%% @private
pb_processes([]) ->
lager:info("No PB listeners were configured, PB connections will be disabled."),
[];
pb_processes(Listeners) ->
[?CHILD(riak_api_pb_sup, supervisor)| pb_listener_specs(Listeners)].

pb_listener_specs(Pairs) ->
[ ?PB_LISTENER(IP, Port) || {IP, Port} <- Pairs ].
90 changes: 90 additions & 0 deletions src/riak_api_web.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
%% -------------------------------------------------------------------
%%
%% riak_api_web: setup Riak's HTTP interface
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

%% @doc Convenience functions for setting up the HTTP interface
%% of Riak.
-module(riak_api_web).


-export([get_listeners/0,
binding_config/2]).

get_listeners() ->
get_listeners(http) ++ get_listeners(https).

get_listeners(Scheme) ->
Listeners = case app_helper:try_envs([{riak_api, Scheme},
{riak_core, Scheme}], []) of
{riak_api, Scheme, List} when is_list(List) ->
List;
{riak_core, Scheme, List} when is_list(List) ->
lager:warning("Setting riak_core/~s is deprecated, please use riak_api/~s", [Scheme, Scheme]),
List;
_ ->
[]
end,
lists:usort([ {Scheme, Binding} || Binding <- Listeners ]).

binding_config(Scheme, Binding) ->
{Ip, Port} = Binding,
Name = spec_name(Scheme, Ip, Port),
Config = spec_from_binding(Scheme, Name, Binding),

{Name,
{webmachine_mochiweb, start, [Config]},
permanent, 5000, worker, [mochiweb_socket_server]}.

spec_from_binding(http, Name, {Ip, Port}) ->
lists:flatten([{name, Name},
{ip, Ip},
{port, Port},
{nodelay, true}],
common_config());

spec_from_binding(https, Name, {Ip, Port}) ->
Etc = app_helper:get_env(riak_core, platform_etc_dir, "etc"),
SslOpts = app_helper:get_env(riak_core, ssl,
[{certfile, filename:join(Etc, "cert.pem")},
{keyfile, filename:join(Etc, "key.pem")}]),
lists:flatten([{name, Name},
{ip, Ip},
{port, Port},
{ssl, true},
{ssl_opts, SslOpts},
{nodelay, true}],
common_config()).

spec_name(Scheme, Ip, Port) ->
FormattedIP = if is_tuple(Ip); tuple_size(Ip) == 4 ->
inet_parse:ntoa(Ip);
is_tuple(Ip); tuple_size(Ip) == 8 ->
[$[, inet_parse:ntoa(Ip), $]];
true -> Ip
end,
lists:flatten(io_lib:format("~s://~s:~p", [Scheme, FormattedIP, Port])).

common_config() ->
[{log_dir, app_helper:get_env(riak_api, http_logdir,
app_helper:get_env(riak_core, platform_log_dir, "log"))},
{backlog, 128},
{dispatch, [{[], riak_api_wm_urlmap, []}
]}].
81 changes: 81 additions & 0 deletions src/riak_api_wm_urlmap.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
%% -------------------------------------------------------------------
%%
%% riak_api_wm_urlmap: expose the roots of registered Webmachine resources
%%
%% Copyright (c) 2007-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

%% @doc This module provides a Webmachine resource that lists the
%% URLs for other resources available on this host.
%%
%% Links to Riak resources will be added to the Link header in
%% the form:
%%```
%% <URL>; rel="RESOURCE_NAME"
%%'''
%% HTML output of this resource is a list of link tags like:
%%```
%% <a href="URL">RESOURCE_NAME</a>
%%'''
%% JSON output of this resource in an object with elements like:
%%```
%% "RESOURCE_NAME":"URL"
%%'''
-module(riak_api_wm_urlmap).
-export([
init/1,
resource_exists/2,
content_types_provided/2,
to_html/2,
to_json/2
]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) ->
{ok, service_list()}.

resource_exists(RD, Services) ->
{true, add_link_header(RD, Services), Services}.

add_link_header(RD, Services) ->
wrq:set_resp_header(
"Link",
string:join([ ["<",Uri,">; rel=\"",Resource,"\""]
|| {Resource, Uri} <- Services ],
","),
RD).

content_types_provided(RD, Services) ->
{[{"text/html", to_html},{"application/json", to_json}], RD, Services}.

to_html(RD, Services) ->
{["<html><body><ul>",
[ ["<li><a href=\"", Uri, "\">", Resource, "</a></li>"]
|| {Resource, Uri} <- Services ],
"</ul></body></html>"],
RD, Services}.

to_json(RD, Services) ->
{mochijson:encode({struct, Services}), RD, Services}.

service_list() ->
Dispatch = webmachine_router:get_routes(),
lists:usort(
[{atom_to_list(Resource), "/"++UriBase}
|| {[UriBase|_], Resource, _} <- Dispatch]).