From 894586f1b833598d76ebb8f5d6bd1720aefcf61c Mon Sep 17 00:00:00 2001 From: Nicolas Sanchez Date: Wed, 4 Sep 2024 16:18:05 -0300 Subject: [PATCH] Make post request to update arena's gateway_url at arena_server creation and update --- .../controllers/arena_server_controller.ex | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/apps/configurator/lib/configurator_web/controllers/arena_server_controller.ex b/apps/configurator/lib/configurator_web/controllers/arena_server_controller.ex index 602722421..3cc4063e4 100644 --- a/apps/configurator/lib/configurator_web/controllers/arena_server_controller.ex +++ b/apps/configurator/lib/configurator_web/controllers/arena_server_controller.ex @@ -17,6 +17,8 @@ defmodule ConfiguratorWeb.ArenaServerController do def create(conn, %{"arena_server" => arena_server_params}) do case Configuration.create_arena_server(arena_server_params) do {:ok, arena_server} -> + update_arena_gateway_url(arena_server.gateway_url, arena_server_params["gateway_url"], arena_server.url) + conn |> put_flash(:info, "Arena server created successfully.") |> redirect(to: ~p"/arena_servers/#{arena_server}") @@ -39,6 +41,7 @@ defmodule ConfiguratorWeb.ArenaServerController do def update(conn, %{"id" => id, "arena_server" => arena_server_params}) do arena_server = Configuration.get_arena_server!(id) + update_arena_gateway_url(arena_server.gateway_url, arena_server_params["gateway_url"], arena_server.url) case Configuration.update_arena_server(arena_server, arena_server_params) do {:ok, arena_server} -> @@ -66,4 +69,22 @@ defmodule ConfiguratorWeb.ArenaServerController do |> redirect(to: ~p"/arena_servers") end end + + defp update_arena_gateway_url(former_url, former_url, _arena_url), do: nil + + defp update_arena_gateway_url(_former_url, new_url, arena_url) do + payload = Jason.encode!(%{gateway_url: new_url}) + + arena_url = + if String.contains?(arena_url, "localhost") do + "http://" <> arena_url + else + "https://" <> arena_url + end + + spawn(fn -> + Finch.build(:post, arena_url <> "/api/update_central", [{"content-type", "application/json"}], payload) + |> Finch.request(Gateway.Finch) + end) + end end