[99s-extend] Rewriting URLs
Paul Dickson
pdtwonotes at gmail.com
Tue Jan 27 14:44:14 CET 2015
Ok, here is all the code in bz_libmap.erl
execute(Req, Env) ->
Path = cowboy_req:path(Req),
Parts = filename:split(Path),
case cowboy_req:method(Req) of
<<"GET">> ->
% Check for "/music/" requests
rewrite( Parts, Req, Env );
_Other ->
{ok, Req, Env}
end.
rewrite( [<<"/">>, <<"music">>, LibName | UrlParts], Req, Env ) ->
% We want URLs of the form "/music/LIBNAME/everythingelse"
% Get library definition. If we do not know that name, then they
% are asking for something that does not exist.
case bz_db:get_library( LibName ) of
[] ->
io:format("No such library '~s'~n", [LibName] ),
{stop, cowboy_req:reply(404, Req)};
L when is_record(L,library) ->
% Replace the library name with the library base.
% This will be the head of an absolute file path.
LibBase = L#library.base,
NewPath = filename:join([LibBase | UrlParts]),
NewInfo = filename:split(NewPath),
Req2 = cowboy_req:set( [
{path_info,[NewPath]},
{path, [NewPath]}], Req),
{ok, Req2, Env}
end;
rewrite( _AnythingElse, Req, Env ) ->
{ok, Req, Env}.
More information about the Extend
mailing list