From 7102ca7f4cd0fd6a190f004c46b0c543a1905c41 Mon Sep 17 00:00:00 2001 From: "kam (from the studio)" Date: Fri, 4 Mar 2022 11:02:57 +0100 Subject: [PATCH] env variables and url prefixer --- .gitignore | 4 +++- config.py | 15 +++++++++++++++ exquisite_branch/__init__.py | 23 ++++------------------- exquisite_branch/prefix.py | 15 +++++++++++++++ setup.py | 3 ++- 5 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 config.py create mode 100644 exquisite_branch/prefix.py diff --git a/.gitignore b/.gitignore index f971665..d9011c2 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,6 @@ htmlcov/ dist/ build/ -*.egg-info/ \ No newline at end of file +*.egg-info/ + +.env \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..851e11e --- /dev/null +++ b/config.py @@ -0,0 +1,15 @@ +import os + +class Config(object): + DEBUG = False + TESTING = False + URL_PREFIX = '' + +class ProductionConfig(Config): + DEBUG = False + URL_PREFIX = os.environ.get("URL_PREFIX") + + +class DevelopmentConfig(Config): + ENV = "development" + DEVELOPMENT = True diff --git a/exquisite_branch/__init__.py b/exquisite_branch/__init__.py index a24a774..ed25e6a 100644 --- a/exquisite_branch/__init__.py +++ b/exquisite_branch/__init__.py @@ -1,23 +1,6 @@ import os from flask import Flask - - -class PrefixMiddleware(object): - - def __init__(self, app, prefix=''): - self.app = app - self.prefix = prefix - - def __call__(self, environ, start_response): - - if environ['PATH_INFO'].startswith(self.prefix): - environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] - environ['SCRIPT_NAME'] = self.prefix - return self.app(environ, start_response) - else: - start_response('404', [('Content-Type', 'text/plain')]) - return ["This url does not belong to the app.".encode()] - +from . import prefix def create_app(test_config=None): # create and configure the app @@ -55,6 +38,8 @@ def create_app(test_config=None): from . import home app.register_blueprint(home.bp) - app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/xquisite') + + + app.wsgi_app = prefix.PrefixMiddleware(app.wsgi_app, prefix=os.environ.get("URL_PREFIX", '')) return app diff --git a/exquisite_branch/prefix.py b/exquisite_branch/prefix.py new file mode 100644 index 0000000..bef6d77 --- /dev/null +++ b/exquisite_branch/prefix.py @@ -0,0 +1,15 @@ +class PrefixMiddleware(object): + + def __init__(self, app, prefix=''): + self.app = app + self.prefix = prefix + + def __call__(self, environ, start_response): + + if environ['PATH_INFO'].startswith(self.prefix): + environ['PATH_INFO'] = environ['PATH_INFO'][len(self.prefix):] + environ['SCRIPT_NAME'] = self.prefix + return self.app(environ, start_response) + else: + start_response('404', [('Content-Type', 'text/plain')]) + return ["This url does not belong to the app.".encode()] \ No newline at end of file diff --git a/setup.py b/setup.py index 7467f60..d5b7a02 100644 --- a/setup.py +++ b/setup.py @@ -8,6 +8,7 @@ setup( zip_safe=False, install_requires=[ 'flask', - 'shortuuid' + 'shortuuid', + 'python-dotenv' ], )