From c8b491bd0beba8ac108aef3e4edddf9868934755 Mon Sep 17 00:00:00 2001 From: marcsello Date: Fri, 27 Nov 2020 23:57:21 +0100 Subject: [PATCH] Did stuff with the config --- requirements.txt | 3 ++- src/app.py | 36 ++++++++---------------------------- src/utils/__init__.py | 1 + src/utils/config.py | 21 ++++++++++++--------- src/views/itemview.py | 3 --- src/views/profileview.py | 3 --- src/views/uploadview.py | 3 --- 7 files changed, 23 insertions(+), 47 deletions(-) diff --git a/requirements.txt b/requirements.txt index 916d750..bd5763f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,5 @@ psycopg2-binary email_validator minio flask-minio -bleach \ No newline at end of file +bleach +bcrypt \ No newline at end of file diff --git a/src/app.py b/src/app.py index b85857e..fddc355 100644 --- a/src/app.py +++ b/src/app.py @@ -7,7 +7,7 @@ from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration from healthcheck import HealthCheck from flask_cors import CORS -from utils.config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG, SECRET_KEY, ALLOWED_ORIGINS, SQLALCHEMY_URI +from utils import Config from utils import health_database_status, security, user_datastore from views import ItemView, ProfileView, UploadView, IndexView @@ -22,38 +22,25 @@ __copyright__ = "Copyright 2020, UnstableVortex Team" __module_name__ = "app" __version__text__ = "1" -if SENTRY_DSN: +# The Flask application is not loaded yet, so we are accessing directly to the configuration values +if Config.SENTRY_DSN: sentry_sdk.init( - dsn=SENTRY_DSN, + dsn=Config.SENTRY_DSN, integrations=[FlaskIntegration(), SqlalchemyIntegration()], traces_sample_rate=1.0, send_default_pii=True, - release=RELEASE_ID, - environment=RELEASEMODE, + release=Config.RELEASE_ID, + environment=Config.RELEASEMODE, _experiments={"auto_enabling_integrations": True} ) app = Flask(__name__) -app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_URI -app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False -app.config['SECRET_KEY'] = SECRET_KEY -app.config['SECURITY_REGISTERABLE'] = True +app.config.from_object(Config) health = HealthCheck() db.init_app(app) security.init_app(app, user_datastore) -CORS(app, origins=ALLOWED_ORIGINS) - -formatter = logging.Formatter( - fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" -) - -handler = logging.StreamHandler() -handler.setFormatter(formatter) - -logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) -logger.addHandler(handler) +CORS(app) for view in [ItemView, ProfileView, UploadView, IndexView]: view.register(app, trailing_slash=False) @@ -65,10 +52,3 @@ app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run()) @app.before_first_request def init_db(): db.create_all() - - -if __name__ == "__main__": - app.run( - debug=bool(DEBUG), - port=int(PORT), - ) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 9c6427a..0bbc965 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,2 +1,3 @@ from .healthchecks import health_database_status from .security import security, user_datastore +from .config import Config \ No newline at end of file diff --git a/src/utils/config.py b/src/utils/config.py index 13ffdc5..69686e9 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -5,21 +5,24 @@ import os Configuration """ - __author__ = "@tormakris" __copyright__ = "Copyright 2020, Birbnetes Team" __module_name__ = "config" __version__text__ = "1" -PORT = os.environ.get("PORT", 8080) -DEBUG = os.environ.get("DEBUG", True) +class Config: + SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI", "sqlite://") + SECRET_KEY = os.environ.get("SECRET_KEY", os.urandom(12)) + CORS_ORIGINS = os.environ.get("ALLOWED_ORIGINS", "*") -SENTRY_DSN = os.environ.get("SENTRY_DSN") -RELEASE_ID = os.environ.get("RELEASE_ID", "test") -RELEASEMODE = os.environ.get("RELEASEMODE", "dev") + SENTRY_DSN = os.environ.get("SENTRY_DSN") + RELEASE_ID = os.environ.get("RELEASE_ID", "test") + RELEASEMODE = os.environ.get("RELEASEMODE", "dev") -SQLALCHEMY_URI = os.environ.get("SQLALCHEMY_URI", "sqlite://") + SECURITY_PASSWORD_SALT = os.environ.get("SECURITY_PASSWORD_SALT") # That's pepper actually -SECRET_KEY = os.getenv("SECRET_KEY") -ALLOWED_ORIGINS = os.environ.get('ALLOWED_ORIGINS', '*') + # Some constant configured stuff configs + SQLALCHEMY_TRACK_MODIFICATIONS = False + SECURITY_REGISTERABLE = True + SECURITY_PASSWORD_HASH = "bcrypt" diff --git a/src/views/itemview.py b/src/views/itemview.py index 787d9e4..5d01355 100644 --- a/src/views/itemview.py +++ b/src/views/itemview.py @@ -14,9 +14,6 @@ __version__text__ = "1" class ItemView(FlaskView): - route_prefix = "/item/" - route_base = '/' - def index(self): pass diff --git a/src/views/profileview.py b/src/views/profileview.py index adf5edf..81b3143 100644 --- a/src/views/profileview.py +++ b/src/views/profileview.py @@ -14,8 +14,5 @@ __version__text__ = "1" class ProfileView(FlaskView): - route_prefix = "/profile/" - route_base = '/' - def index(self): pass diff --git a/src/views/uploadview.py b/src/views/uploadview.py index 9ff1e0b..b1e1d53 100644 --- a/src/views/uploadview.py +++ b/src/views/uploadview.py @@ -14,8 +14,5 @@ __version__text__ = "1" class UploadView(FlaskView): - route_prefix = "/upload/" - route_base = '/' - def index(self): pass