mirror of
https://github.com/mitchell/shortnr.git
synced 2025-12-17 05:17:22 +00:00
Add Dockerfile.dev; remove Makefile; add more some mix tasks;
various minor refactors
This commit is contained in:
parent
4d07f94e89
commit
e8406a9818
8 changed files with 93 additions and 85 deletions
|
|
@ -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
|
||||
|
|
|
|||
40
lib/shortnr/application.ex
Normal file
40
lib/shortnr/application.ex
Normal 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
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue