1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
defmodule Nola.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
|