From c08079595583c8413d8d77738349902dda966427 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 6 Oct 2022 08:59:50 +0200 Subject: [PATCH] Reconfigure plug at runtime with env Closes #1165 Signed-off-by: Thomas Citharel --- lib/web/endpoint.ex | 11 +++-------- lib/web/plug_configs.ex | 14 ++++++++++++++ mix.exs | 1 + mix.lock | 1 + 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 lib/web/plug_configs.ex diff --git a/lib/web/endpoint.ex b/lib/web/endpoint.ex index e3ca1ace..e080a564 100644 --- a/lib/web/endpoint.ex +++ b/lib/web/endpoint.ex @@ -64,14 +64,9 @@ defmodule Mobilizon.Web.Endpoint do plug(Plug.RequestId) plug(Plug.Logger) - upload_limit = - Keyword.get(Application.compile_env(:mobilizon, :instance, []), :upload_limit, 10_485_760) - - plug( - Plug.Parsers, - parsers: [:urlencoded, {:multipart, length: upload_limit}, :json, Absinthe.Plug.Parser], - pass: ["*/*"], - json_decoder: Jason + plug(Replug, + plug: {Plug.Parsers, pass: ["*/*"], json_decoder: Jason}, + opts: {Mobilizon.Web.PlugConfigs, :parsers} ) plug(Plug.MethodOverride) diff --git a/lib/web/plug_configs.ex b/lib/web/plug_configs.ex new file mode 100644 index 00000000..f9bb4be0 --- /dev/null +++ b/lib/web/plug_configs.ex @@ -0,0 +1,14 @@ +defmodule Mobilizon.Web.PlugConfigs do + @moduledoc """ + Runtime configuration for plugs + """ + + def parsers do + upload_limit = + Keyword.get(Application.get_env(:mobilizon, :instance, []), :upload_limit, 10_485_760) + + [ + parsers: [:urlencoded, {:multipart, length: upload_limit}, :json, Absinthe.Plug.Parser] + ] + end +end diff --git a/mix.exs b/mix.exs index 7d84432c..8e1d489b 100644 --- a/mix.exs +++ b/mix.exs @@ -206,6 +206,7 @@ defmodule Mobilizon.Mixfile do {:codepagex, "~> 0.1.6"}, {:vite_phx, "~> 0.2"}, {:unplug, "~> 1.0.0"}, + {:replug, "~> 0.1.0"}, # Dev and test dependencies {:phoenix_live_reload, "~> 1.2", only: [:dev, :e2e]}, {:ex_machina, "~> 2.3", only: [:dev, :test]}, diff --git a/mix.lock b/mix.lock index 4a52a92a..6a05727c 100644 --- a/mix.lock +++ b/mix.lock @@ -115,6 +115,7 @@ "progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"}, "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, "remote_ip": {:hex, :remote_ip, "1.0.0", "3d7fb45204a5704443f480cee9515e464997f52c35e0a60b6ece1f81484067ae", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "9e9fcad4e50c43b5234bb6a9629ed6ab223f3ed07147bd35470e4ee5c8caf907"}, + "replug": {:hex, :replug, "0.1.0", "61d35f8c873c0078a23c49579a48f36e45789414b1ec0daee3fd5f4e34221f23", [:mix], [{:plug, "~> 1.8", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "f71f7a57e944e854fe4946060c6964098e53958074c69fb844b96e0bd58cfa60"}, "sentry": {:hex, :sentry, "8.0.6", "c8de1bf0523bc120ec37d596c55260901029ecb0994e7075b0973328779ceef7", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, "~> 2.3", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "051a2d0472162f3137787c7c9d6e6e4ef239de9329c8c45b1f1bf1e9379e1883"}, "shortuuid": {:hex, :shortuuid, "2.1.2", "14dbafdb2f6c7213fdfcc05c7572384b5051a7b1621170018ad4c05504bd96c1", [:mix], [], "hexpm", "d9b0c4f37500ea5199b6275ece872e213e9f45a015caf4aa777cec84f63ad353"}, "sitemapper": {:hex, :sitemapper, "0.6.0", "0475fac74d1544b0af74b6f89d29cdb39b5a697ed7da54707e8f8276c2d1c665", [:mix], [{:ex_aws_s3, "~> 2.0", [hex: :ex_aws_s3, repo: "hexpm", optional: true]}, {:xml_builder, "~> 2.1", [hex: :xml_builder, repo: "hexpm", optional: false]}], "hexpm", "7392a8af265932ef58af4e1bb950ada826eff8d56277e1187a130f1bda6883e5"},