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:
Jonatan Kłosko 2021-10-05 00:44:27 +02:00 committed by GitHub
parent 25d90eabf1
commit ac60aba2fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -26,6 +26,13 @@ config :livebook, :authentication_mode, :token
# configured arguments.
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
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"

View file

@ -71,6 +71,10 @@ defmodule LivebookWeb.Endpoint do
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
# Run custom plugs from the app configuration
plug LivebookWeb.ConfiguredPlug
plug LivebookWeb.Router
def access_url() do

View 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