add basics of cowboy
This commit is contained in:
parent
e8004c8aa3
commit
4bce1c0146
5 changed files with 73 additions and 41 deletions
|
@ -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
|
||||
|
|
38
lib/cowboy.ex
Normal file
38
lib/cowboy.ex
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
22
lib/supervisor.ex
Normal file
22
lib/supervisor.ex
Normal file
|
@ -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
|
1
mix.exs
1
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
|
||||
|
|
Loading…
Reference in a new issue