├── .gitignore ├── README.md ├── apps ├── chunked_hello_world │ ├── README.md │ ├── lib │ │ ├── chunked_hello_world.ex │ │ └── chunked_hello_world │ │ │ ├── release.ex │ │ │ ├── supervisor.ex │ │ │ └── top_page_handler.ex │ ├── mix.exs │ └── test │ │ ├── chunked_hello_world_test.exs │ │ └── test_helper.exs ├── compress_response │ ├── README.md │ ├── lib │ │ ├── compress_response.ex │ │ └── compress_response │ │ │ ├── supervisor.ex │ │ │ └── top_page_handler.ex │ ├── mix.exs │ └── test │ │ ├── compress_response_test.exs │ │ └── test_helper.exs ├── cookie │ ├── README.md │ ├── lib │ │ ├── cookie.ex │ │ └── cookie │ │ │ ├── supervisor.ex │ │ │ └── top_page_handler.ex │ ├── mix.exs │ ├── templates │ │ └── toppage.dtl │ └── test │ │ ├── cookie_test.exs │ │ └── test_helper.exs ├── echo_get │ ├── .gitignore │ ├── README.md │ ├── lib │ │ ├── echo_get.ex │ │ └── echo_get │ │ │ ├── supervisor.ex │ │ │ └── top_page_handler.ex │ ├── mix.exs │ └── test │ │ ├── echo_get_test.exs │ │ └── test_helper.exs ├── echo_post │ ├── README.md │ ├── lib │ │ ├── echo_post.ex │ │ └── echo_post │ │ │ └── supervisor.ex │ ├── mix.exs │ └── test │ │ ├── echo_post_test.exs │ │ └── test_helper.exs ├── error_hook │ ├── README.md │ ├── lib │ │ ├── error_hook.ex │ │ └── error_hook │ │ │ └── supervisor.ex │ ├── mix.exs │ └── test │ │ ├── error_hook_test.exs │ │ └── test_helper.exs └── hello_world │ ├── README.md │ ├── lib │ ├── hello_world.ex │ └── hello_world │ │ ├── supervisor.ex │ │ └── top_page_handler.ex │ ├── mix.exs │ └── test │ ├── hello_world_test.exs │ └── test_helper.exs └── mix.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | *.ez 5 | mix.lock 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ElixirCowboyExamples 2 | 3 | These examples are the [Elixir](http://elixir-lang.org/) analogs to those found 4 | in the [cowboy](https://github.com/extend/cowboy) 5 | [examples](https://github.com/extend/cowboy/tree/master/examples) directory. 6 | You'll find them in the apps directory. 7 | 8 | This is still a work in progress. Some examples may be non-functional or 9 | incomplete as I make my way through the list. If you are reading this, assume 10 | that this is still the case. 11 | -------------------------------------------------------------------------------- /apps/chunked_hello_world/README.md: -------------------------------------------------------------------------------- 1 | # ChunkedHelloWorld 2 | 3 | ** TODO: Add description ** 4 | -------------------------------------------------------------------------------- /apps/chunked_hello_world/lib/chunked_hello_world.ex: -------------------------------------------------------------------------------- 1 | defmodule ChunkedHelloWorld do 2 | use Application.Behaviour 3 | 4 | def start(_type, _args) do 5 | dispatch = :cowboy_router.compile([ 6 | {:_, [{"/", ChunkedHelloWorld.TopPageHandler, []}]} 7 | ]) 8 | 9 | {:ok, _} = :cowboy.start_http(:http, 100, 10 | [port: 8080], 11 | [env: [dispatch: dispatch]]) 12 | ChunkedHelloWorld.Supervisor.start_link 13 | end 14 | end 15 | 16 | -------------------------------------------------------------------------------- /apps/chunked_hello_world/lib/chunked_hello_world/release.ex: -------------------------------------------------------------------------------- 1 | defmodule ChunkedHellowWorld.Release do 2 | use Relex.Release 3 | 4 | def name, do: "chunked_hello_world" 5 | def version, do: "1" 6 | 7 | def applications, do: [:chunked_hello_world] 8 | end -------------------------------------------------------------------------------- /apps/chunked_hello_world/lib/chunked_hello_world/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule ChunkedHelloWorld.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [ 10 | # Define workers and child supervisors to be supervised 11 | # worker(ChunkedHelloWorld.Worker, []) 12 | ] 13 | 14 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 15 | # for other strategies and supported options 16 | supervise(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /apps/chunked_hello_world/lib/chunked_hello_world/top_page_handler.ex: -------------------------------------------------------------------------------- 1 | defmodule ChunkedHelloWorld.TopPageHandler do 2 | def init(_transport, req, []) do 3 | {:ok, req, nil} 4 | end 5 | 6 | def handle(req, state) do 7 | {:ok, req} = :cowboy_req.chunked_reply(200, req) 8 | :ok = :cowboy_req.chunk("Hello\r\n", req) 9 | :ok = :timer.sleep(1000) 10 | :ok = :cowboy_req.chunk("World\r\n", req) 11 | :ok = :timer.sleep(1000) 12 | :ok = :cowboy_req.chunk("Chunked!\r\n", req) 13 | {:ok, req, state} 14 | end 15 | 16 | def terminate(_reason, _req, _state), do: :ok 17 | end -------------------------------------------------------------------------------- /apps/chunked_hello_world/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ChunkedHelloWorld.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :chunked_hello_world, 6 | version: "0.0.1", 7 | elixir: "~> 0.10.3", 8 | deps: deps ] 9 | end 10 | 11 | # Configuration for the OTP application 12 | def application do 13 | [mod: { ChunkedHelloWorld, [] }, 14 | applications: [:cowboy] ] 15 | end 16 | 17 | # Returns the list of dependencies in the format: 18 | # { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" } 19 | defp deps do 20 | [{:cowboy, github: "extend/cowboy"}] 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /apps/chunked_hello_world/test/chunked_hello_world_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ChunkedHelloWorldTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert(true) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /apps/chunked_hello_world/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /apps/compress_response/README.md: -------------------------------------------------------------------------------- 1 | # CompressResponse 2 | 3 | ** TODO: Add description ** 4 | -------------------------------------------------------------------------------- /apps/compress_response/lib/compress_response.ex: -------------------------------------------------------------------------------- 1 | defmodule CompressResponse do 2 | use Application.Behaviour 3 | 4 | # See http://elixir-lang.org/docs/stable/Application.Behaviour.html 5 | # for more information on OTP Applications 6 | def start(_type, _args) do 7 | dispatch = :cowboy_router.compile([ 8 | {:_, [{"/", CompressResponse.TopPageHandler, []}]} 9 | ]) 10 | 11 | {:ok, _} = :cowboy.start_http(:http, 100, 12 | [port: 8080], 13 | [env: [dispatch: dispatch]]) 14 | 15 | CompressResponse.Supervisor.start_link 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /apps/compress_response/lib/compress_response/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule CompressResponse.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [ 10 | # Define workers and child supervisors to be supervised 11 | # worker(CompressResponse.Worker, []) 12 | ] 13 | 14 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 15 | # for other strategies and supported options 16 | supervise(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /apps/compress_response/lib/compress_response/top_page_handler.ex: -------------------------------------------------------------------------------- 1 | defmodule CompressResponse.TopPageHandler do 2 | def init(_transport, req, []) do 3 | {:ok, req, nil} 4 | end 5 | 6 | def handle(req, state) do 7 | big_body = """ 8 | A cowboy is an animal herder who tends cattle on ranches in North America, 9 | traditionally on horseback, and often performs a multitude of other ranch- 10 | related tasks. The historic American cowboy of the late 19th century arose 11 | from the vaquero traditions of northern Mexico and became a figure of special 12 | significance and legend. A subtype, called a wrangler, specifically tends the 13 | horses used to work cattle. In addition to ranch work, some cowboys work for 14 | or participate in rodeos. Cowgirls, first defined as such in the late 19th 15 | century, had a less-well documented historical role, but in the modern world 16 | have established the ability to work at virtually identical tasks and obtained 17 | considerable respect for their achievements. There are also cattle handlers 18 | in many other parts of the world, particularly South America and Australia, 19 | who perform work similar to the cowboy in their respective nations.\n 20 | """ 21 | {:ok, req} = :cowboy_req.reply(200, [], big_body, req) 22 | {:ok, req, state} 23 | end 24 | 25 | def terminate(_reason, _req, _state), do: :ok 26 | end -------------------------------------------------------------------------------- /apps/compress_response/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule CompressResponse.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :compress_response, 6 | version: "0.0.1", 7 | elixir: "~> 0.10.3", 8 | deps: deps ] 9 | end 10 | 11 | # Configuration for the OTP application 12 | def application do 13 | [mod: { CompressResponse, [] }, 14 | applications: [:cowboy] ] 15 | end 16 | 17 | # Returns the list of dependencies in the format: 18 | # { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" } 19 | defp deps do 20 | [{:cowboy, github: "extend/cowboy"}] 21 | end 22 | 23 | end 24 | -------------------------------------------------------------------------------- /apps/compress_response/test/compress_response_test.exs: -------------------------------------------------------------------------------- 1 | defmodule CompressResponseTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert(true) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /apps/compress_response/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /apps/cookie/README.md: -------------------------------------------------------------------------------- 1 | # Cookie 2 | 3 | ** TODO: Add description ** 4 | -------------------------------------------------------------------------------- /apps/cookie/lib/cookie.ex: -------------------------------------------------------------------------------- 1 | defmodule Cookie do 2 | use Application.Behaviour 3 | 4 | # See http://elixir-lang.org/docs/stable/Application.Behaviour.html 5 | # for more information on OTP Applications 6 | def start(_type, _args) do 7 | dispatch = :cowboy_router.compile([ 8 | {:_, [{"/", Cookie.TopPageHandler, []}]} 9 | ]) 10 | 11 | {:ok, _} = :cowboy.start_http(:http, 100, 12 | [port: 8080], 13 | [env: [dispatch: dispatch]]) 14 | Cookie.Supervisor.start_link 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /apps/cookie/lib/cookie/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule Cookie.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [ 10 | # Define workers and child supervisors to be supervised 11 | # worker(Cookie.Worker, []) 12 | ] 13 | 14 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 15 | # for other strategies and supported options 16 | supervise(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /apps/cookie/lib/cookie/top_page_handler.ex: -------------------------------------------------------------------------------- 1 | defmodule Cookie.TopPageHandler do 2 | def init(_transport, req, []) do 3 | {:ok, req, nil} 4 | end 5 | 6 | def handle(req, state) do 7 | new_value = integer_to_list(:random.uniform(1000000)) 8 | req = :cowboy_req.set_resp_cookie("server", new_value, [path: "/"], req) 9 | {client_cookie, req} = :cowboy_req.cookie("client", req) 10 | {server_cookie, req} = :cowboy_req.cookie("server", req) 11 | body = :cookie_template.toppage([client: client_cookie, server: server_cookie]) 12 | {:ok, req} = :cowboy_req.reply(200, [{"content-type", "text/html"}], body, req) 13 | {:ok, req, state} 14 | end 15 | 16 | def terminate(_reason, _req, _state), do: :ok 17 | end 18 | -------------------------------------------------------------------------------- /apps/cookie/mix.exs: -------------------------------------------------------------------------------- 1 | Code.append_path "deps/relex/ebin" 2 | Code.append_path "deps/pogo/ebin" 3 | defmodule Cookie.Mixfile do 4 | use Mix.Project 5 | 6 | def project do 7 | [ app: :cookie, 8 | version: "0.0.1", 9 | elixir: "~> 0.10.3", 10 | deps: deps ] 11 | end 12 | 13 | # Configuration for the OTP application 14 | def application do 15 | [mod: { Cookie, [] }, 16 | applications: [:cowboy] ] 17 | end 18 | 19 | # Returns the list of dependencies in the format: 20 | # { :foobar, "~> 0.1", github: "https://github.com/elixir-lang/foobar.git" } 21 | defp deps do 22 | [{:cowboy, github: "extend/cowboy"}, 23 | {:exlydtl, github: "joshrotenberg/exlydtl"}, 24 | {:erlydtl, github: "evanmiller/erlydtl", override: true}, 25 | {:relex, github: "interline/relex"}, 26 | {:pogo, github: "onkel-dirtus/pogo"}] 27 | end 28 | 29 | if Enum.all?([Relex.Release, Pogo.Release], &Code.ensure_loaded?/1) do 30 | defmodule Release do 31 | use Relex.Release 32 | use Pogo.Release 33 | 34 | def name, do: "cookie" 35 | def version, do: Mix.project[:version] 36 | def applications, do: [:pogo, Mix.project[:app]] 37 | #def lib_dirs, do: ["deps"] 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /apps/cookie/templates/toppage.dtl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Cowboy Cookie Example 6 | 7 | 8 | 9 |

Cowboy Cookie Example

10 |

Refresh the page to see the next cookie.

11 | 12 |

Cookie Set Server-Side

13 |

{{ server }}

14 | 15 |

Cookie Set Client-Side

16 |

{{ client }}

17 | 18 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /apps/cookie/test/cookie_test.exs: -------------------------------------------------------------------------------- 1 | defmodule CookieTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert(true) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /apps/cookie/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /apps/echo_get/.gitignore: -------------------------------------------------------------------------------- 1 | /ebin 2 | /deps 3 | erl_crash.dump 4 | *.ez 5 | -------------------------------------------------------------------------------- /apps/echo_get/README.md: -------------------------------------------------------------------------------- 1 | # EchoGet 2 | 3 | ** TODO: Add description ** 4 | -------------------------------------------------------------------------------- /apps/echo_get/lib/echo_get.ex: -------------------------------------------------------------------------------- 1 | defmodule EchoGet do 2 | use Application.Behaviour 3 | 4 | # See http://elixir-lang.org/docs/stable/Application.Behaviour.html 5 | # for more information on OTP Applications 6 | def start(_type, _args) do 7 | dispatch = :cowboy_router.compile([ 8 | {:_, [{"/", EchoGet.TopPageHandler, []}]} 9 | ]) 10 | {:ok, _} = :cowboy.start_http(:http, 100, 11 | [port: 8080], 12 | [env: [dispatch: dispatch]]) 13 | EchoGet.Supervisor.start_link 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /apps/echo_get/lib/echo_get/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule EchoGet.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [ 10 | # Define workers and child supervisors to be supervised 11 | # worker(EchoGet.Worker, []) 12 | ] 13 | 14 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 15 | # for other strategies and supported options 16 | supervise(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /apps/echo_get/lib/echo_get/top_page_handler.ex: -------------------------------------------------------------------------------- 1 | defmodule EchoGet.TopPageHandler do 2 | def init(_transport, req, []) do 3 | {:ok, req, nil} 4 | end 5 | 6 | def handle(req, state) do 7 | {method, req} = :cowboy_req.method(req) 8 | {param, req} = :cowboy_req.qs_val("echo", req) 9 | {:ok, req} = echo(method, param, req) 10 | {:ok, req, state} 11 | end 12 | 13 | def echo("GET", :undefined, req) do 14 | :cowboy_req.reply(400, [], "Missing echo parameter.", req) 15 | end 16 | 17 | def echo("GET", param, req) do 18 | :cowboy_req.reply(200, [{"content-type", "text/plain; charset=utf-8"}], param, req) 19 | end 20 | 21 | def echo(_, _, req) do 22 | :cowboy_req.reply(405, req) 23 | end 24 | 25 | def terminate(_reason, _req, _state), do: :ok 26 | end 27 | -------------------------------------------------------------------------------- /apps/echo_get/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule EchoGet.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :echo_get, 6 | version: "0.0.1", 7 | elixir: "~> 0.10.3", 8 | deps: deps ] 9 | end 10 | 11 | # Configuration for the OTP application 12 | def application do 13 | [ mod: { EchoGet, [] }, 14 | applications: [:cowboy] ] 15 | end 16 | 17 | # Returns the list of dependencies in the format: 18 | # { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" } 19 | defp deps do 20 | [ {:cowboy, github: "extend/cowboy"} ] 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /apps/echo_get/test/echo_get_test.exs: -------------------------------------------------------------------------------- 1 | defmodule EchoGetTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert(true) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /apps/echo_get/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /apps/echo_post/README.md: -------------------------------------------------------------------------------- 1 | # EchoPost 2 | 3 | ** TODO: Add description ** 4 | -------------------------------------------------------------------------------- /apps/echo_post/lib/echo_post.ex: -------------------------------------------------------------------------------- 1 | defmodule EchoPost do 2 | use Application.Behaviour 3 | 4 | # See http://elixir-lang.org/docs/stable/Application.Behaviour.html 5 | # for more information on OTP Applications 6 | def start(_type, _args) do 7 | EchoPost.Supervisor.start_link 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /apps/echo_post/lib/echo_post/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule EchoPost.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [ 10 | # Define workers and child supervisors to be supervised 11 | # worker(EchoPost.Worker, []) 12 | ] 13 | 14 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 15 | # for other strategies and supported options 16 | supervise(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /apps/echo_post/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule EchoPost.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :echo_post, 6 | version: "0.0.1", 7 | elixir: "~> 0.10.3", 8 | deps: deps ] 9 | end 10 | 11 | # Configuration for the OTP application 12 | def application do 13 | [mod: { EchoPost, [] }] 14 | end 15 | 16 | # Returns the list of dependencies in the format: 17 | # { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" } 18 | defp deps do 19 | [] 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /apps/echo_post/test/echo_post_test.exs: -------------------------------------------------------------------------------- 1 | defmodule EchoPostTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert(true) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /apps/echo_post/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /apps/error_hook/README.md: -------------------------------------------------------------------------------- 1 | # ErrorHook 2 | 3 | ** TODO: Add description ** 4 | -------------------------------------------------------------------------------- /apps/error_hook/lib/error_hook.ex: -------------------------------------------------------------------------------- 1 | defmodule ErrorHook do 2 | use Application.Behaviour 3 | 4 | # See http://elixir-lang.org/docs/stable/Application.Behaviour.html 5 | # for more information on OTP Applications 6 | def start(_type, _args) do 7 | ErrorHook.Supervisor.start_link 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /apps/error_hook/lib/error_hook/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule ErrorHook.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [ 10 | # Define workers and child supervisors to be supervised 11 | # worker(ErrorHook.Worker, []) 12 | ] 13 | 14 | # See http://elixir-lang.org/docs/stable/Supervisor.Behaviour.html 15 | # for other strategies and supported options 16 | supervise(children, strategy: :one_for_one) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /apps/error_hook/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ErrorHook.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :error_hook, 6 | version: "0.0.1", 7 | elixir: "~> 0.10.3", 8 | deps: deps ] 9 | end 10 | 11 | # Configuration for the OTP application 12 | def application do 13 | [mod: { ErrorHook, [] }] 14 | end 15 | 16 | # Returns the list of dependencies in the format: 17 | # { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" } 18 | defp deps do 19 | [] 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /apps/error_hook/test/error_hook_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ErrorHookTest do 2 | use ExUnit.Case 3 | 4 | test "the truth" do 5 | assert(true) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /apps/error_hook/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /apps/hello_world/README.md: -------------------------------------------------------------------------------- 1 | Elixir Hello World 2 | ================== 3 | 4 | This is an example of running Cowboy with [Elixir](http://elixir-lang.org). 5 | 6 | You need Elixir installed 7 | ([instructions here](http://elixir-lang.org/getting_started/1.html)) 8 | to run this example. After installing Elixir, you should have both 9 | `elixir` and `mix` executables available. 10 | 11 | You also need [rebar](https://github.com/rebar/rebar) in your PATH 12 | to compile dependencies. 13 | 14 | Then type the following command: 15 | 16 | ``` 17 | mix deps.get 18 | ``` 19 | 20 | The command above will fetch all dependencies and compile them. 21 | 22 | You can then start the Erlang node with the following command: 23 | 24 | ``` 25 | mix run --no-halt 26 | ``` 27 | 28 | Then point your browser to localhost:8080. 29 | 30 | Example 31 | ------- 32 | 33 | ``` bash 34 | $ curl -i http://localhost:8080 35 | HTTP/1.1 200 OK 36 | connection: keep-alive 37 | server: Cowboy 38 | date: Fri, 28 Sep 2012 04:10:25 GMT 39 | content-length: 12 40 | 41 | Hello world! 42 | ``` 43 | -------------------------------------------------------------------------------- /apps/hello_world/lib/hello_world.ex: -------------------------------------------------------------------------------- 1 | defmodule HelloWorld do 2 | use Application.Behaviour 3 | 4 | def start(_type, _args) do 5 | dispatch = :cowboy_router.compile([ 6 | {:_, [{"/", HelloWorld.TopPageHandler, []}]} 7 | ]) 8 | {:ok, _} = :cowboy.start_http(:http, 100, 9 | [port: 8080], 10 | [env: [dispatch: dispatch]]) 11 | HelloWorld.Supervisor.start_link 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /apps/hello_world/lib/hello_world/supervisor.ex: -------------------------------------------------------------------------------- 1 | defmodule HelloWorld.Supervisor do 2 | use Supervisor.Behaviour 3 | 4 | def start_link do 5 | :supervisor.start_link(__MODULE__, []) 6 | end 7 | 8 | def init([]) do 9 | children = [] 10 | supervise children, strategy: :one_for_one 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /apps/hello_world/lib/hello_world/top_page_handler.ex: -------------------------------------------------------------------------------- 1 | defmodule HelloWorld.TopPageHandler do 2 | def init(_transport, req, []) do 3 | {:ok, req, nil} 4 | end 5 | 6 | def handle(req, state) do 7 | {:ok, req} = :cowboy_req.reply(200, [], "Hello world!", req) 8 | {:ok, req, state} 9 | end 10 | 11 | def terminate(_reason, _req, _state), do: :ok 12 | end 13 | -------------------------------------------------------------------------------- /apps/hello_world/mix.exs: -------------------------------------------------------------------------------- 1 | defmodule HelloWorld.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ app: :hello_world, 6 | version: "0.0.1", 7 | deps: deps ] 8 | end 9 | 10 | # Configuration for the OTP application 11 | def application do 12 | [ mod: { HelloWorld, [] }, 13 | applications: [:cowboy] ] 14 | end 15 | 16 | defp deps do 17 | [ 18 | {:cowboy, github: "extend/cowboy"} ] 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /apps/hello_world/test/hello_world_test.exs: -------------------------------------------------------------------------------- 1 | Code.require_file "../test_helper.exs", __FILE__ 2 | 3 | defmodule HelloWorldTest do 4 | use ExUnit.Case 5 | 6 | test "the truth" do 7 | assert true 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /apps/hello_world/test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule ElixirCowboyExamples.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [ apps_path: "apps", 6 | deps: deps ] 7 | end 8 | 9 | # Returns the list of dependencies in the format: 10 | # { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" } 11 | # These dependencies are not accessible from child applications 12 | defp deps do 13 | [{:cowboy, github: "extend/cowboy"}] 14 | end 15 | end 16 | --------------------------------------------------------------------------------