2019-12-08 02:34:58 +00:00
|
|
|
defmodule Shortnr do
|
|
|
|
@moduledoc """
|
2019-12-31 02:50:39 +00:00
|
|
|
The Shortnr application entry point. Check README for usage documenation.
|
2019-12-08 02:34:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
require Logger
|
2020-01-05 19:54:55 +00:00
|
|
|
use Application
|
2019-12-08 02:34:58 +00:00
|
|
|
|
2020-01-05 19:54:55 +00:00
|
|
|
@impl Application
|
2019-12-08 02:34:58 +00:00
|
|
|
def start(_type, _args) do
|
|
|
|
children = [
|
2020-01-05 19:54:55 +00:00
|
|
|
{Plug.Cowboy, scheme: :http, plug: Shortnr.Router, options: [port: port()]}
|
2019-12-08 02:34:58 +00:00
|
|
|
]
|
|
|
|
|
2019-12-26 21:11:43 +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
|
|
|
|
2020-01-05 19:54:55 +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
|
|
|
|
|
2020-01-05 19:54:55 +00:00
|
|
|
@impl Application
|
2019-12-26 21:11:43 +00:00
|
|
|
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
|
2019-12-26 21:11:43 +00:00
|
|
|
case Application.fetch_env(:shortnr, :port) do
|
2019-12-08 02:34:58 +00:00
|
|
|
{:ok, port} -> port
|
|
|
|
_ -> 4000
|
|
|
|
end
|
|
|
|
end
|
2019-12-26 21:11:43 +00:00
|
|
|
|
|
|
|
@spec ets_implementation() :: atom()
|
|
|
|
defp ets_implementation, do: Application.fetch_env!(:shortnr, :ets_implementation)
|
2019-12-08 02:34:58 +00:00
|
|
|
end
|