diff --git a/elixir/on_plug/.gitignore b/elixir/on_plug/.gitignore new file mode 100644 index 0000000..9607671 --- /dev/null +++ b/elixir/on_plug/.gitignore @@ -0,0 +1,4 @@ +/_build +/deps +erl_crash.dump +*.ez diff --git a/elixir/on_plug/README.md b/elixir/on_plug/README.md new file mode 100644 index 0000000..7697f91 --- /dev/null +++ b/elixir/on_plug/README.md @@ -0,0 +1,8 @@ +Elixir + Plug +====== + +``` +$ mix do deps.get, compile +$ MIX_ENV=prod iex -S mix +$ wrk -c 64 -d 30s http://localhost:4000/10 +``` \ No newline at end of file diff --git a/elixir/on_plug/config/config.exs b/elixir/on_plug/config/config.exs new file mode 100644 index 0000000..6dfa82f --- /dev/null +++ b/elixir/on_plug/config/config.exs @@ -0,0 +1,24 @@ +# 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 third- +# party users, it should be done in your mix.exs file. + +# Sample configuration: +# +# config :logger, :console, +# level: :info, +# format: "$date $time [$level] $metadata$message\n", +# metadata: [:user_id] + +# 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" diff --git a/elixir/on_plug/lib/app_router.ex b/elixir/on_plug/lib/app_router.ex new file mode 100644 index 0000000..2d2264e --- /dev/null +++ b/elixir/on_plug/lib/app_router.ex @@ -0,0 +1,12 @@ +defmodule AppRouter do + use Plug.Router + + plug :match + plug :dispatch + + + get "/:n" do + n = String.to_integer(n) + conn |> send_resp 200, to_string(Fibonacci.get(n)) + end +end \ No newline at end of file diff --git a/elixir/on_plug/lib/fibonacci.ex b/elixir/on_plug/lib/fibonacci.ex new file mode 100644 index 0000000..e45a316 --- /dev/null +++ b/elixir/on_plug/lib/fibonacci.ex @@ -0,0 +1,14 @@ +defmodule Fibonacci do + def get(0) do + 0 + end + + def get(1) do + 1 + end + + def get(n) when n > 0 do + get(n-1) + get(n-2) + end + +end \ No newline at end of file diff --git a/elixir/on_plug/lib/on_plug.ex b/elixir/on_plug/lib/on_plug.ex new file mode 100644 index 0000000..0994e79 --- /dev/null +++ b/elixir/on_plug/lib/on_plug.ex @@ -0,0 +1,7 @@ +defmodule OnPlug do + use Application + + def start(_type, _args) do + {:ok, _} = Plug.Adapters.Cowboy.http AppRouter, [] + end +end diff --git a/elixir/on_plug/mix.exs b/elixir/on_plug/mix.exs new file mode 100644 index 0000000..17b24aa --- /dev/null +++ b/elixir/on_plug/mix.exs @@ -0,0 +1,38 @@ +defmodule OnPlug.Mixfile do + use Mix.Project + + def project do + [app: :on_plug, + version: "0.0.1", + elixir: "~> 1.0", + build_embedded: Mix.env == :prod, + start_permanent: Mix.env == :prod, + deps: deps] + end + + # Configuration for the OTP application + # + # Type `mix help compile.app` for more information + def application do + [ + mod: {OnPlug, []}, + applications: [:cowboy, :plug] + ] + end + + # Dependencies can be Hex packages: + # + # {:mydep, "~> 0.3.0"} + # + # Or git/path repositories: + # + # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} + # + # Type `mix help deps` for more examples and options + defp deps do + [ + {:cowboy, "~> 1.0.0"}, + {:plug, "~> 0.12"} + ] + end +end diff --git a/elixir/on_plug/mix.lock b/elixir/on_plug/mix.lock new file mode 100644 index 0000000..7505532 --- /dev/null +++ b/elixir/on_plug/mix.lock @@ -0,0 +1,4 @@ +%{"cowboy": {:hex, :cowboy, "1.0.0"}, + "cowlib": {:hex, :cowlib, "1.0.1"}, + "plug": {:hex, :plug, "0.13.0"}, + "ranch": {:hex, :ranch, "1.0.0"}} diff --git a/elixir/on_plug/test/on_plug_test.exs b/elixir/on_plug/test/on_plug_test.exs new file mode 100644 index 0000000..46633da --- /dev/null +++ b/elixir/on_plug/test/on_plug_test.exs @@ -0,0 +1,7 @@ +defmodule OnPlugTest do + use ExUnit.Case + + test "the truth" do + assert 1 + 1 == 2 + end +end diff --git a/elixir/on_plug/test/test_helper.exs b/elixir/on_plug/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/elixir/on_plug/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()