From d90e489fc6f7abbab7c64f80b8d6851c08e9fa1d Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 23 May 2018 00:57:34 +0200 Subject: [PATCH] Improve the debug mode --- README.md | 2 +- docker-compose-tests.yml | 1 + utils/__init__.py | 12 ++++++++++++ utils/urlutils.py | 10 +++++++++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c93a814..9c96c79 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ $ pip install -r requirements.txt # Start the Celery worker, RabbitMQ and MongoDB $ docker-compose -f docker-compose-dev.yml up -d # Run the server locally -$ FLASK_APP=app.py flask run -p 5005 --with-threads +$ MICROBLOGPUB_DEBUG=1 FLASK_APP=app.py flask run -p 5005 --with-threads ``` ## Contributions diff --git a/docker-compose-tests.yml b/docker-compose-tests.yml index d41baf8..af55d05 100644 --- a/docker-compose-tests.yml +++ b/docker-compose-tests.yml @@ -13,6 +13,7 @@ services: environment: - MICROBLOGPUB_AMQP_BROKER=pyamqp://guest@rmq// - MICROBLOGPUB_MONGODB_HOST=mongo:27017 + - MICROBLOGPUB_DEBUG=1 celery: build: . links: diff --git a/utils/__init__.py b/utils/__init__.py index e69de29..c30c37d 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -0,0 +1,12 @@ +import logging + +logger = logging.getLogger(__name__) + + +def strtobool(s: str) -> bool: + if s in ['y', 'yes', 'true', 'on', '1']: + return True + if s in ['n', 'no', 'false', 'off', '0']: + return False + + raise ValueError(f'cannot convert {s} to bool') diff --git a/utils/urlutils.py b/utils/urlutils.py index b304f5e..3e9087e 100644 --- a/utils/urlutils.py +++ b/utils/urlutils.py @@ -1,16 +1,24 @@ import logging +import os import socket import ipaddress from urllib.parse import urlparse +from . import strtobool + logger = logging.getLogger(__name__) -def is_url_valid(url): +def is_url_valid(url: str) -> bool: parsed = urlparse(url) if parsed.scheme not in ['http', 'https']: return False + # XXX in debug mode, we want to allow requests to localhost to test the federation with local instances + debug_mode = strtobool(os.getenv('MICROBLOGPUB_DEBUG', 'false')) + if debug_mode: + return True + if parsed.hostname in ['localhost']: return False