Commits

Anonymous committed bd3e832

sample dispatch tester

enter a URL into the form on the page hosted at /, the page you get back will highlight which resource the request was dispatched to

  • Participants
  • Parent commits 2b30ccc

Comments (0)

Files changed (3)

File priv/www/index.css

 
 pre {
     margin: 0px;
-}
+}
+
+div, p {
+    border-left: solid 10px #fff;
+    padding-left: 1em;
+}
+
+div.pass, div.match, p.match {
+    border-left: solid 10px #090;
+    padding-left: 1em;
+}
+
+p.nomatch {
+    border-left: solid 10px #900;
+    padding-left: 1em;
+    background: #fee;
+}
+
+p.match, div.match {
+    background: #efe;
+}

File src/html.erl

 ?TAG(th).
 ?TAG(tr).
 ?TAG(td).
+?TAG(form).
+?TAG(label).
+?TAG(input).
 
 html(_Attrs, Content) ->
     [<<"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n">>,

File src/wmexamples_resource.erl

 -export([init/1, to_html/2]).
 
 -import(html, [html/2, head/2, body/2,
-               linkblock/2,
+               linkblock/2, divblock/2,
                h1/2, p/2, pre/2, span/2,
-               table/2, th/2, tr/2, td/2]).
+               table/2, th/2, tr/2, td/2,
+               form/2, label/2, input/2]).
 
 -include_lib("webmachine/include/webmachine.hrl").
 
                                      {"type", "text/css"},
                                      {"href", "index.css"}], [])]),
            "\n",body([], [h1([], "Welcome to wmexamples"),
-                          dispatch_details()])
+                          dispatch_details(ReqData)])
           ]),
      ReqData, State}.
 
 -define(DISPATCH_FILENAME, "priv/dispatch.conf").
 
-dispatch_details() ->
+dispatch_details(ReqData) ->
     {ok, Dispatch} = file:consult(?DISPATCH_FILENAME),
-    [p([], [?DISPATCH_FILENAME, " is exposing the following paths:"]),
-     [["\n",dispatch_detail(D)] || D <- Dispatch]].
+    Url = case wrq:get_qs_value("url", ReqData) of
+              "http://"++Rest ->
+                  case string:tokens(Rest, "/") of
+                      [_|Tokens] -> [$/|string:join(Tokens, "/")];
+                      _ -> "/"
+                  end;
+              Rest when is_list(Rest), Rest /= [] -> Rest;
+              _ -> undefined
+          end,
+    HasMatch = if is_list(Url) ->
+                       case webmachine_dispatcher:dispatch(Url, Dispatch) of
+                           {_Module, _, _, _, _, _} ->
+                               true;
+                           {no_dispatch_match, _} ->
+                               false
+                       end;
+                   true ->
+                       not_attempted
+               end,
+    [form([{"action", "http://localhost:8000/"},
+              {"method", "GET"}],
+             p([{"id", "testline"},
+                {"class", case HasMatch of
+                      true -> "match";
+                      false -> "nomatch";
+                      not_attempted -> "none"
+                  end}],
+       [label([], "Test Dispatch to:"),
+              input([{"name", "url"},
+                     {"type", "text"},
+                     {"value", if is_list(Url) -> Url;
+                                  true -> []
+                               end}], []),
+              input([{"type", "submit"}, {"value", "Test"}], [])])),
+     p([], [?DISPATCH_FILENAME, " is exposing the following paths:"]),
+     lists:reverse(element(2,
+        lists:foldl(fun(D, {true, Acc}) ->
+                            Matches = case webmachine_dispatcher:dispatch(
+                                             Url, [D]) of
+                                          {_Mod, _, _, _, _, _} ->
+                                              true;
+                                          {no_dispatch_match, _} ->
+                                              false
+                                      end,
+                            {not Matches,
+                             [["\n",divblock([{"class", if Matches -> "match";
+                                                           true -> "pass"
+                                                        end}],
+                                             dispatch_detail(D))]
+                              |Acc]};
+                       (D, {False, Acc}) ->
+                            {False,
+                             [["\n",divblock([], dispatch_detail(D))]|Acc]}
+                    end,
+                    {HasMatch, []},
+                    Dispatch)))].
 
 dispatch_detail({Path, Resource, Args}) ->
     table([],