I'm trying to get CORS working. A workaround works, however not on all api's. So I would like to have the regular way of working working. Which is now having me stuck here already for a while. I tried initializing CORS like this:
flask_app = Flask(__name__)
CORS(flask_app)
Which gives me the following error:
return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'
The complete error is:
2021-12-18 16:30:57 default[20211218t172924] Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker worker.init_process()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process self.load_wsgi()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi self.wsgi = self.app.wsgi()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load return self.load_wsgiapp()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp return util.import_app(self.app_uri)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/gunicorn/util.py", line 412, in import_app app = app(*args, **kwargs)
File "/workspace/app.py", line 38, in get_flask_app CORS(flask_app)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 59, in __init__ self.init_app(app, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/extension.py", line 81, in init_app getLogger(app).info("Configuring CORS with resources: %s", resources_human)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask_cors/core.py", line 356, in getLogger
return logging.getLogger("%s.cors" % app.logger_name) AttributeError: 'Flask' object has no attribute 'logger_name'
When I remove the CORS(flask_app), everything is fine. Except for the CORS issues I would like to fix.
This is my complete app.py:
# flask packages
from flask import Flask, request, app
from flask_cors import CORS
from flask_restful import Api
from flask_mongoengine import MongoEngine
from flask_jwt_extended import JWTManager
import logging
# local packages
from api.routes import create_routes
# external packages
import os
# default mongodb configuration
default_config = {'MONGODB_SETTINGS': {
'db': '(....)',
'host': '(....)',
'port': 27017,
'username': 'admin',
'password': 'password',
'authentication_source': 'admin'},
'JWT_SECRET_KEY': '(...)'}
def get_flask_app(config: dict = None) -> app.Flask:
"""
Initializes Flask app with given configuration.
Main entry point for wsgi (gunicorn) server.
:param config: Configuration dictionary
:return: app
"""
# init flask
logging.getLogger('flask_cors').level = logging.DEBUG
flask_app = Flask(__name__)
CORS(flask_app)
# configure app
config = default_config if config is None else config
flask_app.config.update(config)
# load config variables
if 'MONGODB_URI' in os.environ:
flask_app.config['MONGODB_SETTINGS'] = {'host': os.environ['MONGODB_URI'],
'retryWrites': False}
if 'JWT_SECRET_KEY' in os.environ:
flask_app.config['JWT_SECRET_KEY'] = os.environ['JWT_SECRET_KEY']
# init api and routes
api = Api(app=flask_app)
create_routes(api=api)
# init mongoengine
db = MongoEngine(app=flask_app)
# init jwt manager
jwt = JWTManager(app=flask_app)
return flask_app
if __name__ == '__main__':
# Main entry point when run in stand-alone mode.
app = get_flask_app()
app.run(host='0.0.0.0', port=8080)
Using Google Cloud Platform. Could this be the key to my issue? My app.yaml is:
runtime: python38
entrypoint: gunicorn -b :$PORT 'app:get_flask_app()'
I will try to see what happens locally, maybe it's solvable easier here. Thinking about when re-typing the question here.
Already updated my Flask-Cors (flask-cors). Tried a lot of things, nothing worked, hence this question.
Help is much appreciated / thanks!!
Adding this part:
Flask.logger_name = "listlogger"
Ultimately solved it for me. Now it was able to find logger_name. That listlogger is just a random name.
I added it right before opening the app + CORS(app):
Flask.logger_name = "listlogger"
flask_app = Flask(__name__)
CORS(flask_app)