@@ -79,38 +79,38 @@ defmodule HttpRouter do
7979 """
8080
8181 import HttpRouter.Util
82- alias Application , as: A
8382
8483 @ typep ast :: tuple
8584 @ http_methods [ :get , :post , :put , :patch , :delete , :any ]
8685
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 ] )
86+ @ default_options [
87+ allow_copy_req_content_type: true ,
88+ allow_head: true ,
89+ allow_method_override: true ,
90+ default_content_type: "text/html; charset=utf-8" ,
91+ json_decoder: Poison ,
92+ parsers: [ :json , :urlencoded , :multipart ]
9593 ]
9694
9795 ## Macros
9896
9997 @ doc false
100- defmacro __using__ ( _ ) do
98+ defmacro __using__ ( opts ) do
99+ opts = @ default_options |> Keyword . merge ( opts )
101100 quote do
102101 import HttpRouter
103102 import Plug.Builder , only: [ plug: 1 , plug: 2 ]
104103 @ before_compile HttpRouter
105104 @ behaviour Plug
106105 Module . register_attribute ( __MODULE__ , :plugs , accumulate: true )
107106 Module . register_attribute ( __MODULE__ , :version , accumulate: false )
107+ Module . put_attribute ( __MODULE__ , :options , unquote ( opts ) )
108108
109109 # Plugs we want early in the stack
110- parsers_opts = [ parsers: unquote ( @ options [ :parsers ] ) ]
110+ parsers_opts = [ parsers: unquote ( opts [ :parsers ] ) ]
111111 if :json in parsers_opts [ :parsers ] do
112112 parsers_opts = parsers_opts
113- |> Keyword . put ( :json_decoder , unquote ( @ options [ :json_decoder ] ) )
113+ |> Keyword . put ( :json_decoder , unquote ( opts [ :json_decoder ] ) )
114114 end
115115
116116 plug Plug.Parsers , parsers_opts
@@ -119,20 +119,21 @@ defmodule HttpRouter do
119119
120120 @ doc false
121121 defmacro __before_compile__ ( env ) do
122+ options = Module . get_attribute ( env . module , :options )
122123 # Plugs we want predefined but aren't necessary to be before
123124 # user-defined plugs
124125 defaults = [ { :match , [ ] , true } ,
125126 { :dispatch , [ ] , true } ]
126127
127- if @ options [ :allow_copy_req_content_type ] == true do
128+ if options [ :allow_copy_req_content_type ] == true do
128129 defaults = [ { :copy_req_content_type , [ ] , true } | defaults ]
129130 end
130131
131- if @ options [ :allow_method_override ] == true do
132+ if options [ :allow_method_override ] == true do
132133 defaults = [ { Plug.MethodOverride , [ ] , true } | defaults ]
133134 end
134135
135- if @ options [ :allow_head ] == true do
136+ if options [ :allow_head ] == true do
136137 defaults = [ { Plug.Head , [ ] , true } | defaults ]
137138 end
138139
@@ -151,9 +152,9 @@ defmodule HttpRouter do
151152
152153 defoverridable [ init: 1 , call: 2 ]
153154
154- if unquote ( @ options [ :allow_copy_req_content_type ] ) == true do
155+ if unquote ( options [ :allow_copy_req_content_type ] ) == true do
155156 def copy_req_content_type ( conn , _opts ) do
156- default = unquote ( @ options [ :default_content_type ] )
157+ default = unquote ( options [ :default_content_type ] )
157158 content_type = case Plug.Conn . get_req_header conn , "content-type" do
158159 [ content_type ] -> content_type
159160 _ -> default
0 commit comments