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}
|
{Credo.Check.Warning.LazyLogging, false}
|
||||||
]
|
]
|
||||||
|
|
||||||
import_config "#{Mix.env}.exs"
|
import_config "#{Mix.env()}.exs"
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import Config
|
import Config
|
||||||
|
|
||||||
config :logger, :console,
|
config :logger, :console, level: :info
|
||||||
level: :info
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ defmodule Shortnr.Application do
|
||||||
require Logger
|
require Logger
|
||||||
use Application
|
use Application
|
||||||
|
|
||||||
@impl true
|
@impl Application
|
||||||
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()]}
|
||||||
|
@ -19,22 +19,25 @@ defmodule Shortnr.Application do
|
||||||
end
|
end
|
||||||
|
|
||||||
Logger.info("server starting", port: port())
|
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
|
end
|
||||||
|
|
||||||
@impl true
|
@impl Application
|
||||||
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
|
||||||
|
|
||||||
@spec port() :: integer()
|
@spec port() :: integer()
|
||||||
defp port do
|
defp port do
|
||||||
case Application.fetch_env(:shortnr, :port) do
|
{:ok, port} = Application.fetch_env(:shortnr, :port)
|
||||||
{:ok, port} -> port
|
port
|
||||||
_ -> 4000
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec ets_implementation() :: atom()
|
@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
|
end
|
||||||
|
|
|
@ -15,15 +15,12 @@ defmodule Shortnr.Router do
|
||||||
plug(:match)
|
plug(:match)
|
||||||
plug(:dispatch)
|
plug(:dispatch)
|
||||||
|
|
||||||
# BEGIN URL routes
|
post("/:url", do: URL.Endpoints.create(conn, url))
|
||||||
post("/:url", do: URL.Endpoints.select(conn, :create, url))
|
|
||||||
|
|
||||||
get("/", do: URL.Endpoints.select(conn, :list))
|
get("/", do: URL.Endpoints.list(conn))
|
||||||
get("/:id", do: URL.Endpoints.select(conn, :get, id))
|
get("/:id", do: URL.Endpoints.get(conn, id))
|
||||||
|
|
||||||
delete("/:id", do: URL.Endpoints.select(conn, :delete, id))
|
delete("/:id", do: URL.Endpoints.delete(conn, id))
|
||||||
|
|
||||||
# END
|
|
||||||
|
|
||||||
match _ do
|
match _ do
|
||||||
conn
|
conn
|
||||||
|
|
|
@ -3,15 +3,9 @@ defmodule Shortnr.URL.Endpoints do
|
||||||
This module implements the Endpoints behaviour.
|
This module implements the Endpoints behaviour.
|
||||||
"""
|
"""
|
||||||
alias Shortnr.Transport.{HTTP, Text}
|
alias Shortnr.Transport.{HTTP, Text}
|
||||||
alias Shortnr.Transport.HTTP.Endpoints
|
|
||||||
alias Shortnr.URL
|
alias Shortnr.URL
|
||||||
|
|
||||||
@behaviour Endpoints
|
def list(conn) do
|
||||||
|
|
||||||
@impl true
|
|
||||||
def select(conn, name, arg \\ nil)
|
|
||||||
|
|
||||||
def select(conn, :list, _arg) do
|
|
||||||
conn
|
conn
|
||||||
|> HTTP.wrap()
|
|> HTTP.wrap()
|
||||||
|> HTTP.handle(fn -> URL.list(URL.Repo.ETS) end)
|
|> HTTP.handle(fn -> URL.list(URL.Repo.ETS) end)
|
||||||
|
@ -19,7 +13,7 @@ defmodule Shortnr.URL.Endpoints do
|
||||||
|> HTTP.send(:ok)
|
|> HTTP.send(:ok)
|
||||||
end
|
end
|
||||||
|
|
||||||
def select(conn, :create, url) do
|
def create(conn, url) do
|
||||||
conn
|
conn
|
||||||
|> HTTP.wrap(url)
|
|> HTTP.wrap(url)
|
||||||
|> HTTP.handle(&URL.create(&1, URL.Repo.ETS))
|
|> HTTP.handle(&URL.create(&1, URL.Repo.ETS))
|
||||||
|
@ -27,7 +21,7 @@ defmodule Shortnr.URL.Endpoints do
|
||||||
|> HTTP.send(:created)
|
|> HTTP.send(:created)
|
||||||
end
|
end
|
||||||
|
|
||||||
def select(conn, :get, id) do
|
def get(conn, id) do
|
||||||
conn
|
conn
|
||||||
|> HTTP.wrap(id)
|
|> HTTP.wrap(id)
|
||||||
|> HTTP.handle(&URL.get(&1, URL.Repo.ETS))
|
|> HTTP.handle(&URL.get(&1, URL.Repo.ETS))
|
||||||
|
@ -35,7 +29,7 @@ defmodule Shortnr.URL.Endpoints do
|
||||||
|> HTTP.send(:found)
|
|> HTTP.send(:found)
|
||||||
end
|
end
|
||||||
|
|
||||||
def select(conn, :delete, id) do
|
def delete(conn, id) do
|
||||||
conn
|
conn
|
||||||
|> HTTP.wrap(id)
|
|> HTTP.wrap(id)
|
||||||
|> HTTP.handle(&URL.delete(&1, URL.Repo.ETS))
|
|> HTTP.handle(&URL.delete(&1, URL.Repo.ETS))
|
||||||
|
|
|
@ -37,5 +37,8 @@ defmodule Shortnr.URL.Repo.ETS do
|
||||||
:ok = ets().delete_all_objects(:urls)
|
:ok = ets().delete_all_objects(:urls)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp ets, do: Application.fetch_env!(:shortnr, :ets_implementation)
|
defp ets do
|
||||||
|
{:ok, impl} = Application.fetch_env(:shortnr, :ets_implementation)
|
||||||
|
impl
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue