-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from oestrich/hosted-site
Hosted sites
- Loading branch information
Showing
22 changed files
with
353 additions
and
57 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,9 @@ | ||
.hosted { | ||
.navbar { | ||
height: 58px; | ||
} | ||
|
||
.cover { | ||
margin: auto; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Web.Client.PageController do | ||
use Web, :controller | ||
|
||
alias Web.ClientRouter.Helpers, as: Routes | ||
|
||
action_fallback(Web.FallbackController) | ||
|
||
def index(conn, _params) do | ||
redirect(conn, to: Routes.play_path(conn, :show)) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Web.Client.PlayController do | ||
use Web, :controller | ||
|
||
alias Grapevine.Games | ||
alias Web.Game | ||
|
||
action_fallback(Web.FallbackController) | ||
|
||
def show(conn, _params) do | ||
with {:ok, game} <- Games.get_by_host(conn.host), | ||
{:ok, game} <- Games.check_web_client(game), | ||
{:ok, game} <- check_user_allowed(conn, game) do | ||
conn | ||
|> assign(:game, game) | ||
|> assign(:title, "Play #{game.name}") | ||
|> assign(:open_graph_title, game.name) | ||
|> assign(:open_graph_description, "Play #{game.name} on Grapevine") | ||
|> assign(:open_graph_url, play_url(conn, :show, game.short_name)) | ||
|> put_layout("cname.html") | ||
|> put_view(Web.PlayView) | ||
|> render("show.html") | ||
else | ||
{:error, _} -> | ||
{:error, :not_found} | ||
end | ||
end | ||
|
||
defp check_user_allowed(conn, game) do | ||
case Game.client_allowed?(game, conn.assigns, :current_user) do | ||
true -> | ||
{:ok, game} | ||
|
||
false -> | ||
{:error, :not_allowed} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Web.Hosted.PageController do | ||
use Web, :controller | ||
|
||
alias Web.Hosted | ||
|
||
action_fallback(Web.FallbackController) | ||
|
||
def index(conn, _params) do | ||
%{current_game: game} = conn.assigns | ||
|
||
conn | ||
|> put_layout("hosted.html") | ||
|> put_view(Hosted.GameView) | ||
|> assign(:game, game) | ||
|> assign(:title, game.name) | ||
|> assign(:open_graph_title, game.name) | ||
|> assign(:open_graph_description, "#{game.name} on Grapevine") | ||
|> render("show.html") | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Web.Hosted.PlayController do | ||
use Web, :controller | ||
|
||
alias Grapevine.Games | ||
alias Web.Game | ||
|
||
action_fallback(Web.FallbackController) | ||
|
||
def show(conn, _params) do | ||
with {:ok, game} <- Games.get_by_host(conn.host), | ||
{:ok, game} <- Games.check_web_client(game), | ||
{:ok, game} <- check_user_allowed(conn, game) do | ||
conn | ||
|> assign(:game, game) | ||
|> assign(:title, "Play #{game.name}") | ||
|> assign(:open_graph_title, game.name) | ||
|> assign(:open_graph_description, "Play #{game.name} on Grapevine") | ||
|> assign(:open_graph_url, play_url(conn, :show, game.short_name)) | ||
|> put_layout("hosted-play.html") | ||
|> put_view(Web.PlayView) | ||
|> render("show.html") | ||
else | ||
{:error, _} -> | ||
{:error, :not_found} | ||
end | ||
end | ||
|
||
defp check_user_allowed(conn, game) do | ||
case Game.client_allowed?(game, conn.assigns, :current_user) do | ||
true -> | ||
{:ok, game} | ||
|
||
false -> | ||
{:error, :not_allowed} | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Web.ClientRouter do | ||
@moduledoc """ | ||
Router for client sites | ||
""" | ||
|
||
use Web, :router | ||
|
||
pipeline :browser do | ||
plug(:accepts, ["html", "json"]) | ||
plug(:fetch_session) | ||
plug(:fetch_flash) | ||
plug(Phoenix.LiveView.Flash) | ||
plug(:protect_from_forgery) | ||
plug(:put_secure_browser_headers) | ||
plug(Web.Plugs.FetchUser) | ||
plug(Web.Plugs.SessionToken) | ||
end | ||
|
||
scope "/", Web.Client do | ||
pipe_through([:browser]) | ||
|
||
get("/", PageController, :index) | ||
|
||
get("/client", PlayController, :show) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Web.HostedRouter do | ||
@moduledoc """ | ||
Router for hosted sites | ||
""" | ||
|
||
use Web, :router | ||
|
||
pipeline :browser do | ||
plug(:accepts, ["html", "json"]) | ||
plug(:fetch_session) | ||
plug(:fetch_flash) | ||
plug(Phoenix.LiveView.Flash) | ||
plug(:protect_from_forgery) | ||
plug(:put_secure_browser_headers) | ||
plug(Web.Plugs.FetchUser) | ||
plug(Web.Plugs.SessionToken) | ||
end | ||
|
||
scope "/", Web.Hosted do | ||
pipe_through([:browser]) | ||
|
||
get("/", PageController, :index) | ||
|
||
get("/client", PlayController, :show) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
defmodule Web.SplitRouter do | ||
@moduledoc """ | ||
Select the proper router based on the host header | ||
""" | ||
|
||
import Plug.Conn | ||
import Phoenix.Controller | ||
|
||
alias Grapevine.CNAMEs | ||
alias Grapevine.Games | ||
|
||
@config Application.get_env(:grapevine, :web)[:url] | ||
|
||
def init(default), do: default | ||
|
||
def call(conn, _opts) do | ||
case conn.host == @config[:host] do | ||
true -> | ||
Web.Router.call(conn, %{}) | ||
|
||
false -> | ||
case CNAMEs.type_of_host(conn.host) do | ||
{:ok, :client, game_id} -> | ||
{:ok, game} = Games.get(game_id) | ||
|
||
conn = assign(conn, :current_game, game) | ||
|
||
Web.ClientRouter.call(conn, %{}) | ||
|
||
{:ok, :site, game_id} -> | ||
{:ok, game} = Games.get(game_id) | ||
|
||
conn = assign(conn, :current_game, game) | ||
|
||
Web.HostedRouter.call(conn, %{}) | ||
|
||
{:error, :not_found} -> | ||
conn | ||
|> fetch_session() | ||
|> fetch_flash() | ||
|> put_format("html") | ||
|> put_status(:not_found) | ||
|> put_layout({Web.LayoutView, "app.html"}) | ||
|> put_view(Web.ErrorView) | ||
|> render(:"404") | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.