src/nine_cowboy_util.erl

-module(nine_cowboy_util).

-export([get_path/1, not_found/1, redirect/2]).

get_path(Req) ->
    Path = cowboy_req:path(Req),
    split_path(Path).

split_path(Path) ->
    string:split(Path, <<"/">>, all).

not_found(#{req := Req} = Context) ->
    Resp = cowboy_req:reply(
        404,
        #{<<"Content-Type">> => <<"text/plain">>},
        <<"Not Found">>,
        Req
    ),
    Context#{resp => Resp}.

redirect(#{req := Req} = Context, RedirectPath) ->
    Resp = cowboy_req:reply(
        302,
        #{<<"Location">> => RedirectPath},
        <<>>,
        Req
    ),
    Context#{resp => Resp}.