Add Dockerfile.dev; remove Makefile; add more some mix tasks;

various minor refactors
This commit is contained in:
mitchell 2020-02-10 01:23:56 -05:00
parent 4d07f94e89
commit e8406a9818
8 changed files with 93 additions and 85 deletions

View file

@ -1,40 +1,5 @@
defmodule Shortnr do
@moduledoc """
The Shortnr application entry point. Check README for usage documenation.
Shortnr is a simple url shortening service.
"""
require Logger
use Application
@impl true
def start(_type, _args) do
children = [
{Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]}
]
if ets_implementation() == :dets do
{:ok, _} = :dets.open_file(:urls, type: :set)
else
:ets.new(:urls, [:set, :named_table])
end
Logger.info("server starting", port: port())
Supervisor.start_link(children, strategy: :one_for_one)
end
@impl true
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
end
@spec ets_implementation() :: atom()
defp ets_implementation, do: Application.fetch_env!(:shortnr, :ets_implementation)
end

View file

@ -0,0 +1,40 @@
defmodule Shortnr.Application do
@moduledoc """
The Shortnr application entry point. Check README for usage documenation.
"""
require Logger
use Application
@impl true
def start(_type, _args) do
children = [
{Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]}
]
if ets_implementation() == :dets do
{:ok, _} = :dets.open_file(:urls, type: :set)
else
:ets.new(:urls, [:set, :named_table])
end
Logger.info("server starting", port: port())
Supervisor.start_link(children, strategy: :one_for_one)
end
@impl true
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
end
@spec ets_implementation() :: atom()
defp ets_implementation, do: Application.fetch_env!(:shortnr, :ets_implementation)
end

View file

@ -4,7 +4,6 @@ defmodule Shortnr.URL do
domain service.
"""
alias Shortnr.Transport
alias Shortnr.URL.Util
defstruct id: "",
created_at: DateTime.utc_now(),
@ -20,7 +19,7 @@ defmodule Shortnr.URL do
@spec create(String.t(), module()) :: {:ok, String.t()} | Transport.error()
def create(url, repo) do
url_struct = %__MODULE__{id: Util.generate_id(), url: URI.parse(url)}
url_struct = %__MODULE__{id: generate_id(), url: URI.parse(url)}
{:ok, extant_url} = repo.get(url_struct.id)
@ -51,6 +50,14 @@ defmodule Shortnr.URL do
{:ok, "Success"}
end
@id_chars String.codepoints("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXWYZ0123456789")
@spec generate_id() :: String.t()
def generate_id do
for _ <- 0..7,
into: "",
do: Enum.random(@id_chars)
end
defimpl Transport.Text.Encodable do
alias Shortnr.URL
@spec encode(URL.t()) :: String.t()

View file

@ -1,12 +0,0 @@
defmodule Shortnr.URL.Util do
@moduledoc """
URL module utility functions.
"""
@id_chars String.codepoints("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXWYZ0123456789")
@spec generate_id() :: String.t()
def generate_id do
for _ <- 0..7,
into: "",
do: Enum.random(@id_chars)
end
end