bryan / wmexamples
Example resources for Webmachine (http://bitbucket.org/justin/webmachine). This project contains several sample webmachine resources that I've posted elsewhere at various times. This is a complete Webmachine app ready to be run and examined.
Clone this repository (size: 42.5 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/bryan/wmexamples/
| commit 9: | bd3e832915bf |
| parent 8: | 2b30cccfd641 |
| branch: | default |
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
10 months ago
Changed (Δ2.8 KB):
raw changeset »
priv/www/index.css (20 lines added, 0 lines removed)
src/html.erl (3 lines added, 0 lines removed)
src/wmexamples_resource.erl (61 lines added, 6 lines removed)
Up to file-list priv/www/index.css:
| … | … | @@ -21,3 +21,23 @@ span.pathmatch { |
21 |
21 |
pre { |
22 |
22 |
margin: 0px; |
23 |
23 |
} |
24 |
||
25 |
div, p { |
|
26 |
border-left: solid 10px #fff; |
|
27 |
padding-left: 1em; |
|
28 |
} |
|
29 |
||
30 |
div.pass, div.match, p.match { |
|
31 |
border-left: solid 10px #090; |
|
32 |
padding-left: 1em; |
|
33 |
} |
|
34 |
||
35 |
p.nomatch { |
|
36 |
border-left: solid 10px #900; |
|
37 |
padding-left: 1em; |
|
38 |
background: #fee; |
|
39 |
} |
|
40 |
||
41 |
p.match, div.match { |
|
42 |
background: #efe; |
|
43 |
} |
43 |
43 |
?TAG(th). |
44 |
44 |
?TAG(tr). |
45 |
45 |
?TAG(td). |
46 |
?TAG(form). |
|
47 |
?TAG(label). |
|
48 |
?TAG(input). |
|
46 |
49 |
|
47 |
50 |
html(_Attrs, Content) -> |
48 |
51 |
[<<"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n">>, |
Up to file-list src/wmexamples_resource.erl:
6 |
6 |
-export([init/1, to_html/2]). |
7 |
7 |
|
8 |
8 |
-import(html, [html/2, head/2, body/2, |
9 |
linkblock/2, |
|
9 |
linkblock/2, divblock/2, |
|
10 |
10 |
h1/2, p/2, pre/2, span/2, |
11 |
table/2, th/2, tr/2, td/2 |
|
11 |
table/2, th/2, tr/2, td/2, |
|
12 |
form/2, label/2, input/2]). |
|
12 |
13 |
|
13 |
14 |
-include_lib("webmachine/include/webmachine.hrl"). |
14 |
15 |
|
| … | … | @@ -22,16 +23,70 @@ to_html(ReqData, State) -> |
22 |
23 |
{"type", "text/css"}, |
23 |
24 |
{"href", "index.css"}], [])]), |
24 |
25 |
"\n",body([], [h1([], "Welcome to wmexamples"), |
25 |
dispatch_details( |
|
26 |
dispatch_details(ReqData)]) |
|
26 |
27 |
]), |
27 |
28 |
ReqData, State}. |
28 |
29 |
|
29 |
30 |
-define(DISPATCH_FILENAME, "priv/dispatch.conf"). |
30 |
31 |
|
31 |
dispatch_details( |
|
32 |
dispatch_details(ReqData) -> |
|
32 |
33 |
{ok, Dispatch} = file:consult(?DISPATCH_FILENAME), |
33 |
[p([], [?DISPATCH_FILENAME, " is exposing the following paths:"]), |
|
34 |
[["\n",dispatch_detail(D)] || D <- Dispatch]]. |
|
34 |
Url = case wrq:get_qs_value("url", ReqData) of |
|
35 |
"http://"++Rest -> |
|
36 |
case string:tokens(Rest, "/") of |
|
37 |
[_|Tokens] -> [$/|string:join(Tokens, "/")]; |
|
38 |
_ -> "/" |
|
39 |
end; |
|
40 |
Rest when is_list(Rest), Rest /= [] -> Rest; |
|
41 |
_ -> undefined |
|
42 |
end, |
|
43 |
HasMatch = if is_list(Url) -> |
|
44 |
case webmachine_dispatcher:dispatch(Url, Dispatch) of |
|
45 |
{_Module, _, _, _, _, _} -> |
|
46 |
true; |
|
47 |
{no_dispatch_match, _} -> |
|
48 |
false |
|
49 |
end; |
|
50 |
true -> |
|
51 |
not_attempted |
|
52 |
end, |
|
53 |
[form([{"action", "http://localhost:8000/"}, |
|
54 |
{"method", "GET"}], |
|
55 |
p([{"id", "testline"}, |
|
56 |
{"class", case HasMatch of |
|
57 |
true -> "match"; |
|
58 |
false -> "nomatch"; |
|
59 |
not_attempted -> "none" |
|
60 |
end}], |
|
61 |
[label([], "Test Dispatch to:"), |
|
62 |
input([{"name", "url"}, |
|
63 |
{"type", "text"}, |
|
64 |
{"value", if is_list(Url) -> Url; |
|
65 |
true -> [] |
|
66 |
end}], []), |
|
67 |
input([{"type", "submit"}, {"value", "Test"}], [])])), |
|
68 |
p([], [?DISPATCH_FILENAME, " is exposing the following paths:"]), |
|
69 |
lists:reverse(element(2, |
|
70 |
lists:foldl(fun(D, {true, Acc}) -> |
|
71 |
Matches = case webmachine_dispatcher:dispatch( |
|
72 |
Url, [D]) of |
|
73 |
{_Mod, _, _, _, _, _} -> |
|
74 |
true; |
|
75 |
{no_dispatch_match, _} -> |
|
76 |
false |
|
77 |
end, |
|
78 |
{not Matches, |
|
79 |
[["\n",divblock([{"class", if Matches -> "match"; |
|
80 |
true -> "pass" |
|
81 |
end}], |
|
82 |
dispatch_detail(D))] |
|
83 |
|Acc]}; |
|
84 |
(D, {False, Acc}) -> |
|
85 |
{False, |
|
86 |
[["\n",divblock([], dispatch_detail(D))]|Acc]} |
|
87 |
end, |
|
88 |
{HasMatch, []}, |
|
89 |
Dispatch)))]. |
|
35 |
90 |
|
36 |
91 |
dispatch_detail({Path, Resource, Args}) -> |
37 |
92 |
table([], |
