File tree Expand file tree Collapse file tree 4 files changed +55
-27
lines changed
elixir/gsmlg_umbrella/apps/gsmlg_web/lib/gsmlg_web Expand file tree Collapse file tree 4 files changed +55
-27
lines changed Original file line number Diff line number Diff line change @@ -8,29 +8,9 @@ defmodule GSMLGWeb.FallbackController do
8
8
9
9
# This clause is an example of how to handle resources that cannot be found.
10
10
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" )
35
15
end
36
16
end
Original file line number Diff line number Diff line change @@ -4,4 +4,45 @@ defmodule GSMLGWeb.PageController do
4
4
def index ( conn , _params ) do
5
5
render ( conn , "index.html" )
6
6
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
7
48
end
Original file line number Diff line number Diff line change @@ -66,4 +66,11 @@ defmodule GSMLGWeb.Router do
66
66
forward "/mailbox" , Plug.Swoosh.MailboxPreview
67
67
end
68
68
end
69
+
70
+ # fallback not_found
71
+ scope "/" , GSMLGWeb do
72
+ pipe_through :browser
73
+
74
+ get "/*request_path" , PageController , :not_found
75
+ end
69
76
end
Original file line number Diff line number Diff line change @@ -3,9 +3,9 @@ defmodule GSMLGWeb.ErrorView do
3
3
4
4
# If you want to customize a particular status code
5
5
# 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
9
9
10
10
# By default, Phoenix returns the status message from
11
11
# the template name. For example, "404.html" becomes
You can’t perform that action at this time.
0 commit comments