Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/ethui/stacks/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ defmodule Ethui.Stacks.Server do
GenServer.call(__MODULE__, :list)
end

@doc "Check if a stack with the given slug is running"
@spec is_slug_running?(slug) :: boolean
def is_slug_running?(slug) do
GenServer.call(__MODULE__, {:is_slug_running, slug})
end

def start(%Stack{} = stack) do
GenServer.cast(__MODULE__, {:start, stack})
end
Expand Down Expand Up @@ -92,6 +98,11 @@ defmodule Ethui.Stacks.Server do
{:reply, Map.keys(instances), state}
end

@impl GenServer
def handle_call({:is_slug_running, slug}, _from, %{instances: instances} = state) do
{:reply, Map.has_key?(instances, slug), state}
end

@impl GenServer
def handle_cast({:start, stack}, state) do
case start_stack(stack, state) do
Expand Down
22 changes: 22 additions & 0 deletions lib/ethui_web/controllers/api/stack_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ defmodule EthuiWeb.Api.StackController do
})
end

def show(conn, %{"slug" => slug}) do
user = conn.assigns[:current_user]

with %Stack{} = stack <- Repo.get_by(Stack, slug: slug),
:ok <- authorize_user_access(user, stack) do
json(conn, %{
status: "success",
data: %{
slug: stack.slug,
urls: Stacks.get_urls(stack),
status: if(Server.is_slug_running?(slug), do: "running", else: "stopped")
}
})
else
nil ->
conn |> put_status(404) |> json(%{status: "error", error: "not found"})

{:error, :unauthorized} ->
conn |> put_status(403) |> json(%{status: "error", error: "unauthorized"})
end
end

def create(conn, params) do
user = conn.assigns[:current_user]

Expand Down