Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 202a0c8

Browse files
committed
Add a CORS header allowing browser-based clients to consume the API.
The MaveDB API is publicly accessible and can be consumed by any client. However, it is currently consumed mainly by server-side clients such as mavevis. Browser-based clients, including any MaveDB client application that is not served by the Django application, generally require CORS headers granting explicit permission to consume the API. To allow this, we add a simple HTTP response header granting access to clients from any domain: Access-Control-Allow-Origin="*" The header is added using Django middleware, with support for Django 1 (though a draft of a version for later Django versions is included and commented out). For current Django versions, there are also Django middleware packages available for this purpose, which provide configuration-driven CORS headers and are useful in case one wants to limit access to specific clients.
1 parent b2ab961 commit 202a0c8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

middleware/cors.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from django.utils.deprecation import MiddlewareMixin
2+
3+
# This version supports Django 1.
4+
5+
6+
class CorsMiddleware(MiddlewareMixin):
7+
"""CORS middleware
8+
9+
Methods
10+
-------
11+
process_response(request, response)
12+
Add a CORS header to the HTTP response.
13+
"""
14+
15+
def process_response(self, request, response):
16+
"""Add a CORS Access-Control-Allow-Origin=* header to the response.
17+
18+
This allows API responses to be consumed by browser-based clients.
19+
20+
Parameters
21+
----------
22+
request : HttpRequest
23+
The current HTTP request
24+
response : HttpResponse
25+
The HTTP response to which a CORS header should be added
26+
27+
Returns
28+
-------
29+
The HTTP response
30+
"""
31+
response["Access-Control-Allow-Origin"] = "*"
32+
return response
33+
34+
35+
# For later versions of Django:
36+
# def cors_middleware(get_response):
37+
# # One-time configuration and initialization.
38+
#
39+
# def middleware(request):
40+
# # Code to be executed for each request before
41+
# # the view (and later middleware) are called.
42+
# response = get_response(request)
43+
# response["Access-Control-Allow-Origin"] = "*"
44+
# return response
45+
#
46+
# return middleware

settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"django.middleware.security.SecurityMiddleware",
104104
"tracking.middleware.VisitorTrackingMiddleware",
105105
"django.contrib.sessions.middleware.SessionMiddleware",
106+
"middleware.cors.CorsMiddleware",
106107
"django.middleware.common.CommonMiddleware",
107108
"django.middleware.csrf.CsrfViewMiddleware",
108109
"django.contrib.auth.middleware.AuthenticationMiddleware",

0 commit comments

Comments
 (0)