shortnr/lib/shortnr.ex

41 lines
971 B
Elixir
Raw Normal View History

2019-12-08 02:34:58 +00:00
defmodule Shortnr do
@moduledoc """
The Shortnr application entry point. Check README for usage documenation.
2019-12-08 02:34:58 +00:00
"""
require Logger
use Application
2019-12-08 02:34:58 +00:00
@impl Application
2019-12-08 02:34:58 +00:00
def start(_type, _args) do
children = [
{Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]}
2019-12-08 02:34:58 +00:00
]
if ets_implementation() == :dets do
{:ok, _} = :dets.open_file(:urls, type: :set)
else
:ets.new(:urls, [:set, :named_table])
end
2019-12-08 18:44:04 +00:00
Logger.info("server starting", port: port())
2019-12-08 02:34:58 +00:00
Supervisor.start_link(children, strategy: :one_for_one)
end
@impl Application
def stop(_state) do
if ets_implementation() == :dets, do: :dets.close(:urls)
end
2019-12-08 02:34:58 +00:00
@spec port() :: integer()
defp port do
case Application.fetch_env(:shortnr, :port) do
2019-12-08 02:34:58 +00:00
{:ok, port} -> port
_ -> 4000
end
end
@spec ets_implementation() :: atom()
defp ets_implementation, do: Application.fetch_env!(:shortnr, :ets_implementation)
2019-12-08 02:34:58 +00:00
end