Skip to content

Commit

Permalink
add a configuration URL, listing other resources available (bz://47)
Browse files Browse the repository at this point in the history
Configuration URL is always "/"
Resource list is created from Webmachine dispatch list
Link header is always added
HTML output is an unordered list of anchor tags
JSON output is an object with the resource module name as the key, and
   [the first element of] its dispatch URL as the value
  • Loading branch information
beerriot committed Nov 23, 2010
1 parent 1ebd294 commit a7fab79
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions ebin/riak_core.app
Expand Up @@ -40,6 +40,7 @@
riak_core_vnode_master,
riak_core_vnode_sup,
riak_core_web,
riak_core_wm_urlmap,
slide,
spiraltime,
vclock
Expand Down
2 changes: 1 addition & 1 deletion src/riak_core_web.erl
Expand Up @@ -46,4 +46,4 @@ config() ->
{port, app_helper:get_env(riak_core, web_port)},
{log_dir, app_helper:get_env(riak_core, web_logdir, "log")},
{backlog, 128},
{dispatch, []}].
{dispatch, [{[], riak_core_wm_urlmap, []}]}].
81 changes: 81 additions & 0 deletions src/riak_core_wm_urlmap.erl
@@ -0,0 +1,81 @@
%% -------------------------------------------------------------------
%%
%% Riak: A lightweight, decentralized key-value store.
%%
%% 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 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_core_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() ->
{ok, Dispatch} = application:get_env(webmachine, dispatch_list),
lists:usort(
[{atom_to_list(Resource), "/"++UriBase}
|| {[UriBase|_], Resource, _} <- Dispatch]).

0 comments on commit a7fab79

Please sign in to comment.