Commits

Anonymous committed 3dff242 Merge

merge

  • Participants
  • Parent commits 0ac9403, 10b2597

Comments (0)

Files changed (6)

File demo/stickynotes/riak-config.erlenv

 
 {jiak_name, "jiak"}.
 {riak_web_ip, "127.0.0.1"}.
-{riak_web_port, 9000}.
+{riak_web_port, 8098}.
 {riak_web_logdir, "priv/weblogs"}.
 {jiak_buckets, [notes, groups]}. %% just to get atoms loaded in node
 

File demo/stickynotes/src/jiak_proxy.erl

-%% @author Bryan Fink
-%% @doc jiak_proxy is intended to be a simple webmachine resource
-%%      for proxying Webmachine requests to Jiak.
-%%
-%%      Note: this is not a production-quality proxy resource.
-%%      For something that will be used in a real application, it
-%%      is recommended that ibrowse be used instead of the inets
-%%      http client.
--module(jiak_proxy).
--export([init/1,
-         service_available/2]).
--include_lib("webmachine/include/webmachine.hrl").
-
-init(Config) -> {ok, Config}.
-
-%% request to jiak is made in service_available, such that
-%% if jiak isn't up, we return 503 Service Unavailable, as expected
-service_available(RP, C={_ExternalPath, JiakPath}) ->
-    %% point path at jiak server
-    Path = lists:append(
-             [JiakPath,
-              wrq:disp_path(RP),
-              case wrq:req_qs(RP) of
-                  [] -> [];
-                  Qs -> [$?|mochiweb_util:urlencode(Qs)]
-              end]),
-
-    %% translate webmachine details to ibrowse details
-    Headers = clean_request_headers(
-                mochiweb_headers:to_list(wrq:req_headers(RP))),
-    Method = wm_to_http_method(wrq:method(RP)),
-    Req = case wrq:req_body(RP) of
-              Empty when Empty==undefined;
-                         Empty==[];
-                         Empty==<<>> ->
-                  {Path, Headers};
-              B ->
-                  {Path, Headers,
-                   wrq:get_req_header("content-type", RP), B}
-          end,
-    case http:request(Method, Req, [{autoredirect, false}], []) of
-        {ok, {{_, Status, _}, JiakHeaders, RespBody}} ->
-            RespHeaders = fix_location(JiakHeaders, C),
-
-            %% stop resource processing here and return whatever
-            %% jiak wanted to return
-            {{halt, Status},
-             wrq:set_resp_headers(RespHeaders,
-                                  wrq:set_resp_body(RespBody, RP)),
-             C};
-        _ ->
-            {false, RP, C}
-    end.
-
-%% ibrowse will recalculate Host and Content-Length headers,
-%% and will muck them up if they're manually specified
-clean_request_headers(Headers) ->
-    [{if is_atom(K) -> atom_to_list(K);
-         true -> K end,
-      V} || {K,V} <- Headers, K /= 'Host', K /= 'Content-Length'].
-
-%% webmachine expresses method as all-caps string or atom,
-%% while ibrowse uses all-lowercase atom
-wm_to_http_method(Method) when is_list(Method) ->
-    list_to_atom(string:to_lower(Method));
-wm_to_http_method(Method) when is_atom(Method) ->
-    wm_to_http_method(atom_to_list(Method)).
-
-%% jiak returns a fully-qualified URI in Location -
-%% hack off the jiak host, and drop in this proxy host
-fix_location([], _) -> [];
-fix_location([{"Location", JiakDataPath}|Rest],
-             {ExternalPath, JiakPath}) ->
-    DataPath = lists:nthtail(length(JiakPath), JiakDataPath),
-    [{"Location", ExternalPath++DataPath}|Rest];
-fix_location([H|T], C) ->
-    [H|fix_location(T, C)].

File demo/stickynotes/src/jiak_proxy_ibrowse.erl

