Skip to content

Commit

Permalink
fix: handle not_found and static_routes
Browse files Browse the repository at this point in the history
  • Loading branch information
javiergarea committed Oct 9, 2024
1 parent 179e2df commit 96afea1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
33 changes: 28 additions & 5 deletions src/erf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ reload_conf(Name, NewConf) ->

case build_router(SpecPath, SpecParser, Callback, StaticRoutes, SwaggerUI) of
{ok, RouterMod, Router, API} ->
RoutePatterns = route_patterns(API),
RoutePatterns = route_patterns(API, StaticRoutes, SwaggerUI),
erf_conf:set(Name, Conf#{
route_patterns => RoutePatterns,
router_mod => RouterMod,
Expand Down Expand Up @@ -243,7 +243,7 @@ init([Name, RawConf]) ->

case build_router(SpecPath, SpecParser, Callback, StaticRoutes, SwaggerUI) of
{ok, RouterMod, Router, API} ->
RoutePatterns = route_patterns(API),
RoutePatterns = route_patterns(API, StaticRoutes, SwaggerUI),
ErfConf = RawErfConf#{
route_patterns => RoutePatterns,
router_mod => RouterMod,
Expand Down Expand Up @@ -395,12 +395,35 @@ match_route_(RawPath, [{Route, RouteRegEx} | Routes]) ->
{ok, Route}
end.

-spec route_patterns(API) -> RoutePatterns when
-spec route_patterns(API, StaticRoutes, SwaggerUI) -> RoutePatterns when
API :: api(),
StaticRoutes :: [static_route()],
SwaggerUI :: boolean(),
RoutePatterns :: route_patterns().
route_patterns(API) ->
route_patterns(API, StaticRoutes, SwaggerUI) ->
Acc =
lists:map(
fun
({Path, {file, _ResourcePath}}) ->
{Path, <<"^", Path/binary, "$">>};
({Path, {dir, _ResourcePath}}) ->
{Path, <<"^", Path/binary>>}
end,
StaticRoutes
),
Acc1 =
case SwaggerUI of
true ->
[
{<<"/swagger">>, <<"^/swagger$">>},
{<<"/swagger/spec.json">>, <<"^/swagger/spec.json$">>}
| Acc
];
_false ->
Acc
end,
RawRoutes = [maps:get(path, Endpoint) || Endpoint <- maps:get(endpoints, API)],
route_patterns(RawRoutes, []).
route_patterns(RawRoutes, Acc1).

-spec route_patterns(RawRoutes, Acc) -> RoutePatterns when
RawRoutes :: [binary()],
Expand Down
8 changes: 7 additions & 1 deletion src/erf_http_server/erf_http_server_elli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,13 @@ preprocess(Name, Req) ->
ElliBody
end,
JoinPath = erlang:list_to_binary([<<"/">> | lists:join(<<"/">>, Path)]),
{ok, Route} = erf:match_route(Name, JoinPath),
Route =
case erf:match_route(Name, JoinPath) of
{ok, R} ->
R;
{error, not_found} ->
JoinPath
end,
#{
scheme => Scheme,
host => Host,
Expand Down
10 changes: 10 additions & 0 deletions test/erf_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ foo(_Conf) ->
)
),

?assertMatch(
{ok, {{"HTTP/1.1", 404, "Not Found"}, _Result3Headers, <<>>}},
httpc:request(
get,
{"http://localhost:8789/1/not_found", []},
[],
[{body_format, binary}]
)
),

ok = erf:stop(erf_server),

meck:unload(erf_callback),
Expand Down

0 comments on commit 96afea1

Please sign in to comment.