CAPI-292: add seadlines support to wapi (#11)

This commit is contained in:
Anton Belyaev 2018-09-04 19:48:19 +03:00 committed by GitHub
parent b8ab555769
commit 8d040b6868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 8 deletions

View File

@ -6,6 +6,8 @@
%% Behaviour definition
-type tag() :: wallet | payres | privdoc.
-type operation_id() ::
swag_server_payres:operation_id() |
swag_server_wallet:operation_id() |
@ -49,13 +51,14 @@
-define(request_result, wapi_req_result).
-spec handle_request(operation_id(), req_data(), swagger_context(), module(), opts()) ->
-spec handle_request(tag(), operation_id(), req_data(), swagger_context(), opts()) ->
request_result().
handle_request(OperationID, Req, SwagContext = #{auth_context := AuthContext}, Handler, Opts) ->
handle_request(Tag, OperationID, Req, SwagContext = #{auth_context := AuthContext}, Opts) ->
_ = lager:info("Processing request ~p", [OperationID]),
try
WoodyContext = create_woody_context(Req, AuthContext, Opts),
WoodyContext = create_woody_context(Tag, Req, AuthContext, Opts),
Context = create_handler_context(SwagContext, WoodyContext),
Handler = get_handler(Tag),
case wapi_auth:authorize_operation(OperationID, Req, Context) of
{ok, AuthDetails} ->
ok = lager:info("Operation ~p authorized via ~p", [OperationID, AuthDetails]),
@ -76,17 +79,33 @@ handle_request(OperationID, Req, SwagContext = #{auth_context := AuthContext}, H
throw_result(Res) ->
erlang:throw({?request_result, Res}).
-spec create_woody_context(req_data(), wapi_auth:context(), opts()) ->
get_handler(wallet) -> wapi_wallet_handler;
get_handler(payres) -> wapi_payres_handler;
get_handler(privdoc) -> wapi_privdoc_handler.
-spec create_woody_context(tag(), req_data(), wapi_auth:context(), opts()) ->
woody_context:ctx().
create_woody_context(#{'X-Request-ID' := RequestID}, AuthContext, Opts) ->
create_woody_context(Tag, #{'X-Request-ID' := RequestID}, AuthContext, Opts) ->
RpcID = #{trace_id := TraceID} = woody_context:new_rpc_id(genlib:to_binary(RequestID)),
ok = scoper:add_meta(#{request_id => RequestID, trace_id => TraceID}),
_ = lager:debug("Created TraceID for the request"),
Ctx = woody_user_identity:put(collect_user_identity(AuthContext, Opts), woody_context:new(RpcID)),
Ctx = woody_user_identity:put(
collect_user_identity(AuthContext, Opts),
woody_context:new(RpcID, undefined, get_deadline(Tag))
),
%% TODO remove this fistful specific step, when separating the wapi service.
ok = ff_woody_ctx:set(Ctx),
Ctx.
get_deadline(Tag) ->
ApiDeadlines = genlib_app:env(wapi, api_deadlines, #{}),
case maps:get(Tag, ApiDeadlines, undefined) of
Timeout when is_integer(Timeout) andalso Timeout >= 0 ->
woody_deadline:from_timeout(Timeout);
undefined ->
undefined
end.
-define(APP, wapi).
collect_user_identity(AuthContext, _Opts) ->

View File

@ -31,7 +31,7 @@ authorize_api_key(OperationID, ApiKey, Opts) ->
-spec handle_request(swag_server_wallet:operation_id(), req_data(), request_context(), handler_opts()) ->
request_result().
handle_request(OperationID, Req, SwagContext, Opts) ->
wapi_handler:handle_request(OperationID, Req, SwagContext, ?MODULE, Opts).
wapi_handler:handle_request(wallet, OperationID, Req, SwagContext, Opts).
%% Providers

View File

@ -108,10 +108,12 @@
cds_storage => "http://cds:8022/v1/storage",
identdoc_storage => "http://cds:8022/v1/identity_document_storage"
}},
{api_deadlines, #{
wallet => 5000 % millisec
}},
{health_checkers, [
{erl_health, service , [<<"wapi">>]}
]}
]},
{ff_server, [