Skip to content

Commit 1b3c66c

Browse files
authored
Merge pull request #226 from CaptainFact/ops/elixir-release
Move to mix release
2 parents f74dff6 + a8b6d87 commit 1b3c66c

36 files changed

+471
-218
lines changed

.github/workflows/e2e.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
run: mix ecto.create && mix ecto.migrate
4747
- name: Start API
4848
run: iex -S mix run &
49+
- uses: actions/setup-node@v1
4950
- name: Checkout Frontend
5051
uses: actions/checkout@v2
5152
with:
@@ -57,7 +58,7 @@ jobs:
5758
id: node-modules
5859
with:
5960
path: captain-fact-frontend/node_modules
60-
key: ${{ runner.os }}-node-modules-${{ hashFiles('captain-fact-frontend/package-lock.json') }}
61+
key: ${{ runner.os }}-node_modules-${{ hashFiles('captain-fact-frontend/package-lock.json') }}
6162
- name: Install Frontend's dependencies
6263
working-directory: captain-fact-frontend
6364
run: npm ci --prefer-offline --no-audit

.github/workflows/release.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
_build
22
deps
3+
erl_crash.dump
34

45
# Local uploads
56
dev/resources

.tool-versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
erlang 21.0
2-
elixir 1.6-otp-21
1+
erlang 22.2.7
2+
elixir 1.10.1

apps/cf_atom_feed/config/config.exs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ config :cf_atom_feed,
1010
namespace: CF.AtomFeed,
1111
ecto_repos: [DB.Repo]
1212

13-
config :cf_atom_feed,
14-
CF.AtomFeed.Router,
15-
cowboy: [port: 4004]
16-
1713
# Configures Elixir's Logger
1814
config :logger, :console,
1915
format: "$time $metadata[$level] $message\n",

apps/cf_atom_feed/config/dev.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ use Mix.Config
22

33
# Do not include metadata nor timestamps in development logs
44
config :logger, :console, format: "[$level] $message\n"
5+
6+
config :cf_atom_feed,
7+
CF.AtomFeed.Router,
8+
cowboy: [port: 4004]

apps/cf_atom_feed/config/prod.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use Mix.Config
22

3-
config :cf_atom_feed, CF.AtomFeed.Router, cowboy: [port: 80]
4-
53
# Do not print debug messages in production
64
config :logger, level: :info

apps/cf_atom_feed/lib/application.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ defmodule CF.AtomFeed.Application do
66
def start(_type, _args) do
77
import Supervisor.Spec
88

9-
# Define workers and child supervisors to be supervised
10-
children = [
11-
# Start the endpoint when the application starts
12-
supervisor(CF.AtomFeed.Router, [])
13-
]
9+
children = []
10+
config = Application.get_env(:cf_atom_feed, CF.AtomFeed.Router)
11+
12+
if config[:cowboy] do
13+
children = [supervisor(CF.AtomFeed.Router, []) | children]
14+
end
1415

1516
# See https://hexdocs.pm/elixir/Supervisor.html
1617
# for other strategies and supported options

apps/cf_graphql/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ Following secrets must be configured in production:
88
- db_username
99
- db_password
1010
- db_name
11-
- basic_auth_password
1211
- frontend_url
1312
- host

apps/cf_graphql/config/config.exs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ use Mix.Config
99
config :cf_graphql,
1010
namespace: CF.Graphql,
1111
ecto_repos: [DB.Repo],
12-
env: Mix.env(),
13-
basic_auth: [
14-
username: "captain",
15-
password: "SetAtRuntime",
16-
realm: "GraphiQL Public Endpoint"
17-
]
12+
env: Mix.env()
1813

1914
# Configures the endpoint
2015
config :cf_graphql, CF.GraphQLWeb.Endpoint,

apps/cf_graphql/config/prod.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ config :logger, level: :info
55

66
# Configure endpoint
77
config :cf_graphql, CF.GraphQLWeb.Endpoint,
8-
http: [port: 80],
8+
server: false,
99
debug_errors: false,
1010
code_reloader: false,
1111
check_origin: false,

apps/cf_graphql/lib/router.ex

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,10 @@ defmodule CF.GraphQLWeb.Router do
1212
plug(CF.Graphql.AuthPipeline)
1313
end
1414

15-
pipeline :basic_auth do
16-
plug(BasicAuth, use_config: {:cf_graphql, :basic_auth})
17-
end
18-
1915
scope "/" do
2016
pipe_through(:api_auth)
2117

2218
scope @graphiql_route do
23-
if Mix.env() == :prod, do: pipe_through(:basic_auth)
24-
2519
forward(
2620
"/",
2721
Absinthe.Plug.GraphiQL,

apps/cf_graphql/mix.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ defmodule CF.Graphql.Mixfile do
3939
{:corsica, "~> 1.0"},
4040
{:absinthe_ecto, "~> 0.1.3"},
4141
{:absinthe_plug, "~> 1.4.1"},
42-
{:basic_auth, "~> 2.2.2"},
4342
{:kaur, "~> 1.1"},
4443
{:poison, "~> 3.1"},
4544

apps/cf_jobs/lib/application.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ defmodule CF.Jobs.Application do
66
def start(_type, _args) do
77
import Supervisor.Spec
88

9+
# Wait 30s before starting to give some time for the migrations to run
10+
:timer.sleep(30000)
11+
912
env = Application.get_env(:cf, :env)
1013
# Define workers and child supervisors to be supervised
1114
children = [

apps/cf_rest_api/config/prod.exs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use Mix.Config
22

33
config :cf_rest_api, CF.RestApi.Endpoint,
4-
url: [port: 80],
5-
http: [port: 80],
64
force_ssl: false,
7-
check_origin: []
5+
check_origin: [],
6+
server: false

apps/cf_rest_api/lib/views/error_view.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ defmodule CF.RestApi.ErrorView do
1616
%{error: message}
1717
end
1818

19-
def render("403.json", assigns) do
20-
Logger.debug(inspect(assigns))
19+
def render("403.json", _) do
2120
%{error: "forbidden"}
2221
end
2322

@@ -29,13 +28,12 @@ defmodule CF.RestApi.ErrorView do
2928
%{error: message}
3029
end
3130

32-
def render("error.json", assigns) do
33-
Logger.debug(inspect(assigns))
31+
def render("error.json", _) do
3432
%{error: "unexpected"}
3533
end
3634

3735
def render(_, assigns) do
38-
Logger.debug(inspect(assigns))
36+
IO.inspect(assigns)
3937
%{error: "unexpected"}
4038
end
4139
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use Mix.Config
2+
3+
# Configures the endpoint
4+
config :cf_reverse_proxy, port: 5000
5+
6+
# Import environment specific config
7+
import_config "#{Mix.env()}.exs"

apps/cf_reverse_proxy/config/dev.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use Mix.Config

apps/cf_reverse_proxy/config/prod.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use Mix.Config
2+
3+
config :cf_reverse_proxy, port: 80

apps/cf_reverse_proxy/config/test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use Mix.Config
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule CF.ReverseProxy.Application do
2+
use Application
3+
4+
def start(_type, _args) do
5+
import Supervisor.Spec, warn: false
6+
port = Application.get_env(:cf_reverse_proxy, :port)
7+
8+
websocket =
9+
{Phoenix.Transports.WebSocket, {CF.RestApi.Endpoint, CF.RestApi.UserSocket, :websocket}}
10+
11+
cowboy_options = [
12+
port: port,
13+
dispatch: [
14+
{:_,
15+
[
16+
{"/socket/websocket", Phoenix.Endpoint.CowboyWebSocket, websocket},
17+
{:_, Plug.Adapters.Cowboy.Handler, {CF.ReverseProxy.Plug, []}}
18+
]}
19+
]
20+
]
21+
22+
cowboy = Plug.Adapters.Cowboy.child_spec(:http, CF.ReverseProxy.Plug, [], cowboy_options)
23+
opts = [strategy: :one_for_one, name: CF.ReverseProxy.Supervisor]
24+
Supervisor.start_link([cowboy], opts)
25+
end
26+
27+
def config_change(_changed, _new, _removed) do
28+
:ok
29+
end
30+
31+
def version() do
32+
case :application.get_key(:cf_reverse_proxy, :vsn) do
33+
{:ok, version} -> to_string(version)
34+
_ -> "unknown"
35+
end
36+
end
37+
end

apps/cf_reverse_proxy/lib/plug.ex

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
defmodule CF.ReverseProxy.Plug do
2+
@moduledoc false
3+
4+
use Plug.Builder
5+
6+
plug(
7+
Corsica,
8+
max_age: 3600,
9+
allow_headers: ~w(Accept Content-Type Authorization Origin),
10+
origins: [~r/(.*)\.captainfact\.io$/]
11+
)
12+
13+
@default_host CF.RestApi.Endpoint
14+
@base_host_regex ~r/^(?<service>rest|graphql|feed)\./
15+
@subdomains %{
16+
"graphql" => CF.GraphQLWeb.Endpoint,
17+
"rest" => CF.RestApi.Endpoint,
18+
"feed" => CF.AtomFeed.Router
19+
}
20+
21+
def init(opts), do: opts
22+
23+
# See https://github.com/wojtekmach/acme_bank/blob/master/apps/master_proxy/lib/master_proxy/plug.ex
24+
# Or CaddyServer
25+
# https://elixirforum.com/t/umbrella-with-2-phoenix-apps-how-to-forward-request-from-1-to-2-and-vice-versa/1797/18?u=betree
26+
# https://github.com/jesseshieh/master_proxy
27+
28+
if Application.get_env(:cf, :env) == :dev do
29+
# Dev requests are routed through here
30+
def call(conn, _) do
31+
[path_info, endpoint] =
32+
case conn.path_info do
33+
["rest" | _] -> [tl(conn.path_info), CF.RestApi.Endpoint]
34+
["graphql" | _] -> [tl(conn.path_info), CF.GraphQLWeb.Endpoint]
35+
["feed" | _] -> [tl(conn.path_info), CF.AtomFeed.Router]
36+
path_info -> [path_info, CF.RestApi.Endpoint]
37+
end
38+
39+
conn
40+
|> Map.replace!(:path_info, path_info)
41+
|> Map.replace!(:request_path, Enum.join(path_info, "/"))
42+
|> endpoint.call(endpoint.init(nil))
43+
end
44+
else
45+
# Prod requests are routed through here
46+
def call(conn, _) do
47+
subdomain = get_domain_from_host(conn.host)
48+
endpoint = Map.get(@subdomains, subdomain, @default_host)
49+
endpoint.call(conn, endpoint.init(nil))
50+
end
51+
52+
defp get_domain_from_host(host) do
53+
@base_host_regex
54+
|> Regex.named_captures(host)
55+
|> case do
56+
%{"service" => service} -> service
57+
_ -> "rest"
58+
end
59+
end
60+
end
61+
end

apps/cf_reverse_proxy/mix.exs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule CF.ReverseProxy.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :cf_reverse_proxy,
7+
version: "1.0.0",
8+
build_path: "../../_build",
9+
compilers: [:phoenix] ++ Mix.compilers(),
10+
config_path: "../../config/config.exs",
11+
deps_path: "../../deps",
12+
lockfile: "../../mix.lock",
13+
elixir: "~> 1.6",
14+
elixirc_paths: elixirc_paths(Mix.env()),
15+
build_embedded: Mix.env() == :prod,
16+
start_permanent: Mix.env() == :prod,
17+
aliases: aliases(),
18+
deps: deps()
19+
]
20+
end
21+
22+
def application do
23+
[
24+
mod: {CF.ReverseProxy.Application, []},
25+
extra_applications: [:logger]
26+
]
27+
end
28+
29+
# Specifies which paths to compile per environment.
30+
defp elixirc_paths(:test), do: ["lib", "test/support"]
31+
defp elixirc_paths(:dev), do: ["lib"]
32+
defp elixirc_paths(_), do: ["lib"]
33+
34+
# Dependencies
35+
defp deps do
36+
[
37+
{:cf_rest_api, in_umbrella: true},
38+
{:cf_graphql, in_umbrella: true},
39+
{:cf_atom_feed, in_umbrella: true},
40+
{:phoenix, "~> 1.3.0"},
41+
{:cowboy, "~> 1.0"},
42+
{:corsica, "~> 1.0"}
43+
]
44+
end
45+
46+
defp aliases do
47+
[]
48+
end
49+
end

apps/db/lib/db/application.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule DB.Application do
22
@moduledoc false
33

44
use Application
5+
require Logger
56

67
def start(_type, _args) do
78
import Supervisor.Spec, warn: false
@@ -16,7 +17,19 @@ defmodule DB.Application do
1617
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
1718
# for other strategies and supported options
1819
opts = [strategy: :one_for_one, name: DB.Supervisor]
19-
Supervisor.start_link(children, opts)
20+
link = Supervisor.start_link(children, opts)
21+
migrate_db()
22+
link
23+
end
24+
25+
defp migrate_db do
26+
Logger.info("Running migrations...")
27+
Ecto.Migrator.run(DB.Repo, migrations_path(), :up, all: true)
28+
Logger.info("Migrated!")
29+
end
30+
31+
defp migrations_path do
32+
Path.join([:code.priv_dir(:db), "repo", "migrations"])
2033
end
2134

2235
def version() do

apps/db/priv/repo/migrations/20170118223600_create_postgres_extensions.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule DB.Repo.Migrations.CreatePostgresExtensions do
22
use Ecto.Migration
33

44
def change do
5-
execute("CREATE EXTENSION citext;")
6-
execute("CREATE EXTENSION unaccent;")
5+
execute("CREATE EXTENSION IF NOT EXISTS citext;")
6+
execute("CREATE EXTENSION IF NOT EXISTS unaccent;")
77
end
88
end

0 commit comments

Comments
 (0)