Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Cowboy middleware to set some CORS headers for every request, and to handle OPTIONS requests without needing to implement them in every handler.
execute(Req, Env) ->
{ok, ReqWithCorsHeaders} = set_cors_headers(Req),
{Method, ReqMethod} = cowboy_req:method(ReqWithCorsHeaders),
case Method of
<<"OPTIONS">> ->
{ok, ReqFinal} = cowboy_req:reply(200, ReqMethod),
{halt, ReqFinal};
_ ->
%% continue as normal
{ok, ReqMethod, Env}
%% ===================================================================
%% Helpers
%% ===================================================================
set_headers(Headers, Req) ->
ReqWithHeaders = lists:foldl(fun({Header, Value}, ReqIn) ->
ReqWithHeader = cowboy_req:set_resp_header(Header, Value, ReqIn),
end, Req, Headers),
{ok, ReqWithHeaders}.
set_cors_headers(Req) ->
Headers = [{<<"access-control-allow-origin">>, <<"*">>},
{<<"access-control-allow-methods">>, <<"POST, GET, OPTIONS">>},
{<<"access-control-allow-headers">>, <<"Origin, X-Requested-With, Content-Type, Accept">>},
{<<"access-control-max-age">>, <<"1000">>}],
{ok, Req2} = set_headers(Headers, Req),
{ok, Req2}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment