elstat/lib/cowboy.ex

42 lines
995 B
Elixir

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([
{:_, [
{"/hewwo", Elstat.Cowboy.DefaultHandler, []},
{"/api/current_status", Elstat.API.CurrentStatus, []},
{"/api/status", Elstat.API.Status, []},
{"/", :cowboy_static, {:priv_file, :elstat, "frontend/build/index.html"}},
{"/[...]", :cowboy_static, {:priv_dir, :elstat, "frontend/build"}},
]}
])
end
end