mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-04-20 03:02:27 +08:00
Make it possible to configure custom plug for all requests (#576)
* Make it possible to configure custom plug for all requests * Apply review comments * Use fetch_env!
This commit is contained in:
parent
25d90eabf1
commit
ac60aba2fb
3 changed files with 29 additions and 0 deletions
|
@ -26,6 +26,13 @@ config :livebook, :authentication_mode, :token
|
||||||
# configured arguments.
|
# configured arguments.
|
||||||
config :livebook, :default_runtime, {Livebook.Runtime.ElixirStandalone, []}
|
config :livebook, :default_runtime, {Livebook.Runtime.ElixirStandalone, []}
|
||||||
|
|
||||||
|
# A list of custom plugs in the following format:
|
||||||
|
#
|
||||||
|
# [{plug_module :: module(), opts :: keyword()}]
|
||||||
|
#
|
||||||
|
# The plugs are called directly before the Livebook router.
|
||||||
|
config :livebook, :plugs, []
|
||||||
|
|
||||||
# Import environment specific config. This must remain at the bottom
|
# Import environment specific config. This must remain at the bottom
|
||||||
# of this file so it overrides the configuration defined above.
|
# of this file so it overrides the configuration defined above.
|
||||||
import_config "#{Mix.env()}.exs"
|
import_config "#{Mix.env()}.exs"
|
||||||
|
|
|
@ -71,6 +71,10 @@ defmodule LivebookWeb.Endpoint do
|
||||||
plug Plug.MethodOverride
|
plug Plug.MethodOverride
|
||||||
plug Plug.Head
|
plug Plug.Head
|
||||||
plug Plug.Session, @session_options
|
plug Plug.Session, @session_options
|
||||||
|
|
||||||
|
# Run custom plugs from the app configuration
|
||||||
|
plug LivebookWeb.ConfiguredPlug
|
||||||
|
|
||||||
plug LivebookWeb.Router
|
plug LivebookWeb.Router
|
||||||
|
|
||||||
def access_url() do
|
def access_url() do
|
||||||
|
|
18
lib/livebook_web/plugs/configured_plug.ex
Normal file
18
lib/livebook_web/plugs/configured_plug.ex
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
defmodule LivebookWeb.ConfiguredPlug do
|
||||||
|
@moduledoc false
|
||||||
|
|
||||||
|
# Runs plugs configured for the :livebook application
|
||||||
|
|
||||||
|
@behaviour Plug
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def init(opts), do: opts
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def call(conn, _opts) do
|
||||||
|
case Application.fetch_env!(:livebook, :plugs) do
|
||||||
|
[] -> conn
|
||||||
|
plugs -> Plug.run(conn, plugs)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue