| commit 11: | baf2cb3c5b70 |
| parent 10: | 66eeb354a92c |
| branch: | default |
simple extract_args & json decoding request handler factories
Changed (Δ988 bytes):
raw changeset »
ebin/routy.app (2 lines added, 2 lines removed)
ebin/routy.appup (13 lines added, 1 lines removed)
src/routy_handlers.erl (22 lines added, 4 lines removed)
Up to file-list ebin/routy.app:
1 |
1 |
{application, routy, [ |
2 |
2 |
{description, "HTTP Request Routing"}, |
3 |
{vsn, "1. |
|
3 |
{vsn, "1.5"}, |
|
4 |
4 |
{mod, {routy, []}}, |
5 |
{registered, [ |
|
5 |
{registered, []}, |
|
6 |
6 |
{modules, [routy, routy_sup, routy_util]}, |
7 |
7 |
{applications, [kernel, stdlib, elib]}, |
8 |
8 |
{env, [ |
Up to file-list ebin/routy.appup:
1 |
{"1. |
|
1 |
{"1.5", [ |
|
2 |
{"1.4", [ |
|
3 |
{load_module, routy_util}, |
|
4 |
{load_module, routy_handlers}, |
|
5 |
{load_module, routy}, |
|
6 |
{apply, {routy, config_change, [[],[],[]]}} |
|
7 |
]}, |
|
2 |
8 |
{"1.3", [ |
3 |
9 |
{load_module, routy}, |
4 |
10 |
{load_module, routy_util} |
5 |
11 |
]} |
6 |
12 |
], [ |
13 |
{"1.4", [ |
|
14 |
{load_module, routy_util}, |
|
15 |
{load_module, routy_handlers}, |
|
16 |
{load_module, routy}, |
|
17 |
{apply, {routy, config_change, [[],[],[]]}} |
|
18 |
]}, |
|
7 |
19 |
{"1.3", [ |
8 |
20 |
{load_module, routy}, |
9 |
21 |
{load_module, routy_util} |
Up to file-list src/routy_handlers.erl:
7 |
7 |
-include_lib("yaws/include/yaws.hrl"). |
8 |
8 |
-include_lib("yaws/include/yaws_api.hrl"). |
9 |
9 |
|
10 |
||
11 |
-export([extract_args/2, extract_all_args/2, echo/1, nop/2, authkey/2]). |
|
10 |
-export([decode_json/2]). |
|
11 |
-export([extract_args/1, extract_args/2, extract_all_args/2, echo/1, nop/2, authkey/2]). |
|
12 |
12 |
-export([redirect_template/2, redirect_template_from_templatedir/2]). |
13 |
13 |
-export([redirect_template_fun/2, redirect_template_from_templatedir_fun/2]). |
14 |
14 |
|
15 |
||
15 |
decode_json(Module, Function) -> |
|
16 |
fun (Req, _) -> |
|
17 |
case json:decode_string(binary_to_list(Req#arg.clidata)) of |
|
18 |
{error, Err} -> |
|
19 |
throw(badarg); |
|
20 |
{ok, Json} -> |
|
21 |
routy_util:try_route(Req, (Req#arg.req)#http_request.method, Module, Function, [Json]) |
|
22 |
end |
|
23 |
end. |
|
16 |
24 |
|
17 |
25 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
18 |
26 |
%% extract_args handler factory %% |
19 |
27 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
20 |
28 |
|
29 |
extract_args({Module, Function, ListParameters}) -> |
|
30 |
fun (Req, _) -> |
|
31 |
Args = case (Req#arg.req)#http_request.method of |
|
32 |
'GET' -> yaws_api:parse_query(Req); |
|
33 |
'POST' -> yaws_api:parse_post(Req) |
|
34 |
end, |
|
35 |
ParsedArgs = routy_util:make_args(ListParameters, Args), |
|
36 |
routy_util:try_route(Req, (Req#arg.req)#http_request.method, Module, Function, ParsedArgs) |
|
37 |
end. |
|
38 |
||
21 |
39 |
%% This request handler factory returns a request handler |
22 |
40 |
%% which will extract the required parameters and call the function |
23 |
41 |
%% reference passed as a parameter |
| … | … | @@ -55,7 +73,7 @@ redirect_template_fun(ErlyDTLTemplateFil |
55 |
73 |
Template = list_to_atom(binary_to_list(term_to_binary(erlang:phash2(ErlyDTLTemplateFilename)))), |
56 |
74 |
erlydtl:compile(ErlyDTLTemplateFilename, Template), |
57 |
75 |
|
58 |
fun ( |
|
76 |
fun (_Req, _) -> |
|
59 |
77 |
{ok, RenderedPage} = Template:render(ParamsCreator()), |
60 |
78 |
[{status, 200}, {content, "text/html", RenderedPage}] |
61 |
79 |
end. |
