Introduce support for custom nginx error pages

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-10-09 12:17:33 +02:00
parent 3a3a500e42
commit 07a5d10421
31 changed files with 476 additions and 63 deletions

View File

@@ -16,6 +16,7 @@ defmodule Mobilizon do
alias Mobilizon.{Config, Storage, Web}
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Service.ErrorPage
alias Mobilizon.Service.Export.{Feed, ICalendar}
@name Mix.Project.config()[:name]
@@ -104,7 +105,7 @@ defmodule Mobilizon do
defp fallback_options(fallback), do: [fallback: fallback(default: fallback)]
defp task_children(:test), do: []
defp task_children(_), do: [relay_actor(), anonymous_actor()]
defp task_children(_), do: [relay_actor(), anonymous_actor(), render_error_page()]
defp relay_actor do
%{
@@ -121,4 +122,12 @@ defmodule Mobilizon do
restart: :temporary
}
end
defp render_error_page do
%{
id: :render_error_page_init,
start: {Task, :start_link, [&ErrorPage.init/0]},
restart: :temporary
}
end
end

17
lib/service/error_page.ex Normal file
View File

@@ -0,0 +1,17 @@
defmodule Mobilizon.Service.ErrorPage do
@moduledoc """
Render an error page
"""
def init do
render_error_page()
end
defp render_error_page do
content =
Phoenix.View.render_to_string(Mobilizon.Web.ErrorView, "500.html", conn: %Plug.Conn{})
path = Path.join(Application.app_dir(:mobilizon, "priv/errors"), "error.html")
File.write(path, content)
end
end

View File

@@ -37,8 +37,8 @@ defmodule Mobilizon.Web.Gettext do
locale in locales -> locale
# Either the first part matches, "fr_CA" => "fr"
split_locale(locale) in locales -> split_locale(locale)
# Otherwise default to english
true -> "en"
# Otherwise set to default
true -> Keyword.get(Mobilizon.Config.instance_config(), :default_language, "en") || "en"
end
end

View File

@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="<%= Gettext.get_locale() %>">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><%= gettext "This page is not correct" %></title>
<style>
body.error {
font-family: BlinkMacSystemFont, Roboto, Oxygen, Ubuntu, Cantarell, Segoe UI, Fira Sans, Droid Sans, Helvetica Neue, Helvetica, Arial, sans-serif;
background: #efeef4;
position: absolute;
text-align: center;
color: #3c376e;
width: 100%;
height: 100%;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
}
body.error .dialog h1 {
font-size: 20px;
line-height: 28px;
font-weight: 400;
}
body.error .dialog img {
display: block;
max-width: 470px;
width: 100%;
height: auto;
margin: -120px auto auto;
}
</style>
</head>
<body class="error">
<main role="main" class="dialog">
<div class="error_illustration">
<img src="/static/img/mobilizon_logo.png" />
<!-- <img src="/static/img/error.png" alt="" width="500" /> -->
</div>
<div class="error__message">
<h1><%= gettext "We're sorry, but something went wrong on our end." %></h1>
<p><%= gettext "The Mobilizon server seems to be temporarily down." %></p>
</div>
</main>
</body>
</html>

View File

@@ -39,7 +39,11 @@ defmodule Mobilizon.Web.ErrorView do
end
def render("500.html", _assigns) do
"Internal server error"
Mobilizon.Config.instance_config()
|> Keyword.get(:default_language, "en")
|> Gettext.put_locale()
render("500_page.html", %{})
end
# In case no render clause matches or no