Browse Source

Elixir Cowboy 2 (#4330)

* Updating to latest version of Elixir, Cowboy, Ranch, Poision

* Adding proper release

* 127.0.0.1->0.0.0.0
Istvan 6 years ago
parent
commit
f6163a3058

+ 2 - 0
frameworks/Elixir/cowboy/.gitignore

@@ -0,0 +1,2 @@
+deps/
+_build/

+ 13 - 0
frameworks/Elixir/cowboy/config/config.exs

@@ -1 +1,14 @@
 use Mix.Config
+
+config :hello, http_ip: {0, 0, 0, 0}
+config :hello, http_port: 8080
+config :hello, compress_body: true
+config :hello, num_acceptors: 128
+config :hello, max_connections: 32000
+config :hello, max_keepalive: 1024
+config :logger,
+  level: :info,
+  compile_time_purge_matching: [
+    [level_lower_than: :info]
+  ]
+

+ 1 - 1
frameworks/Elixir/cowboy/elixir-cowboy.dockerfile

@@ -1,4 +1,4 @@
-FROM elixir:1.5.3
+FROM elixir:1.7.4
 
 ADD ./ /cowboy
 WORKDIR /cowboy

+ 0 - 46
frameworks/Elixir/cowboy/lib/hello.ex

@@ -1,48 +1,2 @@
 defmodule Hello do
-  def start(_type, _args) do
-    dispatch = :cowboy_router.compile([
-      {:_, [{"/json", JsonHandler, []},
-            {"/plaintext", PlaintextHandler, []}]}
-    ])
-    {:ok, _} = :cowboy.start_http(:http,
-                                  5000,
-                                  [port: 8080],
-                                  [env: [dispatch: dispatch], max_keepalive: :infinity])
-  end
-end
-
-defmodule JsonHandler do
-  def init(_type, req, []) do
-    {:ok, req, :no_state}
-  end
-
-  def handle(request, state) do
-    {:ok, reply} = :cowboy_req.reply(200,
-                                     [{"content-type", "application/json"}],
-                                     Poison.encode!(%{message: "Hello, World!"}),
-                                     request)
-    {:ok, reply, state}
-  end
-
-  def terminate(_reason, _request, _state) do
-    :ok
-  end
-end
-
-defmodule PlaintextHandler do
-  def init(_type, req, []) do
-    {:ok, req, :no_state}
-  end
-
-  def handle(request, state) do
-    {:ok, reply} = :cowboy_req.reply(200,
-                                     [{"content-type", "text/plain"}],
-                                     "Hello, World!",
-                                     request)
-    {:ok, reply, state}
-  end
-
-  def terminate(_reason, _request, _state) do
-    :ok
-  end
 end

+ 39 - 0
frameworks/Elixir/cowboy/lib/hello/application.ex

@@ -0,0 +1,39 @@
+defmodule Hello.Application do
+  @moduledoc false
+
+  use Application
+  require Logger
+
+  def start(_type, _args) do
+    _log_level = Application.get_env(:logger, :level)
+    http_ip = Application.get_env(:hello, :http_ip)
+    http_port = Application.get_env(:hello, :http_port)
+    compress_body = Application.get_env(:hello, :compress_body)
+    num_acceptors = Application.get_env(:hello, :num_acceptors)
+    max_connections = Application.get_env(:hello, :max_connections)
+    max_keepalive = Application.get_env(:hello, :max_keepalive)
+
+    children = [
+      {Plug.Cowboy,
+       scheme: :http,
+       plug: Hello.Router,
+       options: [
+         ip: http_ip,
+         port: http_port,
+         compress: compress_body,
+         transport_options: [
+           num_acceptors: num_acceptors,
+           max_connections: max_connections
+         ],
+         protocol_options: [
+           max_keepalive: max_keepalive
+         ]
+       ]}
+    ]
+
+    opts = [strategy: :one_for_one, name: Hello.Supervisor]
+    ip_str = http_ip |> Tuple.to_list() |> Enum.join(".")
+    Logger.info("Starting up on #{ip_str}:#{http_port}")
+    Supervisor.start_link(children, opts)
+  end
+end

+ 26 - 0
frameworks/Elixir/cowboy/lib/hello/router.ex

@@ -0,0 +1,26 @@
+defmodule Hello.Router do
+  use Plug.Router
+
+  plug(:match)
+  plug(:dispatch)
+
+  get "/" do
+    conn
+    |> put_resp_content_type("text/html")
+    |> send_resp(200, "<h2>/</h2>")
+  end
+
+  get "/json" do
+    conn
+    |> put_resp_content_type("application/json")
+    |> send_resp(200, Poison.encode!(%{message: "Hello, World!"}))
+  end
+
+  get "/plaintext" do
+    conn
+    |> put_resp_content_type("text/plain")
+    |> send_resp(200, "Hello, World!")
+  end
+
+  match(_, do: send_resp(conn, 404, "Oops!"))
+end

+ 19 - 10
frameworks/Elixir/cowboy/mix.exs

@@ -1,21 +1,30 @@
-defmodule Hello.Mixfile do
+defmodule Hello.MixProject do
   use Mix.Project
 
   def project do
-    [app: :hello,
-     version: "0.1.0",
-     elixir: "~> 1.5",
-     deps: deps()]
+    [
+      app: :hello,
+      version: "0.1.0",
+      elixir: "~> 1.7",
+      start_permanent: Mix.env() == :prod,
+      deps: deps()
+    ]
   end
 
   def application do
-    [mod: {Hello, []},
-     applications: [:cowboy, :poison, :ranch]]
+    [
+      extra_applications: [
+        :logger
+      ],
+      mod: {Hello.Application, []}
+    ]
   end
 
   defp deps do
-    [{:cowboy, "~> 1.0"},
-     {:poison, "~> 1.4.0"},
-     {:ranch, "~> 1.3.2"}]
+    [
+      {:plug_cowboy, "~> 2.0.1"},
+      {:poison, "~> 3.1"},
+      {:distillery, "~> 2.0"}
+    ]
   end
 end

+ 55 - 0
frameworks/Elixir/cowboy/rel/config.exs

@@ -0,0 +1,55 @@
+# Import all plugins from `rel/plugins`
+# They can then be used by adding `plugin MyPlugin` to
+# either an environment, or release definition, where
+# `MyPlugin` is the name of the plugin module.
+~w(rel plugins *.exs)
+|> Path.join()
+|> Path.wildcard()
+|> Enum.map(&Code.eval_file(&1))
+
+use Mix.Releases.Config,
+    # This sets the default release built by `mix release`
+    default_release: :default,
+    # This sets the default environment used by `mix release`
+    default_environment: Mix.env()
+
+# For a full list of config options for both releases
+# and environments, visit https://hexdocs.pm/distillery/config/distillery.html
+
+
+# You may define one or more environments in this file,
+# an environment's settings will override those of a release
+# when building in that environment, this combination of release
+# and environment configuration is called a profile
+
+environment :dev do
+  # If you are running Phoenix, you should make sure that
+  # server: true is set and the code reloader is disabled,
+  # even in dev mode.
+  # It is recommended that you build with MIX_ENV=prod and pass
+  # the --env flag to Distillery explicitly if you want to use
+  # dev mode.
+  set dev_mode: true
+  set include_erts: false
+  set cookie: :"oHiKgSD18Or5NI|AQIwET6;ni=}i]aq:>_2V/|)4A:*73@(&sB>q^CXj*,n/7hcm"
+end
+
+environment :prod do
+  set include_erts: true
+  set include_src: false
+  set cookie: :"U0zRt1a7BRIi5(<Dg@jQ;:o>Vr=Ezcr``J&SO`v:TPrQMo/PWV;<^IVzU^Y~(Jvy"
+  set vm_args: "rel/vm.args"
+end
+
+# You may define one or more releases in this file.
+# If you have not set a default release, or selected one
+# when running `mix release`, the first release in the file
+# will be used by default
+
+release :hello do
+  set version: current_version(:hello)
+  set applications: [
+    :runtime_tools
+  ]
+end
+

+ 3 - 0
frameworks/Elixir/cowboy/rel/plugins/.gitignore

@@ -0,0 +1,3 @@
+*.*
+!*.exs
+!.gitignore

+ 30 - 0
frameworks/Elixir/cowboy/rel/vm.args

@@ -0,0 +1,30 @@
+## This file provide the arguments provided to the VM at startup
+## You can find a full list of flags and their behaviours at
+## http://erlang.org/doc/man/erl.html
+
+## Name of the node
+-name <%= release_name %>@127.0.0.1
+
+## Cookie for distributed erlang
+-setcookie <%= release.profile.cookie %>
+
+## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive
+## (Disabled by default..use with caution!)
+##-heart
+
+## Enable kernel poll and a few async threads
++K true
+##+A 5
+## For OTP21+, the +A flag is not used anymore,
+## +SDio replace it to use dirty schedulers
++SDio 5
+
+## Increase number of concurrent ports/sockets
+##-env ERL_MAX_PORTS 4096
+
+## Tweak GC to run more often
+##-env ERL_FULLSWEEP_AFTER 10
+
+# Enable SMP automatically based on availability
+# On OTP21+, this is not needed anymore.
+-smp auto

+ 3 - 1
frameworks/Elixir/cowboy/setup.sh

@@ -9,5 +9,7 @@ mix local.hex --force
 mix local.rebar --force
 mix deps.get --force --only prod
 mix compile --force
+mix release
+_build/prod/rel/hello/bin/hello foreground
 
-elixir --erl "+K true +sbwt very_long +swt very_low" --detached --no-halt -S mix
+#elixir --erl "+K true +sbwt very_long +swt very_low" --detached --no-halt -S mix