diff --git a/config/config.exs b/config/config.exs index 08c4b6e..eecbc65 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,30 +1,4 @@ -# This file is responsible for configuring your application -# and its dependencies with the aid of the Mix.Config module. use Mix.Config -# This configuration is loaded before any dependency and is restricted -# to this project. If another project depends on this project, this -# file won't be loaded nor affect the parent project. For this reason, -# if you want to provide default values for your application for -# 3rd-party users, it should be done in your "mix.exs" file. - -# You can configure your application as: -# -# config :elstat, key: :value -# -# and access this configuration in your application as: -# -# Application.get_env(:elstat, :key) -# -# You can also configure a 3rd-party app: -# -# config :logger, level: :info -# - -# It is also possible to import configuration files, relative to this -# directory. For example, you can emulate configuration per environment -# by uncommenting the line below and defining dev.exs, test.exs and such. -# Configuration from the imported file will override the ones defined -# here (which is why it is important to import them last). -# -# import_config "#{Mix.env}.exs" +config :elstat, + port: 8069 diff --git a/lib/cowboy.ex b/lib/cowboy.ex new file mode 100644 index 0000000..cb2f5d4 --- /dev/null +++ b/lib/cowboy.ex @@ -0,0 +1,38 @@ +defmodule Elstat.Cowboy.DefaultHandler do + def init(req0, state) do + + # TODO: find a way to send a html file + # instead of hewwo owo + req = :cowboy_req.reply(200, + %{"content-type" => "text/plain"}, + "hewwo", + req0 + ) + + {:ok, req, state} + end +end + + +defmodule Elstat.Cowboy do + def start_link do + dispatch_config = build_dispatch_config() + + port = Application.fetch_env!(:elstat, :port) + + {:ok, _} = :cowboy.start_clear( + :elstat, + [{:port, port}], + %{env: %{dispatch: dispatch_config}} + ) + end + + def build_dispatch_config do + :cowboy_router.compile([ + {:_, [ + {"/", Elstat.Cowboy.DefaultHandler, []}, + {"/api", Elstat.Cowboy.APIHandler, []}, + ]} + ]) + end +end diff --git a/lib/elstat.ex b/lib/elstat.ex index 84c199d..04acb86 100644 --- a/lib/elstat.ex +++ b/lib/elstat.ex @@ -1,18 +1,15 @@ defmodule Elstat do - @moduledoc """ - Documentation for Elstat. - """ + use Application - @doc """ - Hello world. + require Logger - ## Examples - - iex> Elstat.hello - :world - - """ - def hello do - :world + def start(_type, _args) do + Logger.info "starting app" + Supervisor.start_link( + [ + Elstat.Supervisor + ], + strategy: :one_for_one + ) end end diff --git a/lib/supervisor.ex b/lib/supervisor.ex new file mode 100644 index 0000000..922f850 --- /dev/null +++ b/lib/supervisor.ex @@ -0,0 +1,22 @@ +defmodule Elstat.Supervisor do + use Supervisor + require Logger + + def start_link(opts) do + Supervisor.start_link(__MODULE__, :ok, opts) + end + + def init(:ok) do + Logger.info "starting sup" + + children = [ + %{ + id: Elstat.Cowboy, + start: {Elstat.Cowboy, :start_link, []}, + } + ] + + Supervisor.init(children, strategy: :one_for_one) + end + +end diff --git a/mix.exs b/mix.exs index ddebb14..a537adc 100644 --- a/mix.exs +++ b/mix.exs @@ -14,6 +14,7 @@ defmodule Elstat.MixProject do # Run "mix help compile.app" to learn about applications. def application do [ + mod: {Elstat, []}, extra_applications: [:logger] ] end