Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hello_world_elixir/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ A tiny example Elli application.

mix deps.get
iex -S mix
curl localhost:3000/hello/world
curl localhost:4000/hello/world
27 changes: 27 additions & 0 deletions hello_world_elixir/lib/hello_world.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,38 @@ defmodule HelloWorld do
{200, [], "Hello, World!"}
end

# Return 200 and the first 20 chars of this file to GET requests to /send
defp handle(:GET, ["send"], _req) do
{:ok, [], { :file, __ENV__.file, [{:bytes, 0, 20}] } }
end

# Return 200 and a chunked stream to GET requests to /chunked
defp handle(:GET, ["chunked"], req) do
ref = :elli_request.chunk_ref(req)
spawn_link fn -> chunk_loop(ref) end
{:chunk, [{"Content-Type", "text/event-stream"}]}
end

# Return 404 to any other requests
defp handle(_, _, _req) do
{404, [], "Our princess is in another castle..."}
end

# start the chunk_loop with count n = 10...
defp chunk_loop ref do
chunk_loop ref, 10
end
# close the request when n = 0 is reached...
defp chunk_loop ref, 0 do
:elli_request.close_chunk ref
end
# count down the counter n... once a second.
defp chunk_loop ref, n do
:timer.sleep(1000)
:elli_request.send_chunk(ref, ["chunk#{n}\n"])
chunk_loop ref, n-1
end

# Handle request events, like request completed, exception
# thrown, client timeout, etc. Must return `ok'.
@impl :elli_handler
Expand Down
2 changes: 1 addition & 1 deletion hello_world_elixir/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule HelloWorld.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:elli, "~> 2.0"},
{:elli, "~> 2.1"},
{:httpoison, "~> 1.0", only: [:test]}
]
end
Expand Down