Skip to content

Commit

Permalink
Make post request to update arena's gateway_url at arena_server creat…
Browse files Browse the repository at this point in the history
…ion and update
  • Loading branch information
Nico-Sanchez committed Sep 11, 2024
1 parent 07b1b7a commit 894586f
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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} ->
Expand Down Expand Up @@ -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

0 comments on commit 894586f

Please sign in to comment.