nickjj / flask-static-digest

Flask extension to help make your static files production ready by md5 tagging and gzipping them.
MIT License
157 stars 27 forks source link

Cannot read .env variable due to Flask static digest #8

Closed mick-net closed 4 years ago

mick-net commented 4 years ago

Hi Nick,

I'm using your Build A SaaS app with Flask framework. The course was great. I build my own API app on top of it. I started adding extra environment parameters to the .env file and to the settings.py. I can build the web docker container when Flask is in 'development'. However when I set Flask to 'production' than I get the following error:

 ---> 374360990a86
Step 13/15 : RUN if [ "${FLASK_ENV}" != "development" ]; then   flask digest compile; fi
 ---> Running in 69f4c0d52b38
 * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them.
Traceback (most recent call last):
  File "/usr/local/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 425, in decorator
    with __ctx.ensure_object(ScriptInfo).load_app().app_context():
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/app/snakeeyes/app.py", line 23, in <module>
    from snakeeyes.blueprints.admin import admin
  File "/app/snakeeyes/blueprints/admin/__init__.py", line 1, in <module>
    from snakeeyes.blueprints.admin.views import admin
  File "/app/snakeeyes/blueprints/admin/views.py", line 11, in <module>
    from snakeeyes.blueprints.admin.models import Dashboard
  File "/app/snakeeyes/blueprints/admin/models.py", line 3, in <module>
    from snakeeyes.blueprints.user.models import db, User
  File "/app/snakeeyes/blueprints/user/__init__.py", line 1, in <module>
    from snakeeyes.blueprints.user.views import user
  File "/app/snakeeyes/blueprints/user/views.py", line 18, in <module>
    from snakeeyes.blueprints.user.models import User
  File "/app/snakeeyes/blueprints/user/models.py", line 17, in <module>
    from snakeeyes.blueprints.billing.models.credit_card import CreditCard
  File "/app/snakeeyes/blueprints/billing/__init__.py", line 1, in <module>
    from snakeeyes.blueprints.billing.views.billing import billing
  File "/app/snakeeyes/blueprints/billing/views/billing.py", line 14, in <module>
    from config import settings
  File "/app/config/settings.py", line 193, in <module>
    'MONEYBIRD_TAX_RATE_ID_NO_VAT', None))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
ERROR: Service 'web' failed to build: The command '/bin/sh -c if [ "${FLASK_ENV}" != "development" ]; then   flask digest compile; fi' returned a non-zero code: 1

If I comment out the code below in the dockerfile, it builds again in production:

RUN if [ "${FLASK_ENV}" != "development" ]; then \
  flask digest compile; fi

Do you have any suggestions?

Version: Flask-Static-Digest==0.1.2

mick-net commented 4 years ago

Could this have anything to do with the app_context?

in billing/models/invoice.py I added:

app = Flask(__name__)
app.config.from_object('config.settings')

....

 with app.app_context():
            moneybird_tax_rate_id_no_vat = \
                app.config.get("MONEYBIRD_TAX_RATE_ID_NO_VAT")

I did this since I wasn't able to use Flask's current_app:

  raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
nickjj commented 4 years ago

Hi,

What happens if you put in a dummy string value instead of None for that env's default value?

At Docker build time the .env file will not be available since we're not copying it in (that's because it's being ignored in the .dockerignore file which is good) so our options are:

  1. Pick reasonable default values for the env variables that require being present to not blow up in your settings.py file (such as your custom env variable since it's converting it to an int),

  2. At build time, pass in non-secret dummy values into the build args for each required env variable (similar to what we do with FLASK_ENV) which will let things build, but then at run-time those will be overwritten with our real .env file's values.

mick-net commented 4 years ago

Thanks for the quick reply. That worked.