-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplug.ex
293 lines (245 loc) · 8.17 KB
/
plug.ex
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
defmodule Twirp.Plug do
@moduledoc """
Provides a plug that takes service and handler module. If the request is
directed at the "twirp" endpoint then the plug will intercept the conn and
process it. Otherwise it allows the conn to pass through. This is a deviation
from the twirp specification but it allows users to include twirp services
into their existing plug stacks.
You can use the plug like so:
```elixir
plug Twirp.Plug,
service: MyService,
handler: MyHandler,
```
"""
@content_type "content-type"
alias Twirp.Encoder
alias Twirp.Error
alias Twirp.Telemetry
import Plug.Conn
import Norm
def env_s do
schema(%{
content_type: spec(is_binary()),
method_name: spec(is_atom()),
handler_fn: spec(is_atom()),
input: spec(is_map()),
input_type: spec(is_atom()),
output_type: spec(is_atom()),
http_response_headers: map_of(spec(is_binary()), spec(is_binary())),
})
end
def hook_result_s do
alt(
env: selection(env_s()),
error: schema(%Twirp.Error{})
)
end
def init(args) do
handler =
args
|> Keyword.fetch!(:handler)
Code.ensure_compiled(handler)
service_def =
args
|> Keyword.fetch!(:service)
|> apply(:definition, [])
|> Norm.conform!(Twirp.Service.s())
hooks = %{
before: Keyword.get(args, :before, []),
on_success: Keyword.get(args, :on_success, []),
on_error: Keyword.get(args, :on_error, []),
on_exception: Keyword.get(args, :on_exception, []),
}
rpc_defs =
for rpc <- service_def.rpcs,
do: {"#{rpc.method}", rpc},
into: %{}
service_def =
service_def
|> Map.put(:rpcs, rpc_defs)
|> Map.put(:full_name, Twirp.Service.full_name(service_def))
{service_def, handler, hooks}
end
def call(%{path_info: ["twirp", full_name, method]}=conn, {%{full_name: full_name}=service, handler, hooks}) do
call(%{conn | path_info: [full_name, method]}, {service, handler, hooks})
end
def call(%{path_info: [full_name, method]}=conn, {%{full_name: full_name}=service, handler, hooks}) do
env = %{}
metadata = %{}
start = Telemetry.start(:call, metadata)
try do
with {:ok, env} <- validate_req(conn, method, service),
{:ok, env, conn} <- get_input(env, conn),
{:ok, env} <- call_before_hooks(env, conn, hooks),
{:ok, output} <- call_handler(handler, env)
do
# We're safe to just get the output because call_handler has handled
# the error case for us
resp = Encoder.encode(output, env.output_type, env.content_type)
env = Map.put(env, :output, resp)
call_on_success_hooks(env, hooks)
metadata =
metadata
|> Map.put(:content_type, env.content_type)
|> Map.put(:method, env.method_name)
Telemetry.stop(:call, start, metadata)
conn
|> put_resp_content_type(env.content_type, nil)
|> send_resp(200, resp)
|> halt()
else
{:error, env, error} ->
metadata =
metadata
|> Map.put(:content_type, env.content_type)
|> Map.put(:method, env.method_name)
|> Map.put(:error, error)
Telemetry.stop(:call, start, metadata)
call_on_error_hooks(hooks, env, error)
send_error(conn, error)
end
rescue
exception ->
try do
call_on_exception_hooks(hooks, env, exception)
Telemetry.exception(:call, start, :error, exception, __STACKTRACE__, metadata)
error = Error.internal(Exception.message(exception))
call_on_error_hooks(hooks, env, error)
send_error(conn, error)
rescue
hook_e ->
Telemetry.exception(:call, start, :error, hook_e, __STACKTRACE__, metadata)
error = Error.internal(Exception.message(hook_e))
call_on_error_hooks(hooks, env, error)
send_error(conn, error)
end
end
end
def call(conn, _opts) do
conn
end
def validate_req(conn, method, %{rpcs: rpcs}) do
content_type = content_type(conn)
env = %{
content_type: content_type,
http_response_headers: %{},
method_name: method,
}
cond do
conn.method != "POST" ->
{:error, env, bad_route("HTTP request must be POST", conn)}
!Encoder.valid_type?(content_type) ->
{:error, env, bad_route("Unexpected Content-Type: #{content_type || "nil"}", conn)}
rpcs[method] == nil ->
{:error, env, bad_route("Invalid rpc method: #{method}", conn)}
true ->
rpc = rpcs[method]
env = Map.merge(env, %{
content_type: content_type,
http_response_headers: %{},
method_name: rpc.method,
input_type: rpc.input,
output_type: rpc.output,
handler_fn: rpc.handler_fn,
})
{:ok, conform!(env, env_s())}
end
end
defp get_input(env, conn) do
with {:ok, body, conn} <- get_body(conn, env) do
case Encoder.decode(body, env.input_type, env.content_type) do
{:ok, decoded} ->
{:ok, Map.put(env, :input, decoded), conn}
_error ->
msg = "Invalid request body for rpc method: #{env.method_name}"
error = bad_route(msg, conn)
{:error, env, error}
end
end
end
defp get_body(conn, env) do
# If we're in a phoenix endpoint or an established plug router than the
# user is probably already using a plug parser and the body will be
# empty. We need to check to see if we have body params which is an
# indication that our json has already been parsed. Limiting this to
# only json payloads since the user most likely doesn't have a protobuf
# parser already set up and I want to limit this potentially surprising
# behaviour.
if Encoder.json?(env.content_type) and body_params?(conn) do
{:ok, conn.body_params, conn}
else
case apply(Plug.Conn, :read_body, [conn]) do
{:ok, body, conn} ->
{:ok, body, conn}
_ ->
{:error, env, Error.internal("req_body has already been read or is too large to read")}
end
end
end
defp body_params?(conn) do
case conn.body_params do
%Plug.Conn.Unfetched{} -> false
_ -> true
end
end
defp call_handler(handler, %{output_type: output_type}=env) do
env = conform!(env, selection(env_s()))
if function_exported?(handler, env.handler_fn, 2) do
case apply(handler, env.handler_fn, [env, env.input]) do
%Error{}=error ->
{:error, env, error}
%{__struct__: s}=resp when s == output_type ->
{:ok, resp}
other ->
msg = "Handler method #{env.handler_fn} expected to return one of #{env.output_type} or Twirp.Error but returned #{inspect other}"
{:error, env, Error.internal(msg)}
end
else
{:error, env, Error.unimplemented("Handler function #{env.handler_fn} is not implemented")}
end
end
def call_before_hooks(env, conn, hooks) do
result = Enum.reduce_while(hooks.before, env, fn f, updated_env ->
result = f.(conn, updated_env)
case conform!(result, hook_result_s()) do
{:error, err} -> {:halt, {:error, updated_env, err}}
{:env, next_env} -> {:cont, next_env}
end
end)
case result do
{:error, env, err} ->
{:error, env, err}
env ->
{:ok, env}
end
end
def call_on_success_hooks(env, hooks) do
for hook <- hooks.on_success do
hook.(env)
end
end
def call_on_error_hooks(hooks, env, error) do
for hook <- hooks.on_error do
hook.(env, error)
end
end
def call_on_exception_hooks(hooks, env, exception) do
for hook <- hooks.on_exception do
hook.(env, exception)
end
end
defp content_type(conn) do
Enum.at(get_req_header(conn, @content_type), 0)
end
defp send_error(conn, error) do
body = Jason.encode!(error)
conn
|> put_resp_content_type("application/json", nil)
|> send_resp(Error.code_to_status(error.code), body)
|> halt()
end
defp bad_route(msg, conn) do
Error.bad_route(msg, %{"twirp_invalid_route" => "#{conn.method} #{conn.request_path}"})
end
end