some clean up

This commit is contained in:
Alexander Strizhakov 2020-01-17 16:28:44 +03:00
parent ce027fd0ef
commit 7676ed8239
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
8 changed files with 34 additions and 122 deletions

View File

@ -16,7 +16,6 @@ defmodule Mix.Tasks.Pleroma.Config do
@moduledoc File.read!("docs/administration/CLI_tasks/config.md") @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
def run(["migrate_to_db"]) do def run(["migrate_to_db"]) do
# we want to save original logger level
start_pleroma() start_pleroma()
migrate_to_db() migrate_to_db()
end end

View File

@ -28,7 +28,7 @@ defmodule Mix.Tasks.Pleroma.Docs do
defp do_run(implementation) do defp do_run(implementation) do
start_pleroma() start_pleroma()
with {descriptions, _paths} <- Mix.Config.eval!("config/description.exs"), with descriptions <- Pleroma.Config.Loader.load("config/description.exs"),
{:ok, file_path} <- {:ok, file_path} <-
Pleroma.Docs.Generator.process( Pleroma.Docs.Generator.process(
implementation, implementation,

View File

@ -192,7 +192,6 @@ defmodule Pleroma.ConfigDB do
} }
end end
defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
# TODO: will become useless after removing hackney # TODO: will become useless after removing hackney
defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]} defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
@ -229,14 +228,13 @@ defmodule Pleroma.ConfigDB do
{:proxy_url, {do_transform_string(type), parse_host(host), port}} {:proxy_url, {do_transform_string(type), parse_host(host), port}}
end end
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
{dispatch_settings, []} = do_eval(entity)
{:dispatch, [dispatch_settings]}
end
# TODO: will become useless after removing hackney # TODO: will become useless after removing hackney
defp do_transform(%{"tuple" => [":partial_chain", entity]}) do defp do_transform(%{"tuple" => [":partial_chain", entity]}) do
{partial_chain, []} = do_eval(entity) {partial_chain, []} =
entity
|> String.replace(~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
|> Code.eval_string()
{:partial_chain, partial_chain} {:partial_chain, partial_chain}
end end
@ -322,9 +320,4 @@ defmodule Pleroma.ConfigDB do
Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack)\./, string) or Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack)\./, string) or
string in ["Oban", "Ueberauth", "ExSyslogger"] string in ["Oban", "Ueberauth", "ExSyslogger"]
end end
defp do_eval(entity) do
cleaned_string = String.replace(entity, ~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
Code.eval_string(cleaned_string)
end
end end

View File

@ -3,20 +3,29 @@ defmodule Pleroma.Config.Loader do
if Code.ensure_loaded?(Config.Reader) do if Code.ensure_loaded?(Config.Reader) do
@spec load() :: map() @spec load() :: map()
def load do def load do
config = Config.Reader.read!("config/config.exs") config = load("config/config.exs")
env_config = Config.Reader.read!("config/#{Mix.env()}.exs") env_config = load("config/#{Mix.env()}.exs")
Config.Reader.merge(config, env_config) Config.Reader.merge(config, env_config)
end end
@spec load(Path.t()) :: keyword()
def load(path), do: Config.Reader.read!(path)
else else
# support for Elixir less than 1.9 # support for Elixir less than 1.9
@spec load() :: map() @spec load() :: map()
def load do def load do
{config, _paths} = Mix.Config.eval!("config/config.exs") {config, _paths} = load("config/config.exs")
{env_config, _paths} = Mix.Config.eval!("config/#{Mix.env()}.exs") {env_config, _paths} = load("config/#{Mix.env()}.exs")
Mix.Config.merge(config, env_config) Mix.Config.merge(config, env_config)
end end
@spec load(Path.t()) :: keyword()
def load(path) do
{config, _paths} = Mix.Config.eval!(path)
config
end
end end
end end

View File

@ -48,7 +48,20 @@ defmodule Pleroma.Config.TransferTask do
end end
:ok = Application.put_env(group, key, merged_value) :ok = Application.put_env(group, key, merged_value)
group
if group != :logger do
group
else
# change logger configuration in runtime, without restart
if Keyword.keyword?(merged_value) and
key not in [:compile_time_application, :backends, :compile_time_purge_matching] do
Logger.configure_backend(key, merged_value)
else
Logger.configure([{key, merged_value}])
end
nil
end
end end
rescue rescue
e -> e ->

View File

@ -15,7 +15,7 @@ defmodule Pleroma.Docs.JSON do
end end
def compile do def compile do
with {config, _paths} <- Mix.Config.eval!("config/description.exs") do with config <- Pleroma.Config.Loader.load("config/description.exs") do
config[:pleroma][:config_description] config[:pleroma][:config_description]
|> Pleroma.Docs.Generator.convert_to_strings() |> Pleroma.Docs.Generator.convert_to_strings()
|> Jason.encode!() |> Jason.encode!()

View File

@ -330,45 +330,6 @@ defmodule Pleroma.ConfigDBTest do
{"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer} {"v1", :v2, Pleroma.Bookmark, 150, false, Phoenix.Socket.V1.JSONSerializer}
end end
test "tuple with dispatch key" do
binary = ConfigDB.transform(%{"tuple" => [":dispatch", ["{:_,
[
{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}"]]})
assert binary ==
:erlang.term_to_binary(
{:dispatch,
[
{:_,
[
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{"/websocket", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}
]}
)
assert ConfigDB.from_binary(binary) ==
{:dispatch,
[
{:_,
[
{"/api/v1/streaming", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{"/websocket", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: "/websocket"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}
]}
end
test "map with string key" do test "map with string key" do
binary = ConfigDB.transform(%{"key" => "value"}) binary = ConfigDB.transform(%{"key" => "value"})
assert binary == :erlang.term_to_binary(%{"key" => "value"}) assert binary == :erlang.term_to_binary(%{"key" => "value"})

View File

@ -2632,69 +2632,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
} }
end end
test "dispatch setting", %{conn: conn} do
conn =
post(conn, "/api/pleroma/admin/config", %{
configs: [
%{
"group" => ":pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
"tuple" => [
":http",
[
%{"tuple" => [":ip", %{"tuple" => [127, 0, 0, 1]}]},
%{"tuple" => [":dispatch", ["{:_,
[
{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []},
{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket,
{Phoenix.Transports.WebSocket,
{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}},
{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}
]}"]]}
]
]
}
]
}
]
})
dispatch_string =
"{:_, [{\"/api/v1/streaming\", Pleroma.Web.MastodonAPI.WebsocketHandler, []}, " <>
"{\"/websocket\", Phoenix.Endpoint.CowboyWebSocket, {Phoenix.Transports.WebSocket, " <>
"{Pleroma.Web.Endpoint, Pleroma.Web.UserSocket, [path: \"/websocket\"]}}}, " <>
"{:_, Phoenix.Endpoint.Cowboy2Handler, {Pleroma.Web.Endpoint, []}}]}"
assert json_response(conn, 200) == %{
"configs" => [
%{
"group" => ":pleroma",
"key" => "Pleroma.Web.Endpoint.NotReal",
"value" => [
%{
"tuple" => [
":http",
[
%{"tuple" => [":ip", %{"tuple" => [127, 0, 0, 1]}]},
%{
"tuple" => [
":dispatch",
[
dispatch_string
]
]
}
]
]
}
]
}
]
}
end
test "queues key as atom", %{conn: conn} do test "queues key as atom", %{conn: conn} do
conn = conn =
post(conn, "/api/pleroma/admin/config", %{ post(conn, "/api/pleroma/admin/config", %{