-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move config to struct, fix HlsSkip (#130)
* Move config to struct, fix HlsSkip * Update description * Validate config in controller * Update descriptions
- Loading branch information
Showing
8 changed files
with
97 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Jellyfish.Room.Config do | ||
@moduledoc """ | ||
Room configuration | ||
""" | ||
@enforce_keys [:max_peers, :video_codec, :webhook_url] | ||
|
||
defstruct @enforce_keys | ||
|
||
@type max_peers :: non_neg_integer() | nil | ||
@type video_codec :: :h264 | :vp8 | nil | ||
@type webhook_url :: String.t() | ||
|
||
@type t :: %__MODULE__{ | ||
max_peers: max_peers(), | ||
video_codec: video_codec(), | ||
webhook_url: URI.t() | ||
} | ||
|
||
@spec from_params(map()) :: {:ok, __MODULE__.t()} | {:error, atom()} | ||
def from_params(params) do | ||
max_peers = Map.get(params, "maxPeers") | ||
video_codec = Map.get(params, "videoCodec") | ||
webhook_url = Map.get(params, "webhookUrl") | ||
|
||
with :ok <- validate_max_peers(max_peers), | ||
{:ok, video_codec} <- codec_to_atom(video_codec), | ||
:ok <- validate_webhook_url(webhook_url) do | ||
{:ok, | ||
%__MODULE__{ | ||
max_peers: max_peers, | ||
video_codec: video_codec, | ||
webhook_url: webhook_url | ||
}} | ||
else | ||
error -> error | ||
end | ||
end | ||
|
||
defp validate_max_peers(nil), do: :ok | ||
defp validate_max_peers(max_peers) when is_integer(max_peers) and max_peers >= 0, do: :ok | ||
defp validate_max_peers(_max_peers), do: {:error, :invalid_max_peers} | ||
|
||
defp validate_webhook_url(nil), do: :ok | ||
|
||
defp validate_webhook_url(uri) do | ||
uri | ||
|> URI.parse() | ||
|> Map.take([:host, :path, :scheme]) | ||
|> Enum.all?(fn {_key, value} -> not is_nil(value) end) | ||
|> if do | ||
:ok | ||
else | ||
{:error, :invalid_webhook_url} | ||
end | ||
end | ||
|
||
defp codec_to_atom("h264"), do: {:ok, :h264} | ||
defp codec_to_atom("vp8"), do: {:ok, :vp8} | ||
defp codec_to_atom(nil), do: {:ok, nil} | ||
defp codec_to_atom(_codec), do: {:error, :invalid_video_codec} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters