Okay that's about everything
This commit is contained in:
parent
023ad47ae4
commit
d094ff036f
19 changed files with 236 additions and 79 deletions
|
|
@ -1,4 +1,5 @@
|
|||
defmodule EzcontainerRailway.Railway do
|
||||
alias EzcontainerRailway.Projects
|
||||
alias EzcontainerRailway.GraphqlClient
|
||||
def get_projects(token) do
|
||||
resp = GraphqlClient.query(
|
||||
|
|
@ -23,12 +24,89 @@ defmodule EzcontainerRailway.Railway do
|
|||
end
|
||||
end
|
||||
|
||||
def create_service(container, token) when is_map(container) do
|
||||
def get_services(token) do
|
||||
project_id = Projects.get_project_by_token(token).project_id
|
||||
resp = GraphqlClient.query(
|
||||
"""
|
||||
query getServices($projectId: String!) {
|
||||
project(id: $projectId) {
|
||||
services {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""",
|
||||
token: token,
|
||||
variables: %{
|
||||
projectId: project_id
|
||||
}
|
||||
) |> format_response
|
||||
|
||||
case resp do
|
||||
{:error, _} -> resp
|
||||
{:ok, data} ->
|
||||
final =
|
||||
data["project"]["services"]["edges"]
|
||||
|> Enum.map(&(&1["node"]))
|
||||
|
||||
{:ok, final}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def create_service(source, type, token) do
|
||||
resp = case type do
|
||||
"repo" -> create_service_from_repo(source, token)
|
||||
"image" -> create_service_from_image(source, token)
|
||||
end
|
||||
|
||||
case resp do
|
||||
{:error, _} -> resp
|
||||
{:ok, data} -> {:ok, data["serviceCreate"]}
|
||||
end
|
||||
end
|
||||
|
||||
defp create_service_from_image(source, token) do
|
||||
project_id = Projects.get_project_by_token(token).project_id
|
||||
GraphqlClient.query(
|
||||
"""
|
||||
mutation createService($projectId: String!, $image: String!, $name: String!){
|
||||
serviceCreate(input: {projectId: $projectId, name: $name source: { image: $image }}) {
|
||||
id
|
||||
}
|
||||
}
|
||||
""",
|
||||
variables: container
|
||||
)
|
||||
token: token,
|
||||
variables: %{
|
||||
projectId: project_id,
|
||||
image: source,
|
||||
name: source
|
||||
}
|
||||
) |> format_response
|
||||
end
|
||||
|
||||
defp create_service_from_repo(source, token) do
|
||||
project_id = Projects.get_project_by_token(token).project_id
|
||||
GraphqlClient.query(
|
||||
"""
|
||||
mutation createService($projectId: String!, $repo: String!, $name: String!){
|
||||
serviceCreate(input: {projectId: $projectId, name: $name source: { repo: $repo }}) {
|
||||
id
|
||||
}
|
||||
}
|
||||
""",
|
||||
token: token,
|
||||
variables: %{
|
||||
projectId: project_id,
|
||||
repo: source,
|
||||
name: source
|
||||
}
|
||||
) |> format_response
|
||||
end
|
||||
|
||||
def create_project(token) do
|
||||
|
|
@ -51,6 +129,21 @@ defmodule EzcontainerRailway.Railway do
|
|||
end
|
||||
end
|
||||
|
||||
def delete_container(token, service_id) do
|
||||
GraphqlClient.query(
|
||||
"""
|
||||
mutation deleteContainer(id: String!) {
|
||||
serviceDelete(id: $id)
|
||||
}
|
||||
""",
|
||||
token: token,
|
||||
variables: %{
|
||||
id: service_id
|
||||
}
|
||||
)
|
||||
|> format_response
|
||||
end
|
||||
|
||||
def get_user_id(token) do
|
||||
{:ok, resp} = GraphqlClient.query(
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,32 +1,27 @@
|
|||
<header class="px-4 sm:px-6 lg:px-8">
|
||||
<div class="flex items-center justify-between border-b border-zinc-100 py-3 text-sm">
|
||||
<div class="flex items-center gap-4">
|
||||
<a href="/">
|
||||
<img src={~p"/images/logo.svg"} width="36" />
|
||||
</a>
|
||||
<p class="bg-brand/5 text-brand rounded-full px-2 font-medium leading-6">
|
||||
v<%= Application.spec(:phoenix, :vsn) %>
|
||||
</p>
|
||||
<%= live_flash(@flash, :error) %>
|
||||
<%= live_flash(@flash, :info) %>
|
||||
<body class="bg-base-300 font-sans leading-normal tracking-normal w-screen h-screen">
|
||||
<!-- Navbar -->
|
||||
<div class="bg-primary p-3">
|
||||
<div class="container mx-auto flex items-center">
|
||||
<!-- App Logo/Name -->
|
||||
<div class="flex items-center text-lg font-extrabold text-base-content">
|
||||
<a href="#" class="hover:underline">
|
||||
Your App Name
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<!-- Navigation links -->
|
||||
<div class="flex ml-auto space-x-4">
|
||||
<a class="btn btn-ghost btn-sm rounded-btn" href="/containers/create">Create Containers</a>
|
||||
<a class="btn btn-ghost btn-sm rounded-btn" href="/containers">Show Containers</a>
|
||||
<a class="btn btn-ghost btn-sm rounded-btn" href="#">Logout</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-4 font-semibold leading-6 text-zinc-900">
|
||||
<a href="https://twitter.com/elixirphoenix" class="hover:text-zinc-700">
|
||||
@elixirphoenix
|
||||
</a>
|
||||
<a href="https://github.com/phoenixframework/phoenix" class="hover:text-zinc-700">
|
||||
GitHub
|
||||
</a>
|
||||
<a
|
||||
href="https://hexdocs.pm/phoenix/overview.html"
|
||||
class="rounded-lg bg-zinc-100 px-2 py-1 hover:bg-zinc-200/80"
|
||||
>
|
||||
Get Started <span aria-hidden="true">→</span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<main class="px-4 py-20 sm:px-6 lg:px-8">
|
||||
<div class="mx-auto max-w-2xl">
|
||||
<.flash_group flash={@flash} />
|
||||
</div>
|
||||
|
||||
<!-- Content Body -->
|
||||
<div class="container mx-auto mt-10">
|
||||
<%= @inner_content %>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -11,7 +11,5 @@
|
|||
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<%= @inner_content %>
|
||||
</body>
|
||||
<%= @inner_content %>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
benbot@darch.125027:1698169376
|
||||
|
|
@ -1,13 +1,14 @@
|
|||
defmodule EzcontainerRailwayWeb.ContainerController do
|
||||
alias EzcontainerRailway.Railway
|
||||
alias Phoenix.Component
|
||||
use EzcontainerRailwayWeb, :controller
|
||||
|
||||
def index(conn, params) do
|
||||
form = Component.to_form(%{"container_name" => "", "container_tag" => ""})
|
||||
{:ok, services} = Railway.get_services(conn |> get_session(:railway_token))
|
||||
|
||||
conn
|
||||
|> assign(:form, form)
|
||||
|> render(:make_container, layout: false)
|
||||
|> assign(:services, services)
|
||||
|> render(:containers)
|
||||
end
|
||||
|
||||
def create(conn, params) do
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
<.flash_group flash={@flash} />
|
||||
<div class="grid grid-cols-3 m-10">
|
||||
<%= for service <- @services do %>
|
||||
<div class="card bg-base-100 shadow-xl">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title"><%= service["name"] %></h2>
|
||||
<div class="card-actions justify-start">
|
||||
<button class="btn btn-xs btn-secondary">Delete</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
<.flash_group flash={@flash} />
|
||||
<dialog open class="modal">
|
||||
<dialog open class="modal bg-base-300">
|
||||
<article class="modal-box">
|
||||
<.form
|
||||
for={@form}
|
||||
|
|
@ -8,13 +8,13 @@
|
|||
action="/token"
|
||||
>
|
||||
<label class="label">
|
||||
<span class="label-text">What is your name?</span>
|
||||
<span class="label-text-alt">Top Right label</span>
|
||||
<span class="label-text">Enter your railway token</span>
|
||||
<a href="https://railway.app/account/tokens" class="label-text-alt underline">Get it here</a>
|
||||
</label>
|
||||
|
||||
<input class="input input-bordered" name="railway_token" />
|
||||
<div class="text-center mt-6">
|
||||
<button class="btn">Test</button>
|
||||
<button class="btn">Begin</button>
|
||||
</div>
|
||||
</.form>
|
||||
</article>
|
||||
|
|
|
|||
|
|
@ -1,39 +1,65 @@
|
|||
defmodule EzcontainerRailwayWeb.ContainerCreateLive do
|
||||
alias EzcontainerRailway.Railway
|
||||
alias Phoenix.Component
|
||||
use Phoenix.LiveView
|
||||
use EzcontainerRailwayWeb, :live_view
|
||||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<%= live_flash(@flash, :error) %>
|
||||
<%= live_flash(@flash, :info) %>
|
||||
<dialog open class="modal">
|
||||
<article class="modal-box">
|
||||
<.form
|
||||
for={@form}
|
||||
method="post"
|
||||
action="/container"
|
||||
>
|
||||
<label class="label">
|
||||
<span class="label-text">Container Name</span>
|
||||
<span class="label-text-alt mr-20">Tag</span>
|
||||
</label>
|
||||
<div class="flex space-around w-full">
|
||||
<article>
|
||||
<%= if @target == "container" do %>
|
||||
<.form for={@form} phx-submit="create_container">
|
||||
<label class="label">
|
||||
<span class="label-text">Container Name</span>
|
||||
<span class="label-text-alt mr-20">Tag</span>
|
||||
</label>
|
||||
|
||||
<div class="grid grid-rows-1 grid-cols-4 gap-4">
|
||||
<input class="input input-bordered inline col-span-3" name="container_name" />
|
||||
<input class="input input-bordered inline" name="container_tag" />
|
||||
</div>
|
||||
<div class="text-center mt-6">
|
||||
<button class="btn">Test</button>
|
||||
</div>
|
||||
</.form>
|
||||
</article>
|
||||
</dialog>
|
||||
<div class="grid grid-rows-1 grid-cols-4 gap-4">
|
||||
<input class="input input-bordered inline col-span-3" name="name" />
|
||||
<input class="input input-bordered inline" name="tag" />
|
||||
</div>
|
||||
<div class="text-center mt-6">
|
||||
<button phx-submit class="btn btn-primary">CREATE</button>
|
||||
</div>
|
||||
<input type="hidden" name="target" value="container" />
|
||||
</.form>
|
||||
<% end %>
|
||||
<%= if @target == "repo" do %>
|
||||
<.form for={@form} phx-submit="create_container">
|
||||
<label class="label">
|
||||
<span class="label-text">Github Repo</span>
|
||||
<span class="label-text-alt mr-20">Branch</span>
|
||||
</label>
|
||||
|
||||
<div class="grid grid-rows-1 grid-cols-4 gap-4">
|
||||
<input class="input input-bordered inline col-span-3" name="name" />
|
||||
<input class="input input-bordered inline" name="tag" />
|
||||
</div>
|
||||
<div class="text-center mt-6">
|
||||
<button phx-submit class="btn btn-primary">CREATE</button>
|
||||
</div>
|
||||
<input type="hidden" name="target" value="repo" />
|
||||
</.form>
|
||||
<% end %>
|
||||
</article>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
form = Component.to_form(%{"container_name" => "", "contianer_tag" => ""})
|
||||
def mount(_params, session, socket) do
|
||||
form = Component.to_form(%{"name" => "", "tag" => ""})
|
||||
|
||||
{:ok, socket |> assign(:form, form)}
|
||||
{:ok, socket |> assign(form: form, target: "container", session: session)}
|
||||
end
|
||||
|
||||
def handle_event("create_container", data, socket) do
|
||||
{:ok, resp} = Railway.create_service(
|
||||
data["name"],
|
||||
"image",
|
||||
socket.assigns.session["railway_token"]
|
||||
)
|
||||
|
||||
{:noreply, socket |> push_redirect('/containers')}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
19
lib/ezcontainer_railway_web/plugs/no_railway_check.ex
Normal file
19
lib/ezcontainer_railway_web/plugs/no_railway_check.ex
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
defmodule EzcontainerRailwayWeb.NoRailwayCheck do
|
||||
alias EzcontainerRailway.Projects
|
||||
alias Phoenix.Controller
|
||||
import Plug.Conn
|
||||
|
||||
def init(default), do: default
|
||||
|
||||
def call(conn, _opts) do
|
||||
case get_session(conn, :railway_token) do
|
||||
nil ->
|
||||
conn
|
||||
_ ->
|
||||
conn
|
||||
|> Controller.put_flash(:info, "You have a railway token")
|
||||
|> Controller.redirect(to: "/containers/create")
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -20,7 +20,7 @@ defmodule EzcontainerRailwayWeb.RailwayCheck do
|
|||
end
|
||||
|
||||
conn
|
||||
|> put_session(:project_id, p)
|
||||
|> put_session(:project_id, p.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,8 +19,13 @@ defmodule EzcontainerRailwayWeb.Router do
|
|||
plug EzcontainerRailwayWeb.RailwayCheck
|
||||
end
|
||||
|
||||
pipeline :ensure_no_token do
|
||||
plug EzcontainerRailwayWeb.NoRailwayCheck
|
||||
end
|
||||
|
||||
scope "/", EzcontainerRailwayWeb do
|
||||
pipe_through :browser
|
||||
pipe_through :ensure_no_token
|
||||
|
||||
get "/", PageController, :home
|
||||
post "/token", SessionController, :token
|
||||
|
|
@ -30,8 +35,8 @@ defmodule EzcontainerRailwayWeb.Router do
|
|||
pipe_through :browser
|
||||
pipe_through :ensure_token
|
||||
|
||||
live "/containers", ContainerCreateLive
|
||||
resources("/containers", ContainerController, only: [:create, :show])
|
||||
live "/containers/create", ContainerCreateLive
|
||||
resources("/containers", ContainerController, only: [:index, :create, :show])
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue