@@ -79,38 +79,38 @@ defmodule HttpRouter do
79
79
"""
80
80
81
81
import HttpRouter.Util
82
- alias Application , as: A
83
82
84
83
@ typep ast :: tuple
85
84
@ http_methods [ :get , :post , :put , :patch , :delete , :any ]
86
85
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 ]
95
93
]
96
94
97
95
## Macros
98
96
99
97
@ doc false
100
- defmacro __using__ ( _ ) do
98
+ defmacro __using__ ( opts ) do
99
+ opts = @ default_options |> Keyword . merge ( opts )
101
100
quote do
102
101
import HttpRouter
103
102
import Plug.Builder , only: [ plug: 1 , plug: 2 ]
104
103
@ before_compile HttpRouter
105
104
@ behaviour Plug
106
105
Module . register_attribute ( __MODULE__ , :plugs , accumulate: true )
107
106
Module . register_attribute ( __MODULE__ , :version , accumulate: false )
107
+ Module . put_attribute ( __MODULE__ , :options , unquote ( opts ) )
108
108
109
109
# Plugs we want early in the stack
110
- parsers_opts = [ parsers: unquote ( @ options [ :parsers ] ) ]
110
+ parsers_opts = [ parsers: unquote ( opts [ :parsers ] ) ]
111
111
if :json in parsers_opts [ :parsers ] do
112
112
parsers_opts = parsers_opts
113
- |> Keyword . put ( :json_decoder , unquote ( @ options [ :json_decoder ] ) )
113
+ |> Keyword . put ( :json_decoder , unquote ( opts [ :json_decoder ] ) )
114
114
end
115
115
116
116
plug Plug.Parsers , parsers_opts
@@ -119,20 +119,21 @@ defmodule HttpRouter do
119
119
120
120
@ doc false
121
121
defmacro __before_compile__ ( env ) do
122
+ options = Module . get_attribute ( env . module , :options )
122
123
# Plugs we want predefined but aren't necessary to be before
123
124
# user-defined plugs
124
125
defaults = [ { :match , [ ] , true } ,
125
126
{ :dispatch , [ ] , true } ]
126
127
127
- if @ options [ :allow_copy_req_content_type ] == true do
128
+ if options [ :allow_copy_req_content_type ] == true do
128
129
defaults = [ { :copy_req_content_type , [ ] , true } | defaults ]
129
130
end
130
131
131
- if @ options [ :allow_method_override ] == true do
132
+ if options [ :allow_method_override ] == true do
132
133
defaults = [ { Plug.MethodOverride , [ ] , true } | defaults ]
133
134
end
134
135
135
- if @ options [ :allow_head ] == true do
136
+ if options [ :allow_head ] == true do
136
137
defaults = [ { Plug.Head , [ ] , true } | defaults ]
137
138
end
138
139
@@ -151,9 +152,9 @@ defmodule HttpRouter do
151
152
152
153
defoverridable [ init: 1 , call: 2 ]
153
154
154
- if unquote ( @ options [ :allow_copy_req_content_type ] ) == true do
155
+ if unquote ( options [ :allow_copy_req_content_type ] ) == true do
155
156
def copy_req_content_type ( conn , _opts ) do
156
- default = unquote ( @ options [ :default_content_type ] )
157
+ default = unquote ( options [ :default_content_type ] )
157
158
content_type = case Plug.Conn . get_req_header conn , "content-type" do
158
159
[ content_type ] -> content_type
159
160
_ -> default
0 commit comments