mirror of https://github.com/mitchell/shortnr.git
General cleanups and refactor endpoints
This commit is contained in:
parent
4750555cf8
commit
80d6cfbb31
|
@ -16,4 +16,4 @@ config :credo,
|
|||
{Credo.Check.Warning.LazyLogging, false}
|
||||
]
|
||||
|
||||
import_config "#{Mix.env}.exs"
|
||||
import_config "#{Mix.env()}.exs"
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import Config
|
||||
|
||||
config :logger, :console,
|
||||
level: :info
|
||||
config :logger, :console, level: :info
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue