Skip to content

Commit b2ff4b5

Browse files
committed
Docker building and release
1 parent 76486d3 commit b2ff4b5

File tree

15 files changed

+159
-58
lines changed

15 files changed

+159
-58
lines changed

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM elixir:1.13.4-alpine AS builder
2+
3+
WORKDIR /app
4+
5+
COPY mix.exs .
6+
COPY mix.lock .
7+
COPY .formatter.exs .
8+
9+
RUN mix local.hex --force
10+
RUN mix local.rebar --force
11+
RUN mix deps.get
12+
13+
COPY lib/ ./lib/
14+
COPY config/ ./config/
15+
16+
ENV MIX_ENV=prod
17+
RUN mix release
18+
19+
FROM elixir:1.13.4-alpine
20+
21+
WORKDIR /app
22+
23+
COPY --from=builder /app/_build/prod/rel/prod/ ./_build/prod/rel/prod/
24+
25+
ENV MIX_ENV=prod
26+
CMD ["_build/prod/rel/prod/bin/prod", "start"]

config/config.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Config
22

33
config :srh,
4-
mode: "file",
5-
file_path: "srh-config/tokens.json"
4+
mode: "file",
5+
file_path: "srh-config/tokens.json"
66

7-
import_config "#{config_env()}.exs"
7+
import_config "#{config_env()}.exs"

config/prod.exs

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

config/runtime.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Config
22

33
config :srh,
4-
mode: System.get_env("TOKEN_RESOLUTION_MODE") || "file",
5-
file_path: System.get_env("TOKEN_RESOLUTION_FILE_PATH") || "srh-config/tokens.json"
4+
mode: System.get_env("TOKEN_RESOLUTION_MODE") || "file",
5+
file_path: System.get_env("TOKEN_RESOLUTION_FILE_PATH") || "srh-config/tokens.json"

lib/srh/auth/token_resolver.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule Srh.Auth.TokenResolver do
5555
# Load this into ETS
5656
Enum.each(
5757
config_file_data,
58-
&(:ets.insert(@ets_table_name, &1))
58+
&:ets.insert(@ets_table_name, &1)
5959
)
6060
end
6161

@@ -78,13 +78,11 @@ defmodule Srh.Auth.TokenResolver do
7878
:ok,
7979
# This is done to replicate what will eventually be API endpoints, so they keys are not atoms
8080
Jason.decode!(
81-
Jason.encode!(
82-
%{
83-
srh_id: "1000",
84-
connection_string: "redis://localhost:6379",
85-
max_connections: 10
86-
}
87-
)
81+
Jason.encode!(%{
82+
srh_id: "1000",
83+
connection_string: "redis://localhost:6379",
84+
max_connections: 10
85+
})
8886
)
8987
}
9088
end

lib/srh/http/base_router.ex

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@ defmodule Srh.Http.BaseRouter do
33
alias Srh.Http.RequestValidator
44
alias Srh.Http.CommandHandler
55

6-
plug :match
7-
plug Plug.Parsers, parsers: [:json], pass: ["application/json"], json_decoder: Jason
8-
plug :dispatch
6+
plug(:match)
7+
plug(Plug.Parsers, parsers: [:json], pass: ["application/json"], json_decoder: Jason)
8+
plug(:dispatch)
99

1010
get "/" do
1111
handle_response({:ok, "Welcome to Serverless Redis HTTP!"}, conn)
1212
end
1313

1414
post "/" do
1515
conn
16-
|> handle_extract_auth(&(CommandHandler.handle_command(conn, &1)))
16+
|> handle_extract_auth(&CommandHandler.handle_command(conn, &1))
1717
|> handle_response(conn)
1818
end
1919

2020
post "/pipeline" do
2121
conn
22-
|> handle_extract_auth(
23-
&(CommandHandler.handle_command_array(conn, &1))
24-
)
22+
|> handle_extract_auth(&CommandHandler.handle_command_array(conn, &1))
2523
|> handle_response(conn)
2624
end
2725

