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, config :shortnr,
port: 8080, port: 8080,
ets_implementation: ets_implementation: :dets
(fn
:test -> :ets
_ -> :dets
end).(Mix.env())
config :logger, :console, config :logger, :console,
format: "date=$date time=$time level=$level$levelpad message=\"$message\" $metadata\n", format: "date=$date time=$time level=$level$levelpad message=\"$message\" $metadata\n",
metadata: [:port, :file, :line, :crash_reason, :stack], metadata: [:port, :file, :line, :crash_reason, :stack],
level: level: :debug
(fn
:prod -> :info
_ -> :debug
end).(Mix.env())
config :credo, config :credo,
checks: [ checks: [
@ -23,3 +15,5 @@ config :credo,
{Credo.Check.Refactor.MapInto, false}, {Credo.Check.Refactor.MapInto, false},
{Credo.Check.Warning.LazyLogging, 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 require Logger
use Application use Application
@impl Application @impl true
def start(_type, _args) do def start(_type, _args) do
children = [ children = [
{Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]} {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) Supervisor.start_link(children, strategy: :one_for_one)
end end
@impl Application @impl true
def stop(_state) do def stop(_state) do
if ets_implementation() == :dets, do: :dets.close(:urls) if ets_implementation() == :dets, do: :dets.close(:urls)
end end

View File

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

View File

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