diff --git a/requirements.txt b/requirements.txt index 764db5e..20eb977 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ flask-redis marshmallow flask-marshmallow spotipy -pycryptodome \ No newline at end of file +pycryptodome +flask-cors \ No newline at end of file diff --git a/src/app.py b/src/app.py index fccb6e8..8d92eda 100644 --- a/src/app.py +++ b/src/app.py @@ -1,15 +1,17 @@ #!/usr/bin/env python3 import logging +import musicbrainzngs from flask import Flask from flask_restful import Api +from flask_cors import CORS import sentry_sdk from sentry_sdk.integrations.flask import FlaskIntegration from healthcheck import HealthCheck from marshm import ma from fred import flaskred -from config import SENTRY_DSN, RELEASEMODE, RELEASE_ID, PORT, DEBUG, REDIS_URL +from config import SENTRY_DSN, RELEASEMODE, RELEASE_ID, PORT, DEBUG, REDIS_URL, ALLOWED_ORIGINS from errorhandlers import register_all_error_handlers from resources import LoginApi, ListsApi, MeApi, SingleListApi, TrackApi @@ -41,11 +43,20 @@ api = Api(app) health = HealthCheck() ma.init_app(app) flaskred.init_app(app) +CORS(app, origins=ALLOWED_ORIGINS) formatter = logging.Formatter( fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" ) + +@app.before_first_request +def before_first_request(): + musicbrainzngs.set_useragent("onSpot", 1) + musicbrainzngs.set_rate_limit(1.0, 20) + musicbrainzngs.https = True + + handler = logging.StreamHandler() handler.setFormatter(formatter) diff --git a/src/config.py b/src/config.py index 79da882..3e7c3b7 100644 --- a/src/config.py +++ b/src/config.py @@ -14,6 +14,7 @@ __version__text__ = "1" PORT = os.environ.get("ONSPOT_PORT", 8080) DEBUG = os.environ.get("ONSPOT_DEBUG", True) +ALLOWED_ORIGINS = os.environ.get('ALLOWED_ORIGINS', '*') SENTRY_DSN = os.environ.get("SENTRY_DSN") RELEASE_ID = os.environ.get("RELEASE_ID", "test") diff --git a/src/resources.py b/src/resources.py index 026ad9d..47143ed 100644 --- a/src/resources.py +++ b/src/resources.py @@ -41,10 +41,8 @@ class LoginApi(Resource): try: musicbrainzngs.auth(userobj['name'], userobj['password']) - musicbrainzngs.set_useragent("onSpot", 1) - musicbrainzngs.set_rate_limit(1.0, 20) - musicbrainzngs.https = True - print(musicbrainzngs.get_collections()) + musicbrainzngs.get_collections() + musicbrainzngs.auth(None, None) except Exception as e: current_app.logger.warning(e) abort(401, "login denied to musicbrainz")