@@ -32,10 +30,10 @@ defmodule Srh.Http.BaseRouter do
3230
defp handle_extract_auth(conn, success_lambda) do
3331
case conn
3432
|> get_req_header("authorization")
35-
|> RequestValidator.validate_bearer_header()
36-
do
33+
|> RequestValidator.validate_bearer_header() do
3734
{:ok, token} ->
3835
success_lambda.(token)
36+
3937
{:error, _} ->
4038
{:malformed_data, "Missing/Invalid authorization header"}
4139
end
@@ -44,11 +42,21 @@ defmodule Srh.Http.BaseRouter do
4442
defp handle_response(response, conn) do
4543
%{code: code, message: message, json: json} =
4644
case response do
47-
{:ok, data} -> %{code: 200, message: Jason.encode!(data), json: true}
48-
{:not_found, message} -> %{code: 404, message: message, json: false}
49-
{:malformed_data, message} -> %{code: 400, message: message, json: false}
50-
{:not_authorized, message} -> %{code: 401, message: message, json: false}
51-
{:server_error, _} -> %{code: 500, message: "An error occurred internally", json: false}
45+
{:ok, data} ->
46+
%{code: 200, message: Jason.encode!(data), json: true}
47+
48+
{:not_found, message} ->
49+
%{code: 404, message: message, json: false}
50+
51+
{:malformed_data, message} ->
52+
%{code: 400, message: message, json: false}
53+
54+
{:not_authorized, message} ->
55+
%{code: 401, message: message, json: false}
56+
57+
{:server_error, _} ->
58+
%{code: 500, message: "An error occurred internally", json: false}
59+
5260
_ ->
5361
%{code: 500, message: "An error occurred internally", json: false}
5462
end
@@ -57,6 +65,7 @@ defmodule Srh.Http.BaseRouter do
5765
true ->
5866
conn
5967
|> put_resp_header("content-type", "application/json")
68+
6069
false ->
6170
conn
6271
end

lib/srh/http/command_handler.ex

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule Srh.Http.CommandHandler do
88
case RequestValidator.validate_redis_body(conn.body_params) do
99
{:ok, command_array} ->
1010
do_handle_command(command_array, token)
11+
1112
{:error, error_message} ->
1213
{:malformed_data, error_message}
1314
end
@@ -17,6 +18,7 @@ defmodule Srh.Http.CommandHandler do
1718
case RequestValidator.validate_pipeline_redis_body(conn.body_params) do
1819
{:ok, array_of_command_arrays} ->
1920
do_handle_command_array(array_of_command_arrays, token)
21+
2022
{:error, error_message} ->
2123
{:malformed_data, error_message}
2224
end
@@ -26,28 +28,34 @@ defmodule Srh.Http.CommandHandler do
2628
case TokenResolver.resolve(token) do
2729
{:ok, connection_info} ->
2830
dispatch_command(command_array, connection_info)
29-
{:error, msg} -> {:not_authorized, msg}
31+
32+
{:error, msg} ->
33+
{:not_authorized, msg}
3034
end
3135
end
3236

3337
defp do_handle_command_array(array_of_command_arrays, token) do
3438
case TokenResolver.resolve(token) do
3539
{:ok, connection_info} ->
3640
dispatch_command_array(array_of_command_arrays, connection_info)
37-
{:error, msg} -> {:not_authorized, msg}
41+
42+
{:error, msg} ->
43+
{:not_authorized, msg}
3844
end
3945
end
4046

4147
defp dispatch_command_array(_arr, _connection_info, responses \\ [])
42-
48+
4349
defp dispatch_command_array([current | rest], connection_info, responses) do
44-
updated_responses = case dispatch_command(current, connection_info) do
45-
{:ok, result_map} ->
46-
[result_map | responses]
47-
{:malformed_data, result_json} ->
48-
# TODO: change up the chain to json this at the last moment, so this isn't here
49-
[Jason.decode!(result_json) | responses]
50-
end
50+
updated_responses =
51+
case dispatch_command(current, connection_info) do
52+
{:ok, result_map} ->
53+
[result_map | responses]
54+
55+
{:malformed_data, result_json} ->
56+
# TODO: change up the chain to json this at the last moment, so this isn't here
57+
[Jason.decode!(result_json) | responses]
58+
end
5159

