Commit 76e12c62066dc0d2b2b435f722fd43778dad0e16

Authored by Antoine Goutenoir
1 parent c2a01bd2
Exists in master

Continue tackling issues with distributed configuration.

.env.dist
1 1 # Create a sibling `.env` file from this file, and customize it.
2 2 # `export ` prefixes are ignored and allow bash sourcing of this file.
  3 +# Do not use True and False in here, but 1 and 0, respectively.
  4 +
3 5  
4 6 # Secret for Flask
5 7 export FLASK_SECRET="IAMN0TSECRETCHANGEM3"
  8 +export SECRET_KEY="IAMN0TSECRETCH4NGEM3"
  9 +
6 10  
7 11 # Credentials for the restricted sections
8 12 export ADMIN_USERNAME="admin"
9 13 export ADMIN_PASSWORD="admin"
10 14  
  15 +
  16 +# Database
  17 +#export SQLALCHEMY_DATABASE_URI="sqlite:///../database_prod.db"
  18 +#export SQLALCHEMY_TRACK_MODIFICATIONS=0
  19 +
  20 +
11 21 # Mail sending configuration
12 22 export MAIL_SERVER="localhost"
13 23 export MAIL_PORT=25
... ...
flaskr/__init__.py
... ... @@ -3,8 +3,10 @@ import os
3 3 from markdown import markdown
4 4 from dotenv import load_dotenv, find_dotenv, dotenv_values
5 5 # Load config from .env ; do this before local libs (and flask)
6   -load_dotenv(find_dotenv(), override=True) # write into OS environment
7   -local_env = dotenv_values(find_dotenv()) # load it as well to inject it later
  6 +# 1. Write into OS environment -- if this fails you forgot to create .env file
  7 +load_dotenv(find_dotenv(raise_error_if_not_found=True), override=True)
  8 +# 2. Load it as well to inject it later into app.config
  9 +local_env = dotenv_values(find_dotenv())
8 10  
9 11  
10 12 from flask import Flask, url_for
... ... @@ -44,12 +46,10 @@ def create_app(object_name):
44 46 if type(object_name) == ScriptInfo:
45 47 object_name = 'flaskr.settings.DevelopmentConfig'
46 48  
47   - # Load configuration
  49 + # Load the configuration
48 50 app.config.from_object(object_name)
49   - if local_env:
50   - app.config.update(**local_env)
51   - else:
52   - pass # FIXME
  51 + app.config.update(**local_env)
  52 +
53 53 app.config['BASIC_AUTH_USERNAME'] = os.getenv('ADMIN_USERNAME')
54 54 app.config['BASIC_AUTH_PASSWORD'] = os.getenv('ADMIN_PASSWORD')
55 55  
... ...
flaskr/settings.py
1 1 import tempfile
2   -db_file = tempfile.NamedTemporaryFile()
  2 +
  3 +
  4 +# Configuration should be done in .env file
  5 +# Create it first:
  6 +# cp .env.dist .env
  7 +# And then edit it with your own sauce.
  8 +# The contents of .env will override values in here.
3 9  
4 10  
5 11 class Config(object):
6   - SECRET_KEY = 'TWOOXYGENONECARBON'
7   - # FLASK_RUN_EXTRA_FILES="content.yml"
  12 + FLASK_RUN_EXTRA_FILES="content.yml"
8 13  
9 14  
10 15 class ProductionConfig(Config):
11 16 ENV = 'production'
  17 +
12 18 SQLALCHEMY_DATABASE_URI = 'sqlite:///../database_prod.db'
13 19 SQLALCHEMY_TRACK_MODIFICATIONS = False
14 20  
15 21 CACHE_TYPE = 'simple'
16   - SECRET_KEY = 'REPLACE M3' # todo: use a .env file
17 22  
18 23  
19 24 class DevelopmentConfig(Config):
... ... @@ -28,12 +33,15 @@ class DevelopmentConfig(Config):
28 33 ASSETS_DEBUG = True
29 34  
30 35  
  36 +db_file = tempfile.NamedTemporaryFile()
  37 +
  38 +
31 39 class TestConfig(Config):
32 40 ENV = 'test'
33 41 DEBUG = True
34 42 DEBUG_TB_INTERCEPT_REDIRECTS = False
35 43  
36   - SQLALCHEMY_DATABASE_URI = 'sqlite:///' + db_file.name
  44 + SQLALCHEMY_DATABASE_URI = 'sqlite:///../%s' % db_file.name
37 45 SQLALCHEMY_ECHO = True
38 46 SQLALCHEMY_TRACK_MODIFICATIONS = False
39 47  
... ...
tests/test_config.py deleted
... ... @@ -1,31 +0,0 @@
1   -#! ../env/bin/python
2   -# -*- coding: utf-8 -*-
3   -from flaskr import create_app
4   -
5   -
6   -class TestConfig:
7   - def test_dev_config(self):
8   - """ Tests if the development config loads correctly """
9   -
10   - app = create_app('flaskr.settings.DevConfig')
11   -
12   - assert app.config['DEBUG'] is True
13   - assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///../database.db'
14   - assert app.config['CACHE_TYPE'] == 'null'
15   -
16   - def test_test_config(self):
17   - """ Tests if the test config loads correctly """
18   -
19   - app = create_app('flaskr.settings.TestConfig')
20   -
21   - assert app.config['DEBUG'] is True
22   - assert app.config['SQLALCHEMY_ECHO'] is True
23   - assert app.config['CACHE_TYPE'] == 'null'
24   -
25   - def test_prod_config(self):
26   - """ Tests if the production config loads correctly """
27   -
28   - app = create_app('flaskr.settings.ProdConfig')
29   -
30   - assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///../database.db'
31   - assert app.config['CACHE_TYPE'] == 'simple'