mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-03-01 01:13:16 +08:00
Handle module plugs in proxy
This commit is contained in:
parent
28336da83c
commit
220a4a2cdb
1 changed files with 27 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
||||||
defmodule Livebook.Proxy.Handler do
|
defmodule Livebook.Proxy.Handler do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
# Handles request `conn` with the configured function.
|
# Handles request `conn` with the configured plug.
|
||||||
#
|
#
|
||||||
# The handler forwards all actual communication to the parent
|
# The handler forwards all actual communication to the parent
|
||||||
# `Livebook.Proxy.Server` via `Livebook.Proxy.Adapter`.
|
# `Livebook.Proxy.Server` via `Livebook.Proxy.Adapter`.
|
||||||
|
@ -15,8 +15,11 @@ defmodule Livebook.Proxy.Handler do
|
||||||
@doc """
|
@doc """
|
||||||
Returns a child spec to setup the handler supervision tree.
|
Returns a child spec to setup the handler supervision tree.
|
||||||
|
|
||||||
Expects the `:listen` option to be provided, and be a function with
|
Expects the `:listen` option to be provided, and be one of:
|
||||||
the signature `Plug.Conn.t() -> Plug.Conn.t()`.
|
|
||||||
|
* a function plug: `Plug.Conn.t() -> Plug.Conn.t()`
|
||||||
|
|
||||||
|
* a module plug: `module` atom or `{module, options}` tuple.
|
||||||
"""
|
"""
|
||||||
@spec child_spec(keyword()) :: Supervisor.child_spec()
|
@spec child_spec(keyword()) :: Supervisor.child_spec()
|
||||||
def child_spec(opts) do
|
def child_spec(opts) do
|
||||||
|
@ -39,8 +42,27 @@ defmodule Livebook.Proxy.Handler do
|
||||||
conn =
|
conn =
|
||||||
struct!(Plug.Conn, %{conn_attrs | adapter: {Livebook.Proxy.Adapter, {parent_pid, ref}}})
|
struct!(Plug.Conn, %{conn_attrs | adapter: {Livebook.Proxy.Adapter, {parent_pid, ref}}})
|
||||||
|
|
||||||
listen = :persistent_term.get({__MODULE__, :listen})
|
case :persistent_term.get({__MODULE__, :listen}) do
|
||||||
listen.(conn)
|
fun when is_function(fun, 1) ->
|
||||||
|
fun.(conn)
|
||||||
|
|
||||||
|
mod when is_atom(mod) ->
|
||||||
|
mod.call(conn, mod.init([]))
|
||||||
|
|
||||||
|
{mod, opts} when is_atom(mod) ->
|
||||||
|
mod.call(conn, mod.init(opts))
|
||||||
|
|
||||||
|
other ->
|
||||||
|
raise """
|
||||||
|
expected plug to be one of:
|
||||||
|
|
||||||
|
* fun(conn)
|
||||||
|
* module
|
||||||
|
* {module, options}
|
||||||
|
|
||||||
|
got: #{inspect(other)}
|
||||||
|
"""
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
Loading…
Reference in a new issue