Break apart config by stage; refactor project structure

This commit is contained in:
mitchell 2020-02-07 23:53:08 -05:00
parent 98684feb2d
commit 4d07f94e89
14 changed files with 20 additions and 18 deletions

View File

@ -2,20 +2,12 @@ import Config
config :shortnr,
port: 8080,
ets_implementation:
(fn
:test -> :ets
_ -> :dets
end).(Mix.env())
ets_implementation: :dets
config :logger, :console,
format: "date=$date time=$time level=$level$levelpad message=\"$message\" $metadata\n",
metadata: [:port, :file, :line, :crash_reason, :stack],
level:
(fn
:prod -> :info
_ -> :debug
end).(Mix.env())
level: :debug
config :credo,
checks: [
@ -23,3 +15,5 @@ config :credo,
{Credo.Check.Refactor.MapInto, false},
{Credo.Check.Warning.LazyLogging, false}
]
import_config "#{Mix.env}.exs"

0
config/dev.exs Normal file
View File

4
config/prod.exs Normal file
View File

@ -0,0 +1,4 @@
import Config
config :logger, :console,
level: :info

4
config/test.exs Normal file
View File

@ -0,0 +1,4 @@
import Config
config :shortnr,
ets_implementation: :ets

View File

@ -6,7 +6,7 @@ defmodule Shortnr do
require Logger
use Application
@impl Application
@impl true
def start(_type, _args) do
children = [
{Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]}
@ -22,7 +22,7 @@ defmodule Shortnr do
Supervisor.start_link(children, strategy: :one_for_one)
end
@impl Application
@impl true
def stop(_state) do
if ets_implementation() == :dets, do: :dets.close(:urls)
end

View File

@ -8,7 +8,7 @@ defmodule Shortnr.URL.Endpoints do
@behaviour Endpoints
@impl Endpoints
@impl true
def select(conn, name, arg \\ nil)
def select(conn, :list, _arg) do

View File

@ -6,7 +6,7 @@ defmodule Shortnr.URL.Repo.ETS do
@behaviour Repo
@impl Repo
@impl true
def get(key) do
case ets().lookup(:urls, key) |> List.first() do
{_, url} -> {:ok, url}
@ -14,25 +14,25 @@ defmodule Shortnr.URL.Repo.ETS do
end
end
@impl Repo
@impl true
def put(url) do
:ok = ets().insert(:urls, {url.id, url})
:ok
end
@impl Repo
@impl true
def list do
resp = ets().select(:urls, [{:"$1", [], [:"$1"]}])
{:ok, resp |> Enum.map(&elem(&1, 1))}
end
@impl Repo
@impl true
def delete(key) do
:ok = ets().delete(:urls, key)
:ok
end
@impl Repo
@impl true
def reset do
:ok = ets().delete_all_objects(:urls)
end