Skip to content

Commit

Permalink
Return peer_websocket_url in PeerDetailsResponse (#181)
Browse files Browse the repository at this point in the history
* Return peer_websocket_url in peer creation

* Fix OpenAPI spec

* Fix get_peer_websocket_address

* Move new code from application.ex to jellyfish.ex
  • Loading branch information
Rados13 authored Apr 17, 2024
1 parent 9cfc3e0 commit 69e3a61
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 9 deletions.
10 changes: 10 additions & 0 deletions lib/jellyfish.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ defmodule Jellyfish do
@version Mix.Project.config()[:version]

def version(), do: @version

@spec address() :: binary()
def address() do
Application.fetch_env!(:jellyfish, :address)
end

@spec peer_websocket_address() :: binary()
def peer_websocket_address() do
Application.fetch_env!(:jellyfish, :address) <> "/socket/peer/websocket"
end
end
11 changes: 11 additions & 0 deletions lib/jellyfish/resource_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ defmodule Jellyfish.ResourceManager do
def init(opts) do
Logger.debug("Initialize resource manager")

base_path = Recording.get_base_path()
dir_result = File.mkdir_p(base_path)

case dir_result do
{:error, reason} ->
Logger.error("Can't create directory at #{base_path} with reason: #{reason}")

:ok ->
nil
end

schedule_free_resources(opts.interval)

{:ok, opts}
Expand Down
2 changes: 1 addition & 1 deletion lib/jellyfish/room_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ defmodule Jellyfish.RoomService do

Event.broadcast_server_notification({:room_created, room_id})

{:reply, {:ok, room, Application.fetch_env!(:jellyfish, :address)}, state}
{:reply, {:ok, room, Jellyfish.address()}, state}
else
{:error, :room_already_exists} = error ->
{:reply, error, state}
Expand Down
13 changes: 13 additions & 0 deletions lib/jellyfish_web/api_spec/peer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ defmodule JellyfishWeb.ApiSpec.Peer do
})
end

defmodule WebSocketUrl do
@moduledoc false

require OpenApiSpex

OpenApiSpex.schema(%{
title: "WebsocketURL",
description: "Websocket URL to which peer has to connect",
type: :string,
example: "www.jellyfish.org/socket/peer"
})
end

defmodule PeerMetadata do
@moduledoc false

Expand Down
6 changes: 5 additions & 1 deletion lib/jellyfish_web/api_spec/responses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{PeerDetailsResponse, "Response containing peer details and their token",
%OpenApiSpex.Schema{
type: :object,
properties: %{peer: JellyfishWeb.ApiSpec.Peer, token: JellyfishWeb.ApiSpec.Peer.Token},
properties: %{
peer: JellyfishWeb.ApiSpec.Peer,
token: JellyfishWeb.ApiSpec.Peer.Token,
peer_websocket_url: JellyfishWeb.ApiSpec.Peer.WebSocketUrl
},
required: [:peer, :token]
}},
{RoomDetailsResponse, "Response containing room details", JellyfishWeb.ApiSpec.Room},
Expand Down
6 changes: 5 additions & 1 deletion lib/jellyfish_web/controllers/peer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ defmodule JellyfishWeb.PeerController do
{:ok, peer_type} <- Peer.parse_type(peer_type_string),
{:ok, _room_pid} <- RoomService.find_room(room_id),
{:ok, peer} <- Room.add_peer(room_id, peer_type, peer_options) do
assigns = [peer: peer, token: PeerToken.generate(%{peer_id: peer.id, room_id: room_id})]
assigns = [
peer: peer,
token: PeerToken.generate(%{peer_id: peer.id, room_id: room_id}),
peer_websocket_url: Jellyfish.peer_websocket_address()
]

conn
|> put_resp_content_type("application/json")
Expand Down
4 changes: 2 additions & 2 deletions lib/jellyfish_web/controllers/peer_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule JellyfishWeb.PeerJSON do
@moduledoc false
alias Jellyfish.Peer.WebRTC

def show(%{peer: peer, token: token}) do
%{data: %{peer: data(peer), token: token}}
def show(%{peer: peer, token: token, peer_websocket_url: ws_url}) do
%{data: %{peer: data(peer), token: token, peer_websocket_url: ws_url}}
end

def show(%{peer: peer}) do
Expand Down
8 changes: 8 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ components:
title: HlsPart
type: integer
x-struct: Elixir.JellyfishWeb.ApiSpec.HLS.Params.HlsPart
WebsocketURL:
description: Websocket URL to which peer has to connect
example: www.jellyfish.org/socket/peer
title: WebsocketURL
type: string
x-struct: Elixir.JellyfishWeb.ApiSpec.Peer.WebSocketUrl
ComponentDetailsResponse:
description: Response containing component details
properties:
Expand Down Expand Up @@ -681,6 +687,8 @@ components:
properties:
peer:
$ref: '#/components/schemas/Peer'
peer_websocket_url:
$ref: '#/components/schemas/WebsocketURL'
token:
$ref: '#/components/schemas/AuthToken'
required:
Expand Down
17 changes: 13 additions & 4 deletions test/jellyfish_web/controllers/peer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,26 @@ defmodule JellyfishWeb.PeerControllerTest do
assert response(conn, :no_content)
end)

{:ok, %{conn: conn, room_id: id}}
peer_ws_url = Jellyfish.peer_websocket_address()

{:ok, %{conn: conn, room_id: id, peer_ws_url: peer_ws_url}}
end

describe "create peer" do
test "renders peer when data is valid", %{conn: conn, room_id: room_id} do
test "renders peer when data is valid", %{
conn: conn,
room_id: room_id,
peer_ws_url: peer_ws_url
} do
conn = post(conn, ~p"/room/#{room_id}/peer", type: @peer_type)
response = json_response(conn, :created)
assert_response_schema(response, "PeerDetailsResponse", @schema)

assert %{"peer" => %{"id" => peer_id, "type" => @peer_type}, "token" => token} =
response["data"]
assert %{
"peer" => %{"id" => peer_id, "type" => @peer_type},
"token" => token,
"peer_websocket_url" => ^peer_ws_url
} = response["data"]

conn = get(conn, ~p"/room/#{room_id}")

Expand Down

0 comments on commit 69e3a61

Please sign in to comment.