Skip to content

Commit aed1cc6

Browse files
committed
Add tournaments ext_api stats
1 parent 1f52a4f commit aed1cc6

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
defmodule CodebattleWeb.ExtApi.TournamentController do
2+
use CodebattleWeb, :controller
3+
4+
import Plug.Conn
5+
6+
plug(CodebattleWeb.Plugs.TokenAuth)
7+
8+
# Define the JSON file paths
9+
@json_files [
10+
"/json/tournament1.json",
11+
"/json/tournament2.json",
12+
"/json/tournament3.json",
13+
"/json/tournament4.json",
14+
"/json/tournament5.json",
15+
"/json/tournament6.json",
16+
"/json/tournament7.json"
17+
]
18+
19+
# Use an Agent to store the current index
20+
def start_link do
21+
Agent.start_link(fn -> 0 end, name: __MODULE__)
22+
end
23+
24+
# Initialize the Agent when the application starts
25+
def init do
26+
if !Process.whereis(__MODULE__), do: start_link()
27+
end
28+
29+
def show(conn, %{"base_url" => base_url} = _params) do
30+
# Initialize the agent if it doesn't exist
31+
init()
32+
33+
# Get current index and update it for the next request
34+
current_index =
35+
Agent.get_and_update(__MODULE__, fn index ->
36+
next_index = rem(index + 1, length(@json_files))
37+
{index, next_index}
38+
end)
39+
40+
# Get the current JSON file path
41+
json_path = Enum.at(@json_files, current_index)
42+
43+
# Construct the full URL
44+
redirect_url = "#{base_url}#{json_path}"
45+
46+
# Redirect to the S3 bucket URL
47+
redirect(conn, external: redirect_url)
48+
end
49+
50+
def show(conn, _params) do
51+
conn
52+
|> put_status(:bad_request)
53+
|> json(%{error: "Missing base_url parameter"})
54+
end
55+
end

services/app/apps/codebattle/lib/codebattle_web/plugs/token_auth.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ defmodule CodebattleWeb.Plugs.TokenAuth do
77

88
def call(conn, _) do
99
key = Application.get_env(:codebattle, :api_key)
10+
user_key = get_key(conn)
1011

11-
if key && key == List.first(get_req_header(conn, "x-auth-key")) do
12+
if key && key == user_key do
1213
conn
1314
else
1415
conn
@@ -17,4 +18,11 @@ defmodule CodebattleWeb.Plugs.TokenAuth do
1718
|> halt()
1819
end
1920
end
21+
22+
defp get_key(conn) do
23+
case List.first(get_req_header(conn, "x-auth-key")) do
24+
nil -> conn.params["auth-key"]
25+
header_key -> header_key
26+
end
27+
end
2028
end

services/app/apps/codebattle/lib/codebattle_web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ defmodule CodebattleWeb.Router do
7272
post("/users", UserController, :create)
7373
post("/tasks", TaskController, :create)
7474
post("/task_packs", TaskPackController, :create)
75+
get("tournaments/:id", TournamentController, :show)
7576
end
7677

7778
scope "/", CodebattleWeb do

0 commit comments

Comments
 (0)