+%% @author Bryan Fink
+%% @doc jiak_proxy_ibrowse is intended to be a simple webmachine
+%%      resource for proxying Webmachine requests to Jiak.  In theory,
+%%      it's general enough to be a simple proxy to most any other
+%%      HTTP service, but I make no guarantees about assumption it
+%%      makes that are Jiak-specific.
+%%
+%%      This resource performs the same task as jiak_proxy, but uses
+%%      ibrowse instead of inets http.
+%%
+%%      Load this with a dispatch line like:
+%%      {['*'], jiak_proxy_ibrowse, {ExternalPath, JiakPath}}.
+%%      Where:
+%%        ExternalPath is the base path to this resource, like
+%%          "http://localhost:8000/"
+%%        JiakPath is the base path to your Jiak server, like
+%%          "http://localhost:8098/"
+%%
+%%      Another useful example might be:
+%%        {["jiak", '*'], jiak_proxy_ibrowse,
+%%         {"http://localhost:8000/jiak/",
+%%          "http://localhost:8098/"}}
+%%      Which would redirect requests from
+%%        http://localhost:8000/jiak/BUCKET/KEY
+%%      to
+%%        http://localhost:8098/BUCKET/KEY
+-module(jiak_proxy_ibrowse).
+-export([init/1,
+         service_available/2]).
+-include_lib("webmachine/include/webmachine.hrl").
+
+init(Config) -> {ok, Config}.
+
+%% request to Jiak is made in service_available, such that
+%% if Jiak isn't up, we return 503 Service Unavailable, as expected
+service_available(RP, C={_ExternalPath, JiakPath}) ->
+    %% point path at Jiak server
+    Path = lists:append(
+             [JiakPath,
+              wrq:disp_path(RP),
+              case wrq:req_qs(RP) of
+                  [] -> [];
+                  Qs -> [$?|mochiweb_util:urlencode(Qs)]
+              end]),
+
+    %% translate webmachine details to ibrowse details
+    Headers = clean_request_headers(
+                mochiweb_headers:to_list(wrq:req_headers(RP))),
+    Method = wm_to_ibrowse_method(wrq:method(RP)),
+    ReqBody = case wrq:req_body(RP) of
+                  undefined -> [];
+                  B -> B
+              end,
+
+    case ibrowse:send_req(Path, Headers, Method, ReqBody) of
+        {ok, Status, JiakHeaders, RespBody} ->
+            RespHeaders = fix_location(JiakHeaders, C),
+
+            %% stop resource processing here and return whatever
+            %% Jiak wanted to return
+            {{halt, list_to_integer(Status)},
+             wrq:set_resp_headers(RespHeaders,
+                                  wrq:set_resp_body(RespBody, RP)),
+             C};
+        _ ->
+            {false, RP, C}
+    end.
+
+%% ibrowse will recalculate Host and Content-Length headers,
+%% and will muck them up if they're manually specified
+clean_request_headers(Headers) ->
+    [{K,V} || {K,V} <- Headers,
+              K /= 'Host', K /= 'Content-Length'].
+
+%% webmachine expresses method as all-caps string or atom,
+%% while ibrowse uses all-lowercase atom
+wm_to_ibrowse_method(Method) when is_list(Method) ->
+    list_to_atom(string:to_lower(Method));
+wm_to_ibrowse_method(Method) when is_atom(Method) ->
+    wm_to_ibrowse_method(atom_to_list(Method)).
+
+%% Jiak returns a fully-qualified URI in Location -
+%% hack off the Jiak host, and drop in this proxy host
+fix_location([], _) -> [];
+fix_location([{"Location", JiakDataPath}|Rest],
+             {ExternalPath, JiakPath}) ->
+    DataPath = lists:nthtail(length(JiakPath), JiakDataPath),
+    [{"Location", ExternalPath++DataPath}|Rest];
+fix_location([H|T], C) ->
+    [H|fix_location(T, C)].

File demo/stickynotes/src/jiak_proxy_inets.erl

