Created
August 16, 2018 22:08
-
-
Save bochsdbg/f9cb9448218de00ec6cfbab5f0128d1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(test_method). | |
%-compile([export_all, nowarn_export_all]). | |
% standard callbacks | |
-export([init/2, allowed_methods/2, content_types_provided/2, content_types_accepted/2, resource_exists/2]). | |
% custom callbacks | |
-export([method_jsone/2]). | |
-export([create_jsone/2]). | |
-export([create_jsone2/4]). | |
-export([valid_path/1]). | |
init(Req, Opts) -> | |
{cowboy_rest, Req, Opts}. | |
allowed_methods(Req, State) -> | |
{[<<"GET">>, <<"POST">>], Req, State}. | |
%{[<<"POST">>], Req, State}. | |
content_types_provided(Req, State) -> | |
{[ | |
{ {<<"text">>, <<"plain">>, []}, method_jsone}, | |
{ {<<"text">>, <<"html">>, []}, method_jsone} | |
%{ {<<"application">>, <<"json">>, []}, method_jsone} | |
], Req, State}. | |
content_types_accepted(Req, State) -> | |
{ [{ {<<"application">>, <<"x-www-form-urlencoded">>, []}, create_jsone}], Req, State}. | |
resource_exists(Req, State) -> | |
io:format("resource_exists State: ~p~n", [State]), | |
{ok, Params, Req2} = cowboy_req:read_urlencoded_body(Req), | |
io:format("Id: ~p~n",[Params]), | |
case lists:keyfind(<<"path">>, 1, Params) of | |
false -> {false, Req2, [{path, <<>>} | State]}; | |
{_, Path} -> {valid_path(Path), Req2, [{path, Path} | State]} | |
end. | |
create_jsone(Req, State) -> | |
% io:format("~p~n", ["cowboy read_urlencoded_body:"]), | |
% io:format("~p~n", [cowboy_req:read_urlencoded_body(Req)]), | |
% {ok, [{<<"test_id">>, Value}], Req2} = cowboy_req:read_urlencoded_body(Req), | |
% io:format("~p~n",["value:"]), | |
% io:format("~p~n",[Value]), | |
% io:format("~p~n",["method:"]), | |
% io:format("~p~n",[cowboy_req:method(Req2)]), | |
% io:format("~p~n",["system variables from sys.config:"]), | |
% io:format("~p~n",[application:get_env(p2_core, pgs_user, undefined)]), | |
% Some_Id = <<"777">>, | |
{path, Path} = lists:keyfind(path, 1, State), | |
io:format("create_jsone: ~p~n", [Path]), | |
?MODULE:create_jsone2(cowboy_req:method(Req), Path, Req, State). | |
% helper | |
create_jsone2(<<"POST">>, Some_Id, Req2, State) -> | |
Req3 = cowboy_req:set_resp_body(["Hello world! path: ", Some_Id], Req2), | |
{true, Req3, State}; | |
create_jsone2(_, _, Req2, State) -> | |
{true, Req2, State}. | |
% cowboy_req:binding(test_id, Req, undefined) % get value from router bind,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.binding/ | |
% #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), % get_params,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.match_qs/ | |
% {ok, [{<<"paste">>, Value}], Req2} = cowboy_req:read_urlencoded_body(Req), % post_params,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.read_urlencoded_body/ | |
method_jsone(Req, not_found) -> | |
{ <<"{\"status\":\"error\", \"error_message\":\"invalid data\"}">>, Req, not_found}; | |
method_jsone(Req, Id) -> | |
{ <<"{\"status\":\"ok\", \"data\":[{\"id\":\"", Id/binary, "\"}]}">>, Req, Id}. | |
% %%%%%%%%%%%%%%%%%%%%%%%55 | |
valid_path(<<>>) -> true; | |
valid_path(<<$., _T/binary>>) -> false; | |
valid_path(<<$/, _T/binary>>) -> false; | |
valid_path(<<_Char, T/binary>>) -> valid_path(T). | |
% Escape some HTML characters that might make a fuss | |
escape_html_chars(Bin) -> | |
<< <<(escape_html_char(B))/binary>> || <<B>> <= Bin >>. | |
escape_html_char($<) -> <<"<">>; | |
escape_html_char($>) -> <<">">>; | |
escape_html_char($&) -> <<"&">>; | |
escape_html_char(C) -> <<C>>. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment