Skip to content

Commit 71355fa

Browse files
committedFeb 5, 2015
ensure options are not calculated at runtime
1 parent fe13267 commit 71355fa

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed
 

‎lib/http_router.ex

+17-18
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ defmodule HttpRouter do
8484
@typep ast :: tuple
8585
@http_methods [ :get, :post, :put, :patch, :delete, :any ]
8686

87+
@app A.get_env(:http_router, :otp_app, :http_router)
88+
@options [
89+
allow_copy_req_content_type: A.get_env(@app, :allow_copy_req_content_type, true),
90+
allow_head: A.get_env(@app, :allow_head, true),
91+
allow_method_override: A.get_env(@app, :allow_method_override, true),
92+
default_content_type: A.get_env(@app, :default_content_type, "text/html; charset=utf-8"),
93+
json_decoder: A.get_env(@app, :json_decoder, Poison),
94+
parsers: A.get_env(@app, :parsers, [:json, :urlencoded, :multipart])
95+
]
96+
8797
## Macros
8898

8999
@doc false
@@ -97,10 +107,10 @@ defmodule HttpRouter do
97107
Module.register_attribute(__MODULE__, :version, accumulate: false)
98108

99109
# Plugs we want early in the stack
100-
parsers_opts = [ parsers: options[:parsers] ]
110+
parsers_opts = [ parsers: unquote(@options[:parsers]) ]
101111
if :json in parsers_opts[:parsers] do
102112
parsers_opts = parsers_opts
103-
|> Keyword.put(:json_decoder, options[:json_decoder])
113+
|> Keyword.put(:json_decoder, unquote(@options[:json_decoder]))
104114
end
105115

106116
plug Plug.Parsers, parsers_opts
@@ -114,15 +124,15 @@ defmodule HttpRouter do
114124
defaults = [ { :match, [], true },
115125
{ :dispatch, [], true } ]
116126

117-
if options[:allow_copy_req_content_type] == true do
127+
if @options[:allow_copy_req_content_type] == true do
118128
defaults = [ { :copy_req_content_type, [], true } | defaults ]
119129
end
120130

121-
if options[:allow_method_override] == true do
131+
if @options[:allow_method_override] == true do
122132
defaults = [ { Plug.MethodOverride, [], true } | defaults ]
123133
end
124134

125-
if options[:allow_head] == true do
135+
if @options[:allow_head] == true do
126136
defaults = [ { Plug.Head, [], true } | defaults ]
127137
end
128138

@@ -141,9 +151,9 @@ defmodule HttpRouter do
141151

142152
defoverridable [init: 1, call: 2]
143153

144-
if options[:allow_copy_req_content_type] == true do
154+
if unquote(@options[:allow_copy_req_content_type]) == true do
145155
def copy_req_content_type(conn, _opts) do
146-
default = options[:default_content_type]
156+
default = unquote(@options[:default_content_type])
147157
content_type = case Plug.Conn.get_req_header conn, "content-type" do
148158
[content_type] -> content_type
149159
_ -> default
@@ -289,17 +299,6 @@ defmodule HttpRouter do
289299

290300
## Private API
291301

292-
@app A.get_env(:http_router, :otp_app, :http_router)
293-
@doc false
294-
def options, do: [
295-
allow_copy_req_content_type: A.get_env(@app, :allow_copy_req_content_type, true),
296-
allow_head: A.get_env(@app, :allow_head, true),
297-
allow_method_override: A.get_env(@app, :allow_method_override, true),
298-
default_content_type: A.get_env(@app, :default_content_type, "text/html; charset=utf-8"),
299-
json_decoder: A.get_env(@app, :json_decoder, Poison),
300-
parsers: A.get_env(@app, :parsers, [:json, :urlencoded, :multipart])
301-
]
302-
303302
defp ignore_args(str) do
304303
str
305304
|> String.to_char_list

0 commit comments

Comments
 (0)