diff options
Diffstat (limited to 'lib/lsg')
-rw-r--r-- | lib/lsg/application.ex | 1 | ||||
-rw-r--r-- | lib/lsg/token.ex | 38 |
2 files changed, 39 insertions, 0 deletions
diff --git a/lib/lsg/application.ex b/lib/lsg/application.ex index 8ee8aa2..972767f 100644 --- a/lib/lsg/application.ex +++ b/lib/lsg/application.ex @@ -14,6 +14,7 @@ defmodule LSG.Application do # worker(LSG.Worker, [arg1, arg2, arg3]), worker(Registry, [[keys: :duplicate, name: LSG.BroadcastRegistry]], id: :registry_broadcast), worker(LSG.IcecastAgent, []), + worker(LSG.Token, []), #worker(LSG.Icecast, []), ] ++ LSG.IRC.application_childs diff --git a/lib/lsg/token.ex b/lib/lsg/token.ex new file mode 100644 index 0000000..33946d4 --- /dev/null +++ b/lib/lsg/token.ex @@ -0,0 +1,38 @@ +defmodule LSG.Token do + use GenServer + + def start_link() do + GenServer.start_link(__MODULE__, [], [name: __MODULE__]) + end + + def lookup(id) do + with \ + [{_, cred, date}] <- :ets.lookup(__MODULE__.ETS, id), + IO.inspect("cred: #{inspect cred} valid for #{inspect date} now #{inspect DateTime.utc_now()}"), + d when d > 0 <- DateTime.diff(date, DateTime.utc_now()) + do + {:ok, cred} + else + err -> {:error, err} + end + end + + def new(cred) do + GenServer.call(__MODULE__, {:new, cred}) + end + + def init(_) do + ets = :ets.new(__MODULE__.ETS, [:ordered_set, :named_table, :protected, {:read_concurrency, true}]) + {:ok, ets} + end + + def handle_call({:new, cred}, _, ets) do + id = IRC.UserTrack.Id.large_id() + expire = DateTime.utc_now() + |> DateTime.add(15*60, :second) + obj = {id, cred, expire} + :ets.insert(ets, obj) + {:reply, {:ok, id}, ets} + end + +end |