Permalink
Browse files

add a configuration URL, listing other resources available (bz://47)

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...
Bryan Fink
Bryan Fink committed Nov 12, 2010
1 parent 1ebd294 commit a7fab79e90d6b4f2f9f0a5a0caf1f1ff08313f02
Showing with 83 additions and 1 deletion.
  1. +1 −0 ebin/riak_core.app
  2. +1 −1 src/riak_core_web.erl
  3. +81 −0 src/riak_core_wm_urlmap.erl
View
@@ -40,6 +40,7 @@
riak_core_vnode_master,
riak_core_vnode_sup,
riak_core_web,
+ riak_core_wm_urlmap,
slide,
spiraltime,
vclock
View
@@ -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, []}]}].
@@ -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.