hiett commited on
Commit
40d9f07
·
1 Parent(s): 126a6a1

Add support for environment variable loading for a single redis connection

Browse files
example/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
  import {Redis} from "@upstash/redis";
2
 
3
  const redis = new Redis({
4
- url: "http://srh:8080",
5
  token: "example_token",
6
  responseEncoding: false,
7
  });
 
1
  import {Redis} from "@upstash/redis";
2
 
3
  const redis = new Redis({
4
+ url: "http://127.0.0.1:8080",
5
  token: "example_token",
6
  responseEncoding: false,
7
  });
lib/srh/auth/token_resolver.ex CHANGED
@@ -1,7 +1,6 @@
1
  defmodule Srh.Auth.TokenResolver do
2
  use GenServer
3
 
4
- @mode Application.fetch_env!(:srh, :mode)
5
  @file_path Application.fetch_env!(:srh, :file_path)
6
 
7
  @ets_table_name :srh_token_resolver
@@ -25,7 +24,7 @@ defmodule Srh.Auth.TokenResolver do
25
  table = :ets.new(@ets_table_name, [:named_table, read_concurrency: true])
26
 
27
  # Populate the ETS table with data from storage
28
- do_init_load(@mode)
29
 
30
  {
31
  :ok,
@@ -36,7 +35,7 @@ defmodule Srh.Auth.TokenResolver do
36
  end
37
 
38
  def resolve(token) do
39
- do_resolve(@mode, token)
40
  end
41
 
42
  # Server methods
@@ -49,6 +48,10 @@ defmodule Srh.Auth.TokenResolver do
49
  end
50
 
51
  # Internal server
 
 
 
 
52
  defp do_init_load("file") do
53
  config_file_data = Jason.decode!(File.read!(@file_path))
54
  IO.puts("Loaded config file from disk. #{map_size(config_file_data)} entries.")
@@ -59,6 +62,25 @@ defmodule Srh.Auth.TokenResolver do
59
  )
60
  end
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  defp do_init_load(_), do: :ok
63
 
64
  # Internal, but client side, methods. These are client side to prevent GenServer lockup
@@ -73,6 +95,9 @@ defmodule Srh.Auth.TokenResolver do
73
  end
74
  end
75
 
 
 
 
76
  defp do_resolve("redis", _token) do
77
  {
78
  :ok,
 
1
  defmodule Srh.Auth.TokenResolver do
2
  use GenServer
3
 
 
4
  @file_path Application.fetch_env!(:srh, :file_path)
5
 
6
  @ets_table_name :srh_token_resolver
 
24
  table = :ets.new(@ets_table_name, [:named_table, read_concurrency: true])
25
 
26
  # Populate the ETS table with data from storage
27
+ do_init_load(get_token_loader_mode())
28
 
29
  {
30
  :ok,
 
35
  end
36
 
37
  def resolve(token) do
38
+ do_resolve(get_token_loader_mode(), token)
39
  end
40
 
41
  # Server methods
 
48
  end
49
 
50
  # Internal server
51
+ defp get_token_loader_mode() do
52
+ System.get_env("SRH_MODE", "file")
53
+ end
54
+
55
  defp do_init_load("file") do
56
  config_file_data = Jason.decode!(File.read!(@file_path))
57
  IO.puts("Loaded config file from disk. #{map_size(config_file_data)} entries.")
 
62
  )
63
  end
64
 
65
+ defp do_init_load("env") do
66
+ srh_token = System.get_env("SRH_TOKEN")
67
+ srh_connection_string = System.get_env("SRH_CONNECTION_STRING")
68
+
69
+ # Returns an error if fails, first tuple value is the number
70
+ {srh_max_connections, ""} = Integer.parse(System.get_env("SRH_MAX_CONNECTIONS", "3"))
71
+
72
+ # Create a config-file-like structure that the ETS layout expects, with just one entry
73
+ config_file_data = Map.put(%{}, srh_token, %{
74
+ "srh_id" => "env_config_connection", # Jason.parse! expects these keys to be strings, not atoms, so we need to replicate that setup
75
+ "connection_string" => srh_connection_string,
76
+ "max_connections" => srh_max_connections
77
+ })
78
+
79
+ IO.puts("Loaded config from env. #{map_size(config_file_data)} entries.")
80
+ # Load this into ETS
81
+ Enum.each(config_file_data, &:ets.insert(@ets_table_name, &1))
82
+ end
83
+
84
  defp do_init_load(_), do: :ok
85
 
86
  # Internal, but client side, methods. These are client side to prevent GenServer lockup
 
95
  end
96
  end
97
 
98
+ # The env strategy uses the same ETS table as the file strategy, so we can fall back on that
99
+ defp do_resolve("env", token), do: do_resolve("file", token)
100
+
101
  defp do_resolve("redis", _token) do
102
  {
103
  :ok,