diff --git a/config/config.exs b/config/config.exs index 8a1882e..39206a4 100644 --- a/config/config.exs +++ b/config/config.exs @@ -16,4 +16,4 @@ config :credo, {Credo.Check.Warning.LazyLogging, false} ] -import_config "#{Mix.env}.exs" +import_config "#{Mix.env()}.exs" diff --git a/config/prod.exs b/config/prod.exs index c7a966a..e468b47 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -1,4 +1,3 @@ import Config -config :logger, :console, - level: :info +config :logger, :console, level: :info diff --git a/lib/shortnr/application.ex b/lib/shortnr/application.ex index fe881af..4e03755 100644 --- a/lib/shortnr/application.ex +++ b/lib/shortnr/application.ex @@ -6,7 +6,7 @@ defmodule Shortnr.Application do require Logger use Application - @impl true + @impl Application def start(_type, _args) do children = [ {Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]} @@ -19,22 +19,25 @@ defmodule Shortnr.Application do end Logger.info("server starting", port: port()) - Supervisor.start_link(children, strategy: :one_for_one) + + opts = [strategy: :one_for_one, name: Shortnr.Supervisor] + Supervisor.start_link(children, opts) end - @impl true + @impl Application def stop(_state) do if ets_implementation() == :dets, do: :dets.close(:urls) end @spec port() :: integer() defp port do - case Application.fetch_env(:shortnr, :port) do - {:ok, port} -> port - _ -> 4000 - end + {:ok, port} = Application.fetch_env(:shortnr, :port) + port end @spec ets_implementation() :: atom() - defp ets_implementation, do: Application.fetch_env!(:shortnr, :ets_implementation) + defp ets_implementation do + {:ok, impl} = Application.fetch_env(:shortnr, :ets_implementation) + impl + end end diff --git a/lib/shortnr/router.ex b/lib/shortnr/router.ex index ab314be..0d26474 100644 --- a/lib/shortnr/router.ex +++ b/lib/shortnr/router.ex @@ -15,15 +15,12 @@ defmodule Shortnr.Router do plug(:match) plug(:dispatch) - # BEGIN URL routes - post("/:url", do: URL.Endpoints.select(conn, :create, url)) + post("/:url", do: URL.Endpoints.create(conn, url)) - get("/", do: URL.Endpoints.select(conn, :list)) - get("/:id", do: URL.Endpoints.select(conn, :get, id)) + get("/", do: URL.Endpoints.list(conn)) + get("/:id", do: URL.Endpoints.get(conn, id)) - delete("/:id", do: URL.Endpoints.select(conn, :delete, id)) - - # END + delete("/:id", do: URL.Endpoints.delete(conn, id)) match _ do conn diff --git a/lib/shortnr/url/endpoints.ex b/lib/shortnr/url/endpoints.ex index d3842dc..dabe7ad 100644 --- a/lib/shortnr/url/endpoints.ex +++ b/lib/shortnr/url/endpoints.ex @@ -3,15 +3,9 @@ defmodule Shortnr.URL.Endpoints do This module implements the Endpoints behaviour. """ alias Shortnr.Transport.{HTTP, Text} - alias Shortnr.Transport.HTTP.Endpoints alias Shortnr.URL - @behaviour Endpoints - - @impl true - def select(conn, name, arg \\ nil) - - def select(conn, :list, _arg) do + def list(conn) do conn |> HTTP.wrap() |> HTTP.handle(fn -> URL.list(URL.Repo.ETS) end) @@ -19,7 +13,7 @@ defmodule Shortnr.URL.Endpoints do |> HTTP.send(:ok) end - def select(conn, :create, url) do + def create(conn, url) do conn |> HTTP.wrap(url) |> HTTP.handle(&URL.create(&1, URL.Repo.ETS)) @@ -27,7 +21,7 @@ defmodule Shortnr.URL.Endpoints do |> HTTP.send(:created) end - def select(conn, :get, id) do + def get(conn, id) do conn |> HTTP.wrap(id) |> HTTP.handle(&URL.get(&1, URL.Repo.ETS)) @@ -35,7 +29,7 @@ defmodule Shortnr.URL.Endpoints do |> HTTP.send(:found) end - def select(conn, :delete, id) do + def delete(conn, id) do conn |> HTTP.wrap(id) |> HTTP.handle(&URL.delete(&1, URL.Repo.ETS)) diff --git a/lib/shortnr/url/repo/ets.ex b/lib/shortnr/url/repo/ets.ex index 0b27dc2..e64a57b 100644 --- a/lib/shortnr/url/repo/ets.ex +++ b/lib/shortnr/url/repo/ets.ex @@ -37,5 +37,8 @@ defmodule Shortnr.URL.Repo.ETS do :ok = ets().delete_all_objects(:urls) end - defp ets, do: Application.fetch_env!(:shortnr, :ets_implementation) + defp ets do + {:ok, impl} = Application.fetch_env(:shortnr, :ets_implementation) + impl + end end