Skip to content

Commit

Permalink
Refactor AEWebRootController
Browse files Browse the repository at this point in the history
  • Loading branch information
bchamagne committed Jan 5, 2023
1 parent 9032e4e commit b5acfc7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 43 deletions.
37 changes: 2 additions & 35 deletions lib/archethic_web/controllers/aeweb_root_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,7 @@ defmodule ArchethicWeb.AEWebRootController do

use ArchethicWeb, :controller

def index(conn, params = %{"url_path" => url_path}) do
cache_headers = WebHostingController.get_cache_headers(conn)

case WebHostingController.get_website(params, cache_headers) do
{:ok, file_content, encoding, mime_type, cached?, etag} ->
WebHostingController.send_response(conn, file_content, encoding, mime_type, cached?, etag)

{:error, {:is_a_directory, reference_transaction}} ->
{:ok, listing_html, encoding, mime_type, cached?, etag} =
WebHostingController.DirectoryListing.list(
conn.request_path,
params,
reference_transaction,
cache_headers
)

WebHostingController.send_response(conn, listing_html, encoding, mime_type, cached?, etag)

{:error, :file_not_found} ->
# If file is not found, returning default file (url can be handled by index file)
case url_path do
[] ->
send_resp(conn, 404, "Not Found")

["index.html"] ->
send_resp(conn, 400, "Not Found")

_path ->
params = Map.put(params, "url_path", ["index.html"])
index(conn, params)
end

_ ->
send_resp(conn, 404, "Not Found")
end
def index(conn, params) do
WebHostingController.web_hosting(conn, params)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ defmodule ArchethicWeb.API.WebHostingController do
# Instead of returning the entire transaction,
# we return a triplet with only the formatted data we need
@spec get_reference_transaction(binary()) ::
{:ok, {binary(), map(), DateTime.t()}} | {:error, atom()}
{:ok, {binary(), map(), DateTime.t()}} | {:error, term()}
defp get_reference_transaction(address) do
# started by ArchethicWeb.Supervisor
cache_server = :web_hosting_cache_ref_tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ defmodule ArchethicWeb.API.WebHostingController.Resources do
{:ok, file :: map(), mime_type :: binary(), resource_path :: binary()}
| {:error, :is_a_directory | :file_not_found}
defp get_file(metadata, []) do
case Map.get(metadata, "index.html", :error) do
:error ->
case Map.get(metadata, "index.html") do
nil ->
{:error, :is_a_directory}

value ->
{:ok, value, MIME.from_path("index.html"), "index.html"}
file ->
{:ok, file, MIME.from_path("index.html"), "index.html"}
end
end

Expand All @@ -77,7 +77,15 @@ defmodule ArchethicWeb.API.WebHostingController.Resources do
if is_a_directory?(metadata, resource_path) do
{:error, :is_a_directory}
else
{:error, :file_not_found}
# Handle JS History API by serving index.html instead of a 404
# We loose the ability to return real 404 errors
case Map.get(metadata, "index.html") do
nil ->
{:error, :file_not_found}

file ->
{:ok, file, MIME.from_path("index.html"), "index.html"}
end
end

file ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ defmodule ArchethicWeb.API.WebHostingControllerTest do
:ok
end

test "should return file does not exist", %{conn: conn} do
test "should return index.html on file not found (handle JS routing)", %{conn: conn} do
conn =
get(
conn,
"/api/web_hosting/0000225496a380d5005cb68374e9b8b45d7e0f505a42f8cd61cbd43c3684c5cbacba/file.html"
)

assert "Cannot find file content" = response(conn, 404)
assert "<h1>Archethic</h1>" = response(conn, 200)
end

test "should return default index.html file", %{conn: conn} do
Expand Down

0 comments on commit b5acfc7

Please sign in to comment.