Browse Source

Move listen port to config file

undefined
Piotr Orzechowski 6 months ago
parent
commit
8d871cb892

+ 2
- 0
apps/kv_server/config/config.exs View File

@@ -28,3 +28,5 @@ use Mix.Config
28 28
 # here (which is why it is important to import them last).
29 29
 #
30 30
 #     import_config "#{Mix.env()}.exs"
31
+
32
+config :kv_server, :listen_port, 4040

+ 7
- 1
apps/kv_server/lib/kv_server/application.ex View File

@@ -6,7 +6,7 @@ defmodule KVServer.Application do
6 6
   use Application
7 7
 
8 8
   def start(_type, _args) do
9
-    port = String.to_integer(System.get_env("KV_PORT") || "4040")
9
+    port = listen_port()
10 10
     # List all child processes to be supervised
11 11
     children = [
12 12
       {Task.Supervisor, name: KVServer.TaskSupervisor},
@@ -20,4 +20,10 @@ defmodule KVServer.Application do
20 20
     opts = [strategy: :one_for_one, name: KVServer.Supervisor]
21 21
     Supervisor.start_link(children, opts)
22 22
   end
23
+
24
+  defp listen_port() do
25
+    env_port = System.get_env("KV_PORT")
26
+    conf_port = Application.fetch_env!(:kv_server, :listen_port)
27
+    String.to_integer(env_port || "#{conf_port}")
28
+  end
23 29
 end

+ 1
- 0
apps/kv_server/mix.exs View File

@@ -19,6 +19,7 @@ defmodule KVServer.MixProject do
19 19
   def application do
20 20
     [
21 21
       extra_applications: [:logger],
22
+      env: [listen_port: 4040],
22 23
       mod: {KVServer.Application, []}
23 24
     ]
24 25
   end

Loading…
Cancel
Save