/
nova_cors_plugin.erl
69 lines (63 loc) · 2.21 KB
/
nova_cors_plugin.erl
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-module(nova_cors_plugin).
-behaviour(nova_plugin).
-export([
pre_request/2,
post_request/2,
plugin_info/0
]).
%%--------------------------------------------------------------------
%% @doc
%% Pre-request callback
%% @end
%%--------------------------------------------------------------------
-spec pre_request(Req :: cowboy_req:req(), Options :: map()) ->
{ok, Req0 :: cowboy_req:req()}.
pre_request(Req, #{allow_origins := Origins}) ->
ReqWithOptions = add_cors_headers(Req, Origins),
continue(ReqWithOptions).
%%--------------------------------------------------------------------
%% @doc
%% Post-request callback
%% @end
%%--------------------------------------------------------------------
-spec post_request(Req :: cowboy_req:req(), Options :: map()) ->
{ok, Req0 :: cowboy_req:req()}.
post_request(Req, _) ->
{ok, Req}.
%%--------------------------------------------------------------------
%% @doc
%% nova_plugin callback. Returns information about the plugin.
%% @end
%%--------------------------------------------------------------------
-spec plugin_info() -> {Title :: binary(),
Version :: binary(),
Author :: binary(),
Description :: binary(),
Options :: [{Key :: atom(), OptionDescription :: binary()}]}.
plugin_info() ->
{
<<"nova_cors_plugin">>,
<<"0.2.0">>,
<<"Nova team <info@novaframework.org">>,
<<"Add CORS headers to request">>,
[
{
allow_origins,
<<"Specifies which origins to insert into Access-Control-Allow-Origin">>
}
]}.
%%%%%%%%%%%%%%%%%%%%%%
%% Private functions
%%%%%%%%%%%%%%%%%%%%%%
continue(#{method := <<"OPTIONS">>} = Req) ->
Reply = cowboy_req:reply(200, Req),
{stop, Reply};
continue(Req) ->
{ok, Req}.
add_cors_headers(Req, Origins) ->
OriginsReq = cowboy_req:set_resp_header(
<<"Access-Control-Allow-Origin">>, Origins, Req),
HeadersReq = cowboy_req:set_resp_header(
<<"Access-Control-Allow-Headers">>, <<"*">>, OriginsReq),
cowboy_req:set_resp_header(
<<"Access-Control-Allow-Methods">>, <<"*">>, HeadersReq).