5260
dispatch_command_array(rest, connection_info, updated_responses)
5361
end
@@ -57,7 +65,10 @@ defmodule Srh.Http.CommandHandler do
5765
{:ok, Enum.reverse(responses)}
5866
end
5967

60-
defp dispatch_command(command_array, %{"srh_id" => srh_id, "max_connections" => max_connections} = connection_info)
68+
defp dispatch_command(
69+
command_array,
70+
%{"srh_id" => srh_id, "max_connections" => max_connections} = connection_info
71+
)
6172
when is_number(max_connections) do
6273
case GenRegistry.lookup_or_start(Client, srh_id, [max_connections, connection_info]) do
6374
{:ok, pid} ->
@@ -66,16 +77,16 @@ defmodule Srh.Http.CommandHandler do
6677
|> ClientWorker.redis_command(command_array) do
6778
{:ok, res} ->
6879
{:ok, %{result: res}}
80+
6981
{:error, error} ->
7082
{
7183
:malformed_data,
72-
Jason.encode!(
73-
%{
74-
error: error.message
75-
}
76-
)
84+
Jason.encode!(%{
85+
error: error.message
86+
})
7787
}
7888
end
89+
7990
{:error, msg} ->
8091
{:server_error, msg}
8192
end

lib/srh/http/request_validator.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
defmodule Srh.Http.RequestValidator do
2-
def validate_redis_body(%{"_json" => command_array}) when is_list(command_array), do: {:ok, command_array}
2+
def validate_redis_body(%{"_json" => command_array}) when is_list(command_array),
3+
do: {:ok, command_array}
34

45
def validate_redis_body(_),
5-
do: {:error, "Invalid command array. Expected a string array at root of the command and its arguments."}
6+
do:
7+
{:error,
8+
"Invalid command array. Expected a string array at root of the command and its arguments."}
69

7-
def validate_pipeline_redis_body(%{"_json" => array_of_command_arrays}) when is_list(array_of_command_arrays) do
10+
def validate_pipeline_redis_body(%{"_json" => array_of_command_arrays})
11+
when is_list(array_of_command_arrays) do
812
do_validate_pipeline_redis_body(array_of_command_arrays, array_of_command_arrays)
913
end
1014

@@ -32,6 +36,7 @@ defmodule Srh.Http.RequestValidator do
3236
|> String.split(" ") do
3337
["Bearer", token] ->
3438
{:ok, token}
39+
3540
_ ->
3641
do_validate_bearer_header(rest)
3742
end

lib/srh/redis/client.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule Srh.Redis.Client do
2323
}
2424
end
2525

26-
def find_worker(client) do
26+
def find_worker(client) do
2727
GenServer.call(client, {:find_worker})
2828
end
2929

lib/srh/redis/client_registry.ex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ defmodule Srh.Redis.ClientRegistry do
3535
len ->
3636
target = state.last_worker_index + 1
3737

38-
corrected_target = case target >= len do
39-
true -> 0
40-
false -> target
41-
end
42-
43-
{:reply, {:ok, Enum.at(state.worker_pids, corrected_target)}, %{state | last_worker_index: corrected_target}}
38+
corrected_target =
39+
case target >= len do
40+
true -> 0
41+
false -> target
42+
end
43+
44+
{:reply, {:ok, Enum.at(state.worker_pids, corrected_target)},
45+
%{state | last_worker_index: corrected_target}}
4446
end
4547
end
4648

@@ -55,10 +57,9 @@ defmodule Srh.Redis.ClientRegistry do
5557
:noreply,
5658
%{
5759
state
58-
|
59-
worker_pids:
60-
[pid | state.worker_pids]
61-
|> Enum.uniq()
60+
| worker_pids:
61+
[pid | state.worker_pids]
62+
|> Enum.uniq()
6263
}
6364
}
6465
end

0 commit comments

Comments
 (0)