file server started in supervisor tree

This commit is contained in:
Owen
2024-12-15 22:48:47 +01:00
parent 81c95c28b2
commit 1b1fa19fff
8 changed files with 30 additions and 9 deletions
+1 -3
View File
@@ -8,9 +8,7 @@ defmodule PlantIdDiscordBot.Cog.PlantNet do
ID a plant from up to 5 images of organs.
"""
def id(interaction) do
guild_id = interaction.guild_id
case RateLimiter.check_limit(guild_id) do
case RateLimiter.check_limit(interaction.guild_id) do
{:limit_exceeded, value} ->
@api.create_interaction_response(interaction, %{
type: 4,
+12
View File
@@ -0,0 +1,12 @@
defmodule PlantIdDiscordBot.FileServer do
use Plug.Builder
plug(Plug.Logger)
plug(Plug.Static,
at: "/public",
from: "/priv/static"
)
plug(PlantIdDiscordBot.FileServer.Router)
end
@@ -0,0 +1,15 @@
defmodule PlantIdDiscordBot.FileServer.Router do
use Plug.Router
plug(:match)
plug(:dispatch)
def init(options), do: options
get "/health" do
conn
|> send_resp(200, "OK")
end
match(_, do: conn |> send_resp(404, "Not Found"))
end
+52
View File
@@ -0,0 +1,52 @@
defmodule PlantIdDiscordBot.RateLimiter do
use GenServer
@request_limit Application.compile_env(:plantid_discord_bot, :guild_request_limit_per_day)
def start_link(_) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
@impl GenServer
def init(_) do
:ets.new(__MODULE__, [:named_table, :public, write_concurrency: true])
{:ok, nil}
end
defp put(key, value) do
:ets.insert(__MODULE__, {key, value})
end
defp get(key) do
case :ets.lookup(__MODULE__, key) do
[{^key, value}] -> {:ok, value}
[] -> {:error, nil}
end
end
@doc """
Check if the number of requests for a guild exceeds the limit.
"""
@spec check_limit(String.t()) :: {:ok, integer()} | {:limit_exceeded, integer()}
def check_limit(guild_id) do
case get(guild_id) do
{:ok, value} ->
if value >= @request_limit do
{:limit_exceeded, value}
else
{:ok, value}
end
{:error, _} ->
put(guild_id, 0)
{:ok, 0}
end
end
@doc """
Increase the counter for the number of requests for a guild by one.
"""
def increase_counter(guild_id) do
:ets.update_counter(__MODULE__, guild_id, 1)
end
end
+19
View File
@@ -0,0 +1,19 @@
defmodule PlantIdDiscordBot.Utils do
@start_time Application.compile_env(:plantid_discord_bot, :start_time)
def get_uptime() do
DateTime.diff(DateTime.utc_now(), @start_time)
|> PlantIdDiscordBot.Utils.Duration.sec_to_str()
end
def get_shard_latency() do
Nostrum.Util.get_all_shard_latencies()
|> Map.get(0)
|> to_string()
|> Kernel.<>("ms")
end
def get_guilds_names() do
Nostrum.Cache.GuildCache.fold([], fn %{name: name}, acc -> [name | acc] end)
end
end
+19
View File
@@ -0,0 +1,19 @@
# https://rosettacode.org/wiki/Convert_seconds_to_compound_duration
defmodule PlantIdDiscordBot.Utils.Duration do
@minute 60
@hour @minute * 60
@day @hour * 24
@week @day * 7
@divisor [@week, @day, @hour, @minute, 1]
def sec_to_str(sec) do
{_, [s, m, h, d, w]} =
Enum.reduce(@divisor, {sec, []}, fn divisor, {n, acc} ->
{rem(n, divisor), [div(n, divisor) | acc]}
end)
["#{w}w", "#{d}d", "#{h}h", "#{m}m", "#{s}s"]
|> Enum.reject(fn str -> String.starts_with?(str, "0") end)
|> Enum.join(" ")
end
end