just need to add creating services, deploying them, and showing logs
This commit is contained in:
parent
a3a4af7965
commit
023ad47ae4
6 changed files with 99 additions and 29 deletions
|
|
@ -9,7 +9,7 @@ defmodule EzcontainerRailway.Application do
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
children = [
|
children = [
|
||||||
EzcontainerRailwayWeb.Telemetry,
|
EzcontainerRailwayWeb.Telemetry,
|
||||||
# EzcontainerRailway.Repo,
|
EzcontainerRailway.Repo,
|
||||||
{DNSCluster, query: Application.get_env(:ezcontainer_railway, :dns_cluster_query) || :ignore},
|
{DNSCluster, query: Application.get_env(:ezcontainer_railway, :dns_cluster_query) || :ignore},
|
||||||
{Phoenix.PubSub, name: EzcontainerRailway.PubSub},
|
{Phoenix.PubSub, name: EzcontainerRailway.PubSub},
|
||||||
# Start the Finch HTTP client for sending emails
|
# Start the Finch HTTP client for sending emails
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,19 @@ defmodule EzcontainerRailway.Projects do
|
||||||
|
|
||||||
alias EzcontainerRailway.Repo
|
alias EzcontainerRailway.Repo
|
||||||
alias EzcontainerRailway.Project
|
alias EzcontainerRailway.Project
|
||||||
|
alias EzcontainerRailway.Railway
|
||||||
|
|
||||||
|
def get_project_by_token(token) do
|
||||||
|
{:ok, user_id} = Railway.get_user_id(token)
|
||||||
|
|
||||||
|
get_project_by_user_id(user_id)
|
||||||
|
|
||||||
|
Repo.one(
|
||||||
|
from p in Project,
|
||||||
|
where: p.user_id == ^user_id,
|
||||||
|
limit: 1
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def get_project_by_user_id(user_id) do
|
def get_project_by_user_id(user_id) do
|
||||||
Repo.one(
|
Repo.one(
|
||||||
|
|
@ -11,4 +24,13 @@ defmodule EzcontainerRailway.Projects do
|
||||||
limit: 1
|
limit: 1
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_project(token) do
|
||||||
|
{:ok, project_id} = Railway.create_project(token)
|
||||||
|
{:ok, user_id} = Railway.get_user_id(token)
|
||||||
|
|
||||||
|
%Project{}
|
||||||
|
|> Project.changeset(%{user_id: user_id, project_id: project_id})
|
||||||
|
|> Repo.insert!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
defmodule EzcontainerRailway.Railway do
|
defmodule EzcontainerRailway.Railway do
|
||||||
alias EzcontainerRailway.GraphqlClient
|
alias EzcontainerRailway.GraphqlClient
|
||||||
def get_projects(token) do
|
def get_projects(token) do
|
||||||
GraphqlClient.query(
|
resp = GraphqlClient.query(
|
||||||
"""
|
"""
|
||||||
query {
|
query {
|
||||||
projects {
|
projects {
|
||||||
|
|
@ -15,7 +15,12 @@ defmodule EzcontainerRailway.Railway do
|
||||||
}
|
}
|
||||||
""",
|
""",
|
||||||
token: token
|
token: token
|
||||||
)
|
) |> format_response
|
||||||
|
|
||||||
|
case resp do
|
||||||
|
{:error, _} -> resp
|
||||||
|
{:ok, data} -> {:ok, data["projects"]["edges"]}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_service(container, token) when is_map(container) do
|
def create_service(container, token) when is_map(container) do
|
||||||
|
|
@ -26,41 +31,51 @@ defmodule EzcontainerRailway.Railway do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_project(name, token) do
|
def create_project(token) do
|
||||||
GraphqlClient.query(
|
resp = GraphqlClient.query(
|
||||||
"""
|
"""
|
||||||
mutation {
|
mutation {
|
||||||
projectCreate(input: {name: $name}) {
|
projectCreate(input: {}) {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
""",
|
""",
|
||||||
token: token,
|
token: token
|
||||||
variables: %{ "name" => name }
|
|
||||||
)
|
)
|
||||||
|> format_response
|
|> format_response
|
||||||
end
|
|
||||||
|
|
||||||
def is_token_valid?(token) do
|
case resp do
|
||||||
resp = get_projects(token).body
|
{:error, _} -> resp
|
||||||
|
{:ok, data} -> {:ok, data["projectCreate"]["id"]}
|
||||||
if has_errors?(resp) do
|
|
||||||
false
|
|
||||||
else
|
|
||||||
true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_errors?(resp) when is_map(resp) do
|
def get_user_id(token) do
|
||||||
|
{:ok, resp} = GraphqlClient.query(
|
||||||
|
"""
|
||||||
|
query {
|
||||||
|
me {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""",
|
||||||
|
token: token
|
||||||
|
)
|
||||||
|
|> format_response
|
||||||
|
|
||||||
|
{:ok, resp["me"]["id"]}
|
||||||
|
end
|
||||||
|
|
||||||
|
def has_errors?(resp) do
|
||||||
has_errors_in_array?(resp["errors"])
|
has_errors_in_array?(resp["errors"])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_response(resp) when is_map(resp) do
|
defp format_response(resp) when is_map(resp) do
|
||||||
if has_errors?(resp) do
|
if has_errors?(resp.body) do
|
||||||
{:error, resp["error"]}
|
{:error, resp.body["errors"]}
|
||||||
else
|
else
|
||||||
{:ok, resp["data"]}
|
{:ok, resp.body["data"]}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,7 @@ defmodule EzcontainerRailwayWeb.SessionController do
|
||||||
|
|
||||||
def token(conn, %{"railway_token" => token}) do
|
def token(conn, %{"railway_token" => token}) do
|
||||||
case get_projects(token) do
|
case get_projects(token) do
|
||||||
resp when is_map(resp) ->
|
resp when is_list(resp) ->
|
||||||
IO.inspect(resp)
|
|
||||||
conn
|
conn
|
||||||
|> put_session(:railway_token, token)
|
|> put_session(:railway_token, token)
|
||||||
|> assign(:railway_token, token)
|
|> assign(:railway_token, token)
|
||||||
|
|
@ -18,12 +17,11 @@ defmodule EzcontainerRailwayWeb.SessionController do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_projects(token) do
|
defp get_projects(token) do
|
||||||
resp = EzcontainerRailway.Railway.get_projects(token).body
|
case EzcontainerRailway.Railway.get_projects(token) do
|
||||||
|
{:error, _} ->
|
||||||
if Railway.has_errors?(resp) do
|
|
||||||
nil
|
nil
|
||||||
else
|
{:ok, data} ->
|
||||||
resp["data"]
|
data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
26
lib/ezcontainer_railway_web/plugs/railway_check.ex
Normal file
26
lib/ezcontainer_railway_web/plugs/railway_check.ex
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
defmodule EzcontainerRailwayWeb.RailwayCheck do
|
||||||
|
alias EzcontainerRailway.Projects
|
||||||
|
alias EzcontainerRailway.Railway
|
||||||
|
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
|
||||||
|
|> Controller.put_flash(:error, "You need a railway token")
|
||||||
|
|> Controller.redirect(to: "/")
|
||||||
|
token ->
|
||||||
|
p =
|
||||||
|
case Projects.get_project_by_token(token) do
|
||||||
|
nil -> Projects.create_project(token)
|
||||||
|
p -> p
|
||||||
|
end
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> put_session(:project_id, p)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -4,7 +4,7 @@ defmodule EzcontainerRailwayWeb.Router do
|
||||||
pipeline :browser do
|
pipeline :browser do
|
||||||
plug :accepts, ["html"]
|
plug :accepts, ["html"]
|
||||||
plug :fetch_session
|
plug :fetch_session
|
||||||
#plug EzcontainerRailwayWeb.AnonSession
|
plug EzcontainerRailwayWeb.AnonSession
|
||||||
plug :fetch_live_flash
|
plug :fetch_live_flash
|
||||||
plug :put_root_layout, html: {EzcontainerRailwayWeb.Layouts, :root}
|
plug :put_root_layout, html: {EzcontainerRailwayWeb.Layouts, :root}
|
||||||
plug :protect_from_forgery
|
plug :protect_from_forgery
|
||||||
|
|
@ -15,11 +15,20 @@ defmodule EzcontainerRailwayWeb.Router do
|
||||||
plug :accepts, ["json"]
|
plug :accepts, ["json"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
pipeline :ensure_token do
|
||||||
|
plug EzcontainerRailwayWeb.RailwayCheck
|
||||||
|
end
|
||||||
|
|
||||||
scope "/", EzcontainerRailwayWeb do
|
scope "/", EzcontainerRailwayWeb do
|
||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
get "/", PageController, :home
|
get "/", PageController, :home
|
||||||
post "/token", SessionController, :token
|
post "/token", SessionController, :token
|
||||||
|
end
|
||||||
|
|
||||||
|
scope "/", EzcontainerRailwayWeb do
|
||||||
|
pipe_through :browser
|
||||||
|
pipe_through :ensure_token
|
||||||
|
|
||||||
live "/containers", ContainerCreateLive
|
live "/containers", ContainerCreateLive
|
||||||
resources("/containers", ContainerController, only: [:create, :show])
|
resources("/containers", ContainerController, only: [:create, :show])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue