Skip to content

Commit c4f99b7

Browse files
committed
Tau - set OTP application config in config/runtime.exs
This was previously set via Erlang in the pre-Elixir releases. Now trying to make things more idiomatic for Elixir developers.
1 parent ca98d25 commit c4f99b7

File tree

3 files changed

+42
-101
lines changed

3 files changed

+42
-101
lines changed

app/server/beam/tau/config/runtime.exs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,29 @@ import Config
66
# and secrets from environment variables or elsewhere. Do not define
77
# any compile-time configuration in here, as it won't be applied.
88

9-
log_path = case System.fetch_env("TAU_LOG_PATH") do
10-
{:ok, log_path} ->
11-
log_path
12-
_error ->
13-
"log/tau.log"
14-
end
9+
extract_env = fn name, kind, default ->
10+
env_val = System.get_env(name)
11+
12+
if !env_val do
13+
default
14+
else
15+
extracted =
16+
case kind do
17+
:int ->
18+
{val, ""} = Integer.parse(env_val)
19+
val
20+
21+
:bool ->
22+
dc_val = env_val |> String.downcase() |> String.trim()
23+
dc_val != "false" and dc_val != "0"
24+
25+
:string ->
26+
env_val
27+
end
28+
29+
extracted
30+
end
31+
end
1532

1633
config :tau,
1734
handle_otp_reports: true,
@@ -26,7 +43,7 @@ else
2643
end
2744

2845
config :logger, :tau_file_log,
29-
path: log_path,
46+
path: extract_env.("TAU_LOG_PATH", :string, "log/tau.log"),
3047
level: :info
3148

3249
# ## Using releases
@@ -39,18 +56,22 @@ config :logger, :tau_file_log,
3956
# Then you can assemble a release by calling `mix release`.
4057
# See `mix help release` for more information.
4158

42-
phx_port_default = 8001
43-
phx_port = case System.fetch_env("TAU_PHX_PORT") do
44-
{:ok, port_str} ->
45-
case Integer.parse(port_str) do
46-
{port, ""} ->
47-
port
48-
_other ->
49-
phx_port_default
50-
end
51-
_error ->
52-
phx_port_default
53-
end
59+
phx_port = extract_env.("TAU_PHX_PORT", :int, 8002)
60+
61+
config :tau,
62+
midi_on: extract_env.("TAU_MIDI_ON", :bool, false),
63+
midi_enabled: extract_env.("TAU_MIDI_ENABLED", :bool, false),
64+
link_enabled: extract_env.("TAU_LINK_ENABLED", :bool, false),
65+
cues_on: extract_env.("TAU_CUES_ON", :bool, true),
66+
osc_in_udp_loopback_restricted: extract_env.("TAU_OSC_IN_UDP_LOOPBACK_RESTRICTED", :bool, true),
67+
link_on: extract_env.("TAU_LINK_ON", :bool, false),
68+
osc_in_udp_port: extract_env.("TAU_OSC_IN_UDP_PORT", :int, 5000),
69+
api_port: extract_env.("TAU_API_PORT", :int, 5001),
70+
spider_port: extract_env.("TAU_SPIDER_PORT", :int, 5002),
71+
daemon_port: extract_env.("TAU_DAEMON_PORT", :int, -1),
72+
daemon_token: extract_env.("TAU_DAEMON_TOKEN", :int, -1),
73+
daemon_host: {127, 0, 0, 1},
74+
phx_port: phx_port
5475

5576
config :tau, TauWeb.Endpoint,
5677
server: true,

app/server/beam/tau/lib/tau/application.ex

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,8 @@ defmodule Tau.Application do
1010
def start(_type, _args) do
1111
Logger.info("All systems booting....")
1212

13-
_tau_env = extract_env("TAU_ENV", :string, "prod")
14-
midi_enabled = extract_env("TAU_MIDI_ENABLED", :bool, false)
15-
link_enabled = extract_env("TAU_LINK_ENABLED", :bool, false)
16-
cues_on = extract_env("TAU_CUES_ON", :bool, true)
17-
osc_in_udp_loopback_restricted = extract_env("TAU_OSC_IN_UDP_LOOPBACK_RESTRICTED", :bool, true)
18-
midi_on = extract_env("TAU_MIDI_ON", :bool, false)
19-
link_on = extract_env("TAU_LINK_ON", :bool, false)
20-
osc_in_udp_port = extract_env("TAU_OSC_IN_UDP_PORT", :int, 5000)
21-
api_port = extract_env("TAU_API_PORT", :int, 5001)
22-
spider_port = extract_env("TAU_SPIDER_PORT", :int, 5002)
23-
daemon_port = extract_env("TAU_DAEMON_PORT", :int, -1)
24-
daemon_token = extract_env("TAU_DAEMON_TOKEN", :int, -1)
25-
daemon_host = {127,0,0,1}
13+
midi_enabled = Application.get_env(:tau, :midi_enabled, false)
14+
link_enabled = Application.get_env(:tau, :link_enabled, false)
2615

2716
if midi_enabled do
2817
Logger.info("Initialising MIDI native interface")
@@ -38,21 +27,6 @@ defmodule Tau.Application do
3827
Logger.info("Starting without Link native interface")
3928
end
4029

41-
:tau_server_sup.set_application_env(
42-
midi_enabled,
43-
link_enabled,
44-
cues_on,
45-
osc_in_udp_loopback_restricted,
46-
midi_on,
47-
link_on,
48-
osc_in_udp_port,
49-
api_port,
50-
spider_port,
51-
daemon_port,
52-
daemon_token,
53-
daemon_host
54-
)
55-
5630
Logger.info("Starting Phoenix server")
5731

5832
children = [
@@ -83,31 +57,4 @@ defmodule Tau.Application do
8357
TauWeb.Endpoint.config_change(changed, removed)
8458
:ok
8559
end
86-
87-
defp extract_env(name, kind, default) do
88-
env_val = System.get_env(name)
89-
90-
if !env_val do
91-
Logger.info("No env variable supplied for #{name} using default: #{default}")
92-
default
93-
else
94-
extracted = extract_env(env_val, kind)
95-
Logger.info("Extracting env #{name} #{kind}: #{extracted}")
96-
extracted
97-
end
98-
end
99-
100-
defp extract_env(env_val, :int) do
101-
{val, ""} = Integer.parse(env_val)
102-
val
103-
end
104-
105-
defp extract_env(env_val, :bool) do
106-
dc_val = env_val |> String.downcase() |> String.trim()
107-
(dc_val != "false") and (dc_val != "0")
108-
end
109-
110-
defp extract_env(env_val, :string) do
111-
env_val
112-
end
11360
end

app/server/beam/tau/src/tau_server/tau_server_sup.erl

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
%% API
1010
-export([start_link/0, child_spec/1]).
11-
-export([set_application_env/12]).
1211

1312
%% Supervisor callbacks
1413
-export([init/1]).
@@ -42,32 +41,6 @@ start_link() ->
4241
%% NOTE: it is important that this code cannot fail, because that
4342
%% would prevent the application from even being started.
4443

45-
set_application_env(MIDIEnabled,
46-
LinkEnabled,
47-
CuesOn,
48-
OSCInUDPLoopbackRestricted,
49-
MidiOn,
50-
LinkOn,
51-
OSCInUDPPort,
52-
ApiPort,
53-
SpiderPort,
54-
DaemonPort,
55-
DaemonToken,
56-
DaemonHost) ->
57-
58-
application:set_env(?APPLICATION, midi_enabled, MIDIEnabled),
59-
application:set_env(?APPLICATION, link_enabled, LinkEnabled),
60-
application:set_env(?APPLICATION, cues_on, CuesOn),
61-
application:set_env(?APPLICATION, osc_in_udp_loopback_restricted, OSCInUDPLoopbackRestricted),
62-
application:set_env(?APPLICATION, midi_on, MidiOn),
63-
application:set_env(?APPLICATION, link_on, LinkOn),
64-
application:set_env(?APPLICATION, osc_in_udp_port, OSCInUDPPort),
65-
application:set_env(?APPLICATION, api_port, ApiPort),
66-
application:set_env(?APPLICATION, spider_port, SpiderPort),
67-
application:set_env(?APPLICATION, daemon_port, DaemonPort),
68-
application:set_env(?APPLICATION, daemon_token, DaemonToken),
69-
application:set_env(?APPLICATION, daemon_host, DaemonHost).
70-
7144
init(_Args) ->
7245
CueServer = tau_server_cue:server_name(),
7346
MIDIServer = tau_server_midi:server_name(),

0 commit comments

Comments
 (0)