Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to read environment variables #51

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ The project runs on Python 3.
3. Install all the dependencies in `requirements.txt` file:
`pip install -r requirements.txt`

4. Run the app:
4. Export the following environment variables:

```
export SECRET_KEY=<your-secret-key>
```

5. Run the app:
`python run.py`

5. When you are done using the app, decativate the virtual environment:
6. When you are done using the app, deactivate the virtual environment:
`deactivate`


### Run tests

To run the unitests type this on the terminal:
Expand Down
22 changes: 21 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from datetime import timedelta
import os


class BaseConfig(object):
DEBUG = False
TESTING = False
SECRET_KEY = 'EXAMPLE_SECRET_KEY'

SECRET_KEY = os.getenv('SECRET_KEY')

# SQLAlchemy settings
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
Expand All @@ -16,21 +18,39 @@ class BaseConfig(object):


class ProductionConfig(BaseConfig):
ENV = 'production'

# SQLALCHEMY_DATABASE_URI = 'mysql://user@localhost/foo'
SQLALCHEMY_DATABASE_URI = 'sqlite:///prod_data.db'


class DevelopmentConfig(BaseConfig):
ENV = 'development'
DEBUG = True

SQLALCHEMY_DATABASE_URI = 'sqlite:///dev_data.db'


class TestingConfig(BaseConfig):
ENV = 'testing'
DEBUG = True
TESTING = True

# Use in-memory SQLite database for testing
SQLALCHEMY_DATABASE_URI = 'sqlite://'
# SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
# SQLALCHEMY_DATABASE_URI = 'sqlite:///test_data.db'


def get_env_config():
flask_config_name = os.getenv('FLASK_ENVIRONMENT_CONFIG', 'dev')
if flask_config_name not in ['prod', 'test', 'dev']:
raise ValueError('The environment config value has to be within these values: prod, dev, test.')
return CONFIGURATION_MAPPER[flask_config_name]


CONFIGURATION_MAPPER = {
'dev': 'config.DevelopmentConfig',
'test': 'config.TestingConfig',
'prod': 'config.ProductionConfig'
}
13 changes: 2 additions & 11 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
from flask_jwt import JWT
from app.security import authenticate, identity
from datetime import datetime

CONFIG_NAME_MAPPER = {
'development': 'config.DevelopmentConfig',
'testing': 'config.TestingConfig',
'production': 'config.ProductionConfig'
}
from config import get_env_config

application = Flask(__name__)

# setup application environment
flask_config_name = os.getenv('FLASK_CONFIG')
if flask_config_name is None:
flask_config_name = 'development'

application.config.from_object(CONFIG_NAME_MAPPER[flask_config_name])
application.config.from_object(get_env_config())

api = Api(
app=application,
Expand Down
30 changes: 15 additions & 15 deletions tests/test_app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ def create_app(self):
return application

def test_app_testing_config(self):
self.assertEqual(application.config['SECRET_KEY'], 'EXAMPLE_SECRET_KEY')
self.assertIsNone(application.config['SECRET_KEY'])
self.assertTrue(application.config['DEBUG'])
self.assertTrue(application.config['TESTING'])
self.assertFalse(application.config['SQLALCHEMY_TRACK_MODIFICATIONS'])
self.assertEqual(application.config['SQLALCHEMY_DATABASE_URI'], 'sqlite://')
self.assertFalse(current_app is None)
self.assertEqual('sqlite://', application.config['SQLALCHEMY_DATABASE_URI'])
self.assertIsNotNone(current_app)

# testing JWT configurations
self.assertTrue(application.config['JWT_AUTH_URL_RULE'] == '/login')
self.assertEqual(application.config['JWT_EXPIRATION_DELTA'], timedelta(weeks=1))
self.assertEqual('/login', application.config['JWT_AUTH_URL_RULE'])
self.assertEqual(timedelta(weeks=1), application.config['JWT_EXPIRATION_DELTA'])


class TestDevelopmentConfig(TestCase):
Expand All @@ -30,16 +30,16 @@ def create_app(self):
return application

def test_app_development_config(self):
self.assertEqual(application.config['SECRET_KEY'], 'EXAMPLE_SECRET_KEY')
self.assertIsNone(application.config['SECRET_KEY'])
self.assertTrue(application.config['DEBUG'])
self.assertFalse(application.config['TESTING'])
self.assertFalse(application.config['SQLALCHEMY_TRACK_MODIFICATIONS'])
self.assertEqual(application.config['SQLALCHEMY_DATABASE_URI'], 'sqlite:///dev_data.db')
self.assertFalse(current_app is None)
self.assertEqual('sqlite:///dev_data.db', application.config['SQLALCHEMY_DATABASE_URI'])
self.assertIsNotNone(current_app)

# testing JWT configurations
self.assertEqual(application.config['JWT_AUTH_URL_RULE'], '/login')
self.assertEqual(application.config['JWT_EXPIRATION_DELTA'], timedelta(weeks=1))
self.assertEqual('/login', application.config['JWT_AUTH_URL_RULE'])
self.assertEqual(timedelta(weeks=1), application.config['JWT_EXPIRATION_DELTA'])


class TestProductionConfig(TestCase):
Expand All @@ -48,16 +48,16 @@ def create_app(self):
return application

def test_app_production_config(self):
self.assertEqual(application.config['SECRET_KEY'], 'EXAMPLE_SECRET_KEY')
self.assertIsNone(application.config['SECRET_KEY'])
self.assertFalse(application.config['DEBUG'])
self.assertFalse(application.config['TESTING'])
self.assertFalse(application.config['SQLALCHEMY_TRACK_MODIFICATIONS'])
self.assertEqual(application.config['SQLALCHEMY_DATABASE_URI'], 'sqlite:///prod_data.db')
self.assertFalse(current_app is None)
self.assertEqual('sqlite:///prod_data.db', application.config['SQLALCHEMY_DATABASE_URI'])
self.assertIsNotNone(current_app)

# testing JWT configurations
self.assertTrue(application.config['JWT_AUTH_URL_RULE'] == '/login')
self.assertEqual(application.config['JWT_EXPIRATION_DELTA'], timedelta(weeks=1))
self.assertEqual('/login', application.config['JWT_AUTH_URL_RULE'])
self.assertEqual(timedelta(weeks=1), application.config['JWT_EXPIRATION_DELTA'])


if __name__ == '__main__':
Expand Down