+%% @author Bryan Fink
+%% @doc jiak_proxy_inets is intended to be a simple webmachine
+%%      resource for proxying Webmachine requests to Jiak.
+%%
+%%      Note: this is not a production-quality proxy resource.
+%%      For something that will be used in a real application, it
+%%      is recommended that ibrowse be used instead of the inets
+%%      http client.
+-module(jiak_proxy_inets).
+-export([init/1,
+         service_available/2]).
+-include_lib("webmachine/include/webmachine.hrl").
+
+init(Config) -> {ok, Config}.
+
+%% request to jiak is made in service_available, such that
+%% if jiak isn't up, we return 503 Service Unavailable, as expected
+service_available(RP, C={_ExternalPath, JiakPath}) ->
+    %% point path at jiak server
+    Path = lists:append(
+             [JiakPath,
+              wrq:disp_path(RP),
+              case wrq:req_qs(RP) of
+                  [] -> [];
+                  Qs -> [$?|mochiweb_util:urlencode(Qs)]
+              end]),
+
+    %% translate webmachine details to ibrowse details
+    Headers = clean_request_headers(
+                mochiweb_headers:to_list(wrq:req_headers(RP))),
+    Method = wm_to_http_method(wrq:method(RP)),
+    Req = case wrq:req_body(RP) of
+              Empty when Empty==undefined;
+                         Empty==[];
+                         Empty==<<>> ->
+                  {Path, Headers};
+              B ->
+                  {Path, Headers,
+                   wrq:get_req_header("content-type", RP), B}
+          end,
+    case http:request(Method, Req, [{autoredirect, false}], []) of
+        {ok, {{_, Status, _}, JiakHeaders, RespBody}} ->
+            RespHeaders = fix_location(JiakHeaders, C),
+
+            %% stop resource processing here and return whatever
+            %% jiak wanted to return
+            {{halt, Status},
+             wrq:set_resp_headers(RespHeaders,
+                                  wrq:set_resp_body(RespBody, RP)),
+             C};
+        _ ->
+            {false, RP, C}
+    end.
+
+%% ibrowse will recalculate Host and Content-Length headers,
+%% and will muck them up if they're manually specified
+clean_request_headers(Headers) ->
+    [{if is_atom(K) -> atom_to_list(K);
+         true -> K end,
+      V} || {K,V} <- Headers, K /= 'Host', K /= 'Content-Length'].
+
+%% webmachine expresses method as all-caps string or atom,
+%% while ibrowse uses all-lowercase atom
+wm_to_http_method(Method) when is_list(Method) ->
+    list_to_atom(string:to_lower(Method));
+wm_to_http_method(Method) when is_atom(Method) ->
+    wm_to_http_method(atom_to_list(Method)).
+
+%% jiak returns a fully-qualified URI in Location -
+%% hack off the jiak host, and drop in this proxy host
+fix_location([], _) -> [];
+fix_location([{"Location", JiakDataPath}|Rest],
+             {ExternalPath, JiakPath}) ->
+    DataPath = lists:nthtail(length(JiakPath), JiakDataPath),
+    [{"Location", ExternalPath++DataPath}|Rest];
+fix_location([H|T], C) ->
+    [H|fix_location(T, C)].

File demo/stickynotes/src/stickynotes.erl

     stickynotes_deps:ensure(),
     ensure_started(crypto),
     ensure_started(webmachine),
-    ensure_started(inets),
-    http:set_option(max_keep_alive_length, 0),
+    case application:start(ibrowse) of
+        OK when OK == ok;
+                OK == {error, {already_started, ibrowse}} ->
+            application:set_env(stickynotes, use_ibrowse, true);
+        {error, _} ->
+            ensure_started(inets),
+            http:set_option(max_keep_alive_length, 0)
+    end,
     application:start(stickynotes).
 
 %% @spec stop() -> ok
 %% @doc Stop the stickynotes server.
 stop() ->
     Res = application:stop(stickynotes),
-    application:stop(inets),
+    case get_app_env(use_ibrowse) of
+        true -> application:stop(ibrowse);
+        _    -> application:stop(inets)
+    end,
     application:stop(webmachine),
     application:stop(crypto),
     Res.

File demo/stickynotes/src/stickynotes_sup.erl

     External = lists:flatten(
                  io_lib:format("http://~s:~b/jiak/", [Ip, 8000])),
     Internal = lists:flatten(
-                 io_lib:format("http://~s:~b/jiak/",
+                 io_lib:format("http://~s:~b/~s/",
                                [proplists:get_value(riak_web_ip, RiakConfig),
-                                proplists:get_value(riak_web_port, RiakConfig)])),
-    Dispatch = [{["jiak",'*'], jiak_proxy, {External, Internal}},
+                                proplists:get_value(riak_web_port, RiakConfig),
+                                proplists:get_value(jiak_name, RiakConfig)])),
+    ProxyRes = case stickynotes:get_app_env(use_ibrowse) of
+                   true -> jiak_proxy_ibrowse;
+                   _    -> jiak_proxy_inets
+               end,
+    Dispatch = [{["jiak",'*'], ProxyRes, {External, Internal}},
                 {['*'], stickynotes_resource, ["priv/www/"]}],
-    WebConfig = [
-		 {ip, Ip},
+    WebConfig = [{ip, Ip},
 		 {port, 8000},
                  {log_dir, "priv/log"},
 		 {dispatch, Dispatch}],