From e8406a98181220d48c3e2e8b4ec4b91dc5a313da Mon Sep 17 00:00:00 2001 From: mitchell Date: Mon, 10 Feb 2020 01:23:56 -0500 Subject: [PATCH] Add Dockerfile.dev; remove Makefile; add more some mix tasks; various minor refactors --- Dockerfile.dev | 11 +++++++++++ Makefile | 20 ------------------- lib/shortnr.ex | 37 +---------------------------------- lib/shortnr/application.ex | 40 ++++++++++++++++++++++++++++++++++++++ lib/shortnr/url.ex | 11 +++++++++-- lib/shortnr/url/util.ex | 12 ------------ mix.exs | 23 +++++++++++++++++++--- mix.lock | 24 +++++++++++------------ 8 files changed, 93 insertions(+), 85 deletions(-) create mode 100644 Dockerfile.dev delete mode 100644 Makefile create mode 100644 lib/shortnr/application.ex delete mode 100644 lib/shortnr/url/util.ex diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..8c96921 --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,11 @@ +FROM elixir:1.10 as dev + +WORKDIR /root/shortnr + +RUN apt-get update +RUN apt-get install -y fish vim + +RUN mix local.hex --force +RUN mix local.rebar --force + +CMD ["sleep", "infinity"] diff --git a/Makefile b/Makefile deleted file mode 100644 index 52abdaa..0000000 --- a/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -.PHONY: all build clean install lint start test - -build: install lint test clean - docker build -t shortnr:latest . - -clean: - mix clean - -install: - mix deps.get - -lint: - mix dialyzer - mix credo --strict - -start: - iex -S mix run --no-halt - -test: - mix test diff --git a/lib/shortnr.ex b/lib/shortnr.ex index 5459cc6..9d82959 100644 --- a/lib/shortnr.ex +++ b/lib/shortnr.ex @@ -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 diff --git a/lib/shortnr/application.ex b/lib/shortnr/application.ex new file mode 100644 index 0000000..fe881af --- /dev/null +++ b/lib/shortnr/application.ex @@ -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 diff --git a/lib/shortnr/url.ex b/lib/shortnr/url.ex index 73ebaa3..d02fdc1 100644 --- a/lib/shortnr/url.ex +++ b/lib/shortnr/url.ex @@ -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() diff --git a/lib/shortnr/url/util.ex b/lib/shortnr/url/util.ex deleted file mode 100644 index 846e462..0000000 --- a/lib/shortnr/url/util.ex +++ /dev/null @@ -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 diff --git a/mix.exs b/mix.exs index 9caa4bb..f89ef5e 100644 --- a/mix.exs +++ b/mix.exs @@ -6,17 +6,18 @@ defmodule Shortnr.MixProject do app: :shortnr, description: "A small & simple url shortener", version: "0.1.0", - elixir: "~> 1.9", + elixir: "~> 1.10", start_permanent: Mix.env() == :prod, dialyzer: [ignore_warnings: "dialyzer.ignore-warnings"], - deps: deps() + deps: deps(), + aliases: aliases() ] end # Run "mix help compile.app" to learn about applications. def application do [ - mod: {Shortnr, []}, + mod: {Shortnr.Application, []}, extra_applications: [:logger] ] end @@ -32,4 +33,20 @@ defmodule Shortnr.MixProject do # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} ] end + + defp aliases do + [ + build: &docker_build/1, + "build.dev": &docker_build_dev/1, + lint: ["compile", "dialyzer", "credo --strict"] + ] + end + + defp docker_build(_) do + 0 = Mix.shell().cmd("docker build -t shortnr:latest .") + end + + defp docker_build_dev(_) do + 0 = Mix.shell().cmd("docker build -t shortnr_dev:latest -f ./Dockerfile.dev .") + end end diff --git a/mix.lock b/mix.lock index 87e63d8..06ce477 100644 --- a/mix.lock +++ b/mix.lock @@ -1,14 +1,14 @@ %{ - "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, - "cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"}, - "cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm"}, - "credo": {:hex, :credo, "1.1.5", "caec7a3cadd2e58609d7ee25b3931b129e739e070539ad1a0cd7efeeb47014f4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, - "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"}, - "elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm"}, - "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, - "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"}, - "plug": {:hex, :plug, "1.8.3", "12d5f9796dc72e8ac9614e94bda5e51c4c028d0d428e9297650d09e15a684478", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"}, - "plug_cowboy": {:hex, :plug_cowboy, "2.1.0", "b75768153c3a8a9e8039d4b25bb9b14efbc58e9c4a6e6a270abff1cd30cbe320", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"}, - "plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"}, - "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"}, + "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, + "cowboy": {:hex, :cowboy, "2.7.0", "91ed100138a764355f43316b1d23d7ff6bdb0de4ea618cb5d8677c93a7a2f115", [:rebar3], [{:cowlib, "~> 2.8.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "04fd8c6a39edc6aaa9c26123009200fc61f92a3a94f3178c527b70b767c6e605"}, + "cowlib": {:hex, :cowlib, "2.8.0", "fd0ff1787db84ac415b8211573e9a30a3ebe71b5cbff7f720089972b2319c8a4", [:rebar3], [], "hexpm", "79f954a7021b302186a950a32869dbc185523d99d3e44ce430cd1f3289f41ed4"}, + "credo": {:hex, :credo, "1.1.5", "caec7a3cadd2e58609d7ee25b3931b129e739e070539ad1a0cd7efeeb47014f4", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d0bbd3222607ccaaac5c0340f7f525c627ae4d7aee6c8c8c108922620c5b6446"}, + "dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"}, + "elixir_uuid": {:hex, :elixir_uuid, "1.2.1", "dce506597acb7e6b0daeaff52ff6a9043f5919a4c3315abb4143f0b00378c097", [:mix], [], "hexpm", "f7eba2ea6c3555cea09706492716b0d87397b88946e6380898c2889d68585752"}, + "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"}, + "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"}, + "plug": {:hex, :plug, "1.8.3", "12d5f9796dc72e8ac9614e94bda5e51c4c028d0d428e9297650d09e15a684478", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "164baaeb382d19beee0ec484492aa82a9c8685770aee33b24ec727a0971b34d0"}, + "plug_cowboy": {:hex, :plug_cowboy, "2.1.0", "b75768153c3a8a9e8039d4b25bb9b14efbc58e9c4a6e6a270abff1cd30cbe320", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "6cd8ddd1bd1fbfa54d3fc61d4719c2057dae67615395d58d40437a919a46f132"}, + "plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm", "73c1682f0e414cfb5d9b95c8e8cd6ffcfdae699e3b05e1db744e58b7be857759"}, + "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, }