Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): introduce a MaintenanceMiddleware #1395

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions apis_core/core/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
from pathlib import Path

from django.conf import settings
from django.shortcuts import render

b1rger marked this conversation as resolved.
Show resolved Hide resolved
logger = logging.getLogger(__name__)


class MaintenanceMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
maintenance_file = getattr(
settings, "APIS_MAINTENANCE_FILE", "apis_maintenance"
)
if Path(maintenance_file).exists():
b1rger marked this conversation as resolved.
Show resolved Hide resolved
logger.warning("Site is running in maintenance mode")
if hasattr(request, "user"):
if request.user.is_superuser:
return self.get_response(request)
return render(request, "maintenance.html")
return self.get_response(request)
20 changes: 20 additions & 0 deletions apis_core/core/static/css/maintenance.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
body {
text-align: center;
padding: 150px;
}

h1 {
font-size: 50px;
}

body {
font: 20px Helvetica, sans-serif;
color: #333;
}

article {
display: block;
text-align: left;
width: 650px;
margin: 0 auto;
}
20 changes: 20 additions & 0 deletions apis_core/core/templates/maintenance.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
{% load static %}
<html lang="en">
<link href="{% static 'css/maintenance.css' %}" rel="stylesheet" />
<head>
<meta charset="UTF-8">
<title>Site Maintenance</title>
</head>
<body>
<article>
<h1>We&rsquo;ll be back soon!</h1>
<div>
<p>
Sorry for the inconvenience but we&rsquo;re performing some maintenance at the moment. We&rsquo;ll be back online shortly!
</p>
<p>&mdash; The Team</p>
</div>
</article>
</body>
</html>
19 changes: 18 additions & 1 deletion docs/source/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,21 @@ APIS_LIST_VIEW_OBJECT_FILTER
Allows to define a function that receives the view - including e.g. the
`request` object - and a queryset and can do custom filtering on that queryset.
This can be used to set the listviews to public using the
`APIS_LIST_VIEWS_ALLOWED` setting, but still only list specific entities.
`APIS_LIST_VIEWS_ALLOWED` setting, but still only list specific entities.


Maintenance Middleware
^^^^^^^^^^^^^^^^^^^^^^

APIS ships a maintenance middlware that you can use and activate to enable a maintenance mode in your project.
Maintenance mode means that only superuser accounts can access the webinterfaces, all other requests are being
answered with a simple maintenance mode page (the ``maintenance.html`` template).
To use the middleware, add

.. code-block:: python

"apis_core.core.middleware.MaintenanceMiddleware"

to your ``settings.MIDDLEWARE`` list. To activate the maintenance mode once the middlware is enabled, simply
create a file ``apis_maintenance`` in the directory the main Django process runs in.
The path of the maintenance file can be changed in the settings: ``APIS_MAINTENANCE_FILE = "path of the file"``
Loading