Skip to content

Commit 63d4140

Browse files
committed
fix(elxir:gsmlg): Fix serve static html.
1 parent b9b8732 commit 63d4140

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/controllers/fallback_controller.ex

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,9 @@ defmodule GSMLGWeb.FallbackController do
88

99
# This clause is an example of how to handle resources that cannot be found.
1010
def call(conn, {:error, :not_found}) do
11-
case handle_path(conn.request_path) do
12-
{:html, file} ->
13-
conn
14-
|> put_resp_header("content-type", "text/html; charset=utf-8")
15-
|> Plug.Conn.send_file(200, file)
16-
{:not_found_page, file} ->
17-
conn
18-
|> put_resp_header("content-type", "text/html; charset=utf-8")
19-
|> Plug.Conn.send_file(404, file)
20-
_ ->
21-
conn
22-
|> put_status(:not_found)
23-
|> put_view(GSMLGWeb.ErrorView)
24-
|> render(:"404")
25-
end
26-
end
27-
28-
defp handle_path(path) do
29-
path = if path == "/", do: "/index", else: path
30-
cond do
31-
File.exists?("priv/static" <> path <> ".html") -> {:html, "priv/static" <> path <> ".html"}
32-
File.exists?("priv/static/404.html") -> {:not_found_page, "priv/static/404.html"}
33-
true -> {:null, nil}
34-
end
11+
conn
12+
|> put_status(:not_found)
13+
|> put_view(GSMLGWeb.ErrorView)
14+
|> render(:"404")
3515
end
3616
end

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/controllers/page_controller.ex

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,45 @@ defmodule GSMLGWeb.PageController do
44
def index(conn, _params) do
55
render(conn, "index.html")
66
end
7+
8+
def not_found(conn, _params) do
9+
IO.inspect(conn)
10+
11+
case handle_path(conn.request_path) do
12+
{:html, file} ->
13+
conn
14+
|> put_resp_header("content-type", "text/html; charset=utf-8")
15+
|> Plug.Conn.send_file(200, file)
16+
17+
{:not_found_page, file} ->
18+
conn
19+
|> put_resp_header("content-type", "text/html; charset=utf-8")
20+
|> Plug.Conn.send_file(404, file)
21+
22+
_ ->
23+
conn
24+
|> put_status(:not_found)
25+
|> put_view(GSMLGWeb.ErrorView)
26+
|> render(:"404")
27+
end
28+
end
29+
30+
defp handle_path(path) do
31+
IO.puts(path)
32+
path = if path == "/", do: "/index", else: path
33+
34+
cond do
35+
File.exists?(file_path = Path.join(:code.priv_dir(:gsmlg_web), "static" <> path <> ".html")) ->
36+
{:html, file_path}
37+
38+
File.exists?(file_path = Path.join(:code.priv_dir(:gsmlg_web), "static" <> path <> "/index.html")) ->
39+
{:html, file_path}
40+
41+
File.exists?(file_path = Path.join(:code.priv_dir(:gsmlg_web), "static/404.html")) ->
42+
{:not_found_page, file_path}
43+
44+
true ->
45+
{:null, nil}
46+
end
47+
end
748
end

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/router.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,11 @@ defmodule GSMLGWeb.Router do
6666
forward "/mailbox", Plug.Swoosh.MailboxPreview
6767
end
6868
end
69+
70+
# fallback not_found
71+
scope "/", GSMLGWeb do
72+
pipe_through :browser
73+
74+
get "/*request_path", PageController, :not_found
75+
end
6976
end

elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web/views/error_view.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ defmodule GSMLGWeb.ErrorView do
33

44
# If you want to customize a particular status code
55
# for a certain format, you may uncomment below.
6-
# def render("500.html", _assigns) do
7-
# "Internal Server Error"
8-
# end
6+
def render("500.html", _assigns) do
7+
"Internal Server Error"
8+
end
99

1010
# By default, Phoenix returns the status message from
1111
# the template name. For example, "404.html" becomes

0 commit comments

Comments
 (0)