Skip to content

Commit

Permalink
Allow bypassing app router with FARSIDE_NO_ROUTER
Browse files Browse the repository at this point in the history
Setting the aforementioned env var skips creation of the app router,
which is useful for running update.exs when the main app is already
running (otherwise there's a port conflict).
  • Loading branch information
benbusby committed Nov 16, 2021
1 parent 5904c7c commit d458179
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ request per second per IP.
- Run Farside: `mix run --no-halt`
- Uses localhost:4001

### Environment Variables

| Name | Purpose |
| -- | -- |
| FARSIDE_TEST | If enabled, skips the instance availability check in `update.exs`. |
| FARSIDE_NO_ROUTER | If enabled, skips creation of the router. Useful for running `update.exs` with `mix run` when the app is already running. |
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config

config :farside,
port: 4001,
redis_conn: "redis://localhost:6379",
update_file: ".update-results",
service_prefix: "service-",
Expand Down
17 changes: 14 additions & 3 deletions lib/farside/application.ex
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
defmodule Farside.Application do
@farside_port Application.fetch_env!(:farside, :port)
@redis_conn Application.fetch_env!(:farside, :redis_conn)
@moduledoc false

use Application

@impl true
def start(_type, _args) do
children = [
Plug.Cowboy.child_spec(scheme: :http, plug: Farside.Router, options: [port: 4001]),
{Redix, {@redis_conn, [name: :redix]}},
plug_children = [
Plug.Cowboy.child_spec(
scheme: :http,
plug: Farside.Router,
options: [
port: @farside_port
]
),
{PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000}
]

children = [
{Redix, {@redis_conn, [name: :redix]}} |
System.get_env("FARSIDE_NO_ROUTER") && [] || plug_children
]

opts = [strategy: :one_for_one, name: Farside.Supervisor]
Supervisor.start_link(children, opts)
end
Expand Down
2 changes: 1 addition & 1 deletion update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
SCRIPT_DIR="$(builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"

cd "$SCRIPT_DIR"
mix run update.exs
FARSIDE_NO_ROUTER=1 mix run update.exs

0 comments on commit d458179

Please sign in to comment.