Skip to content

Commit

Permalink
messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 25, 2022
1 parent 00158f8 commit 5430de1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/handler.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Servy.Handler do
alias Servy.Conv
alias Servy.BearController
alias Servy.VideoCam
import Servy.Plugins, only: [rewrite_path: 1, log: 1, track: 1]
import Servy.Parser, only: [parse: 1]
@moduledoc "Handles HTTP requests."
Expand All @@ -20,6 +21,37 @@ defmodule Servy.Handler do
# route(conv, conv.method, conv.path)
# end

def route(%Conv{method: "GET", path: "/snapshots"} = conv) do
# the request-handling process
parent = self()

spawn(fn -> send(parent, {:result, VideoCam.get_snapshot("cam-1")}) end)
spawn(fn -> send(parent, {:result, VideoCam.get_snapshot("cam-2")}) end)
spawn(fn -> send(parent, {:result, VideoCam.get_snapshot("cam-3")}) end)

snapshot1 =
receive do
{:result, filename} -> filename
end

snapshot2 =
receive do
{:result, filename} -> filename
end

snapshot3 =
receive do
{:result, filename} -> filename
end

# snapshot2 = VideoCam.get_snapshot("cam-2")
# snapshot3 = VideoCam.get_snapshot("cam-3")

snapshots = [snapshot1, snapshot2, snapshot3]

%{conv | status: 200, resp_body: inspect(snapshots)}
end

def route(%Conv{method: "GET", path: "/kaboom"}) do
raise "Kaboom!"
end
Expand Down
15 changes: 15 additions & 0 deletions lib/video_cam.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Servy.VideoCam do
@doc """
Simulates sending a request to an external API
to get a snapshot image from a video camera.
"""
def get_snapshot(camera_name) do
# CODE GOES HERE TO SEND A REQUEST TO THE EXTERNAL API

# Sleep for 1 second to simulate that the API can be slow:
:timer.sleep(1000)

# Example response returned from the API:
"#{camera_name}-snapshot.jpg"
end
end

1 comment on commit 5430de1

@dids-reyes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iex -S mix
spawn(Servy.HttpServer, :start, [4000])
curl http://localhost:4000/snapshots

GET requests and snapshots delay

Please sign in to comment.