-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prepare docker (compose) setup
docker compose setup to run complete obr-web. e.g. to use for (iOS) app development.
- Loading branch information
Showing
24 changed files
with
249 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM python:3.12-alpine | ||
|
||
ENV PYTHONUNBUFFERED True | ||
|
||
RUN pip3 install -U pip gunicorn pytz | ||
|
||
WORKDIR /app | ||
COPY compose/cdn-cookie-validator/ ./ | ||
|
||
EXPOSE 8000 | ||
|
||
CMD exec gunicorn -w 4 main:app --bind :8000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
import re | ||
import pytz | ||
import http.cookies | ||
import datetime | ||
|
||
""" | ||
curl --cookie "Cloud-CDN-Cookie=URLPrefix=aHR0cHM6Ly9tZWRpYS5vcGVuYnJvYWRjYXN0LmNoL2VuY29kZWQ=:Expires=1721308780:KeyName=cdn-key:Signature=YZ1MOde2rsKV-sucFvddUBTCDKs=" http://localhost:8008/ | ||
""" | ||
|
||
TIMEZONE = pytz.timezone("Europe/Zurich") | ||
|
||
def app(environ, start_response): | ||
cookie_string = environ.get('HTTP_COOKIE', '') | ||
cookies = http.cookies.SimpleCookie(cookie_string) | ||
cloud_cdn_cookie = cookies.get('Cloud-CDN-Cookie', None) | ||
|
||
is_valid = True | ||
|
||
status = '200 OK' | ||
response = 'Valid' | ||
|
||
if cloud_cdn_cookie: | ||
match = re.search(r'Expires=(\d+)', cloud_cdn_cookie.value) | ||
if match: | ||
now = datetime.datetime.now(pytz.utc).astimezone(TIMEZONE).replace(tzinfo=pytz.utc) | ||
expires_at = datetime.datetime.fromtimestamp(int(match.group(1)), pytz.utc) | ||
# print("now: ", now) | ||
# print("expires: ", expires_at) | ||
print("expires in:", (expires_at - now).total_seconds()) | ||
if expires_at < now: | ||
is_valid = False | ||
else: | ||
is_valid = False | ||
|
||
status = '200 OK' if is_valid else '403 Forbidden' | ||
response = 'OK' if is_valid else 'Invalid CDN Cookie' | ||
|
||
response_headers = [('Content-type', 'text/plain')] | ||
start_response(status, response_headers) | ||
return [response.encode()] |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,7 @@ | ||
import os | ||
|
||
import django | ||
from django.core.handlers.asgi import ( | ||
ASGIHandler, | ||
FileResponse, | ||
RequestAborted, | ||
set_script_prefix, | ||
signals, | ||
sync_to_async, | ||
) | ||
from django.core.asgi import get_asgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.default") | ||
|
||
|
||
class PatchedASGIHandler(ASGIHandler): | ||
async def __call__(self, scope, receive, send): | ||
if scope["type"] != "http": | ||
return | ||
try: | ||
body_file = await self.read_body(receive) | ||
except RequestAborted: | ||
return | ||
set_script_prefix(self.get_script_prefix(scope)) | ||
await sync_to_async(signals.request_started.send, thread_sensitive=True)( | ||
sender=self.__class__, | ||
scope=scope, | ||
) | ||
request, error_response = self.create_request(scope, body_file) | ||
if request is None: | ||
await self.send_response(error_response, send) | ||
return | ||
response = await self.get_response_async(request) | ||
response._handler_class = self.__class__ # NOQA: SLF001 | ||
if isinstance(response, FileResponse): | ||
response.block_size = self.chunk_size | ||
await self.send_response(response, send) | ||
|
||
|
||
def get_asgi_application(): | ||
django.setup(set_prefix=False) | ||
return PatchedASGIHandler() | ||
|
||
|
||
application = get_asgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.