forked from travelping/hello
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_router.erl
More file actions
32 lines (30 loc) · 1.28 KB
/
hello_router.erl
File metadata and controls
32 lines (30 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-module(hello_router).
-export([route/3]).
-include("hello.hrl").
-include("hello_log.hrl").
route(_Context = #context{session_id = Id}, Request = #request{method = Method}, ExURI) ->
Namespace = get_namespace(Method),
case hello_binding:lookup(ExURI, Namespace) of
{error, not_found} ->
?LOG_INFO("Unable to find service for key ~s. Request is dismissed.", [Namespace],
[ {hello_request, hello_log:format(Request)},
{hello_error_response, {error, not_found, Namespace}}],
?LOGID37),
{error, method_not_found};
{ok, _, Name} ->
?LOG_DEBUG("Resolved namespace ~p to hello handler ~p.", [Namespace, Name],
[{hello_request, hello_log:format(Request)}], ?LOGID38),
{ok, Name, Id}
end.
get_namespace(Method) ->
Method1 = hello_lib:to_binary(Method),
SplittedMethod = binary:split(Method1, <<".">>, [global]),
SplittedNs = lists:sublist(SplittedMethod, 1, length(SplittedMethod)-1),
case SplittedNs of
[] ->
<<>>;
[NsHead] ->
NsHead;
[NsHead | NsTail] ->
lists:foldl( fun(NewNs, Ns) -> <<Ns/binary, ".", NewNs/binary>> end, NsHead, NsTail )
end.