Skip to content

Commit

Permalink
KAZOO-1679: Redo crossbar bindings to allow more than one version
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Defebvre authored and k-anderson committed Nov 21, 2013
1 parent 466d2be commit 71aec65
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 69 deletions.

Large diffs are not rendered by default.

Expand Up @@ -9,7 +9,7 @@
%%% Karl Anderson
%%% Jon Blanton
%%%-------------------------------------------------------------------
-module(v1_util).
-module(api_util).

-export([is_cors_preflight/1
,is_cors_request/1
Expand All @@ -33,6 +33,7 @@
,halt/2
,content_type_matches/2
,ensure_content_type/1
,create_event_name/2
]).

-include("crossbar.hrl").
Expand Down Expand Up @@ -891,3 +892,9 @@ halt(Req0, #cb_context{resp_error_code=StatusCode}=Context) ->
lager:debug("setting status code: ~p", [StatusCode]),
{'ok', Req3} = cowboy_req:reply(StatusCode, Req2),
{'halt', Req3, Context}.

-spec create_event_name(cb_context:context(), ne_binary()) -> ne_binary().
create_event_name(#cb_context{api_version=ApiVersion}, Name) ->
<<ApiVersion/binary, "_resource.", Name/binary>>.


3 changes: 3 additions & 0 deletions applications/crossbar/src/cb_context.erl
Expand Up @@ -36,6 +36,7 @@
,resp_data/1, set_resp_data/2
,resp_status/1, set_resp_status/2
,resp_headers/1, set_resp_header/3, add_resp_header/3
,api_version/1, set_api_version/2

%% Special accessors
,req_value/2, req_value/3
Expand Down Expand Up @@ -66,6 +67,7 @@ doc(#cb_context{doc=Doc}) -> Doc.
resp_data(#cb_context{resp_data=RespData}) -> RespData.
resp_status(#cb_context{resp_status=RespStatus}) -> RespStatus.
resp_headers(#cb_context{resp_headers=RespHeaders}) -> RespHeaders.
api_version(#cb_context{api_version=ApiVersion}) -> ApiVersion.

%% Setters
set_account_id(#cb_context{}=Context, AcctId) -> Context#cb_context{account_id=AcctId}.
Expand All @@ -81,6 +83,7 @@ set_req_id(#cb_context{}=Context, ReqId) -> Context#cb_context{req_id=ReqId}.
set_doc(#cb_context{}=Context, Doc) -> Context#cb_context{doc=Doc}.
set_resp_data(#cb_context{}=Context, RespData) -> Context#cb_context{resp_data=RespData}.
set_resp_status(#cb_context{}=Context, RespStatus) -> Context#cb_context{resp_status=RespStatus}.
set_api_version(#cb_context{}=Context, ApiVersion) -> Context#cb_context{api_version=ApiVersion}.
set_resp_header(K, V, #cb_context{resp_headers=RespHeaders}=Context) ->
Context#cb_context{resp_headers=lists:keystore(K, 1, RespHeaders, {K, V})}.
add_resp_header(K, V, #cb_context{resp_headers=RespHeaders}=Context) ->
Expand Down
10 changes: 5 additions & 5 deletions applications/crossbar/src/crossbar.erl
Expand Up @@ -29,7 +29,7 @@ start_link() ->
_ = declare_exchanges(),
Dispatch = cowboy_router:compile([
%% {HostMatch, list({PathMatch, Handler, Opts})}
{'_', [{<<"/v1/[...]">>, 'v1_resource', []}
{'_', [{<<"/:version/[...]">>, 'api_resource', []}
,{'_', 'crossbar_default_handler', []}
]}
]),
Expand All @@ -48,8 +48,8 @@ start() ->
%%--------------------------------------------------------------------
-spec stop() -> 'ok'.
stop() ->
cowboy:stop_listener('v1_resource'),
cowboy:stop_listener('v1_resource_ssl'),
cowboy:stop_listener('api_resource'),
cowboy:stop_listener('api_resource_ssl'),
exit(whereis('crossbar_sup'), 'shutdown').

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -150,7 +150,7 @@ maybe_start_plaintext(Dispatch) ->
Workers = whapps_config:get_integer(?CONFIG_CAT, <<"workers">>, 100),

%% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
try cowboy:start_http('v1_resource', Workers
try cowboy:start_http('api_resource', Workers
,[{'port', Port}]
,[{'env', [{'dispatch', Dispatch}
,{'timeout', ReqTimeout}
Expand Down Expand Up @@ -183,7 +183,7 @@ start_ssl(Dispatch) ->
ReqTimeout = whapps_config:get_integer(?CONFIG_CAT, <<"request_timeout_ms">>, 10000),
Workers = whapps_config:get_integer(?CONFIG_CAT, <<"ssl_workers">>, 100),

try cowboy:start_https('v1_resource_ssl', Workers
try cowboy:start_https('api_resource_ssl', Workers
,SSLOpts
,[{'env', [{'dispatch', Dispatch}
,{'timeout', ReqTimeout}
Expand Down
1 change: 1 addition & 0 deletions applications/crossbar/src/crossbar.hrl
Expand Up @@ -124,6 +124,7 @@
,client_ip = <<"127.0.0.1">> :: ne_binary()
,load_merge_bypass :: api_object()
,profile_id :: api_binary()
,api_version = <<"v1">> :: binary()
}).

-define(CROSSBAR_HRL, 'true').
Expand Down
6 changes: 5 additions & 1 deletion applications/crossbar/src/crossbar_bindings.erl
Expand Up @@ -96,7 +96,11 @@ get_bindings(Routing) ->
[Vsn, Action | _] = binary:split(Routing, <<".">>, ['global']),

ets:select(?MODULE, [{ {'_', '_', '_', '$1'}
,[{'=:=', '$1', <<Vsn/binary, ".", Action/binary>>}]
,[{'orelse',
{'=:=', '$1', <<Vsn/binary, ".", Action/binary>>}
,{'=:=', '$1', <<"*.", Action/binary>>}
}
]
,['$_']
}]).
%% ets:match(?MODULE, ['$_']).
Expand Down

0 comments on commit 71aec65

Please sign in to comment.