From 7ab471fe498e0962614719d2d539a572671b4ac2 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 2 Dec 2016 20:43:39 +0200 Subject: [PATCH] Update services miniapp with configuration provider --- README.rst | 16 +++++++++++----- examples/miniapps/services/containers.py | 10 ++++++---- examples/miniapps/services/run.py | 6 +++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index 3952a390..2d25d6f3 100644 --- a/README.rst +++ b/README.rst @@ -126,13 +126,15 @@ several IoC containers for some example application: class Platform(containers.DeclarativeContainer): """IoC container of platform service providers.""" + configuration = providers.Configuration('config') + logger = providers.Singleton(logging.Logger, name='example') - database = providers.Singleton(sqlite3.connect, ':memory:') + database = providers.Singleton(sqlite3.connect, configuration.database.dsn) s3 = providers.Singleton(boto.s3.connection.S3Connection, - aws_access_key_id='KEY', - aws_secret_access_key='SECRET') + configuration.aws.access_key_id, + configuration.aws.secret_access_key) class Services(containers.DeclarativeContainer): @@ -145,7 +147,7 @@ several IoC containers for some example application: auth = providers.Factory(example.services.AuthService, logger=Platform.logger, db=Platform.database, - token_ttl=3600) + token_ttl=Platform.configuration.auth.token_ttl) photos = providers.Factory(example.services.PhotosService, logger=Platform.logger, @@ -174,7 +176,11 @@ Next example demonstrates run of example application defined above: if __name__ == '__main__': - # Configure platform logger: + # Configure platform: + Platform.configuration.update({'database': {'dsn': ':memory:'}, + 'aws': {'access_key_id': 'KEY', + 'secret_access_key': 'SECRET'}, + 'auth': {'token_ttl': 3600}}) Platform.logger().addHandler(logging.StreamHandler(sys.stdout)) # Run application: diff --git a/examples/miniapps/services/containers.py b/examples/miniapps/services/containers.py index 4beb12cd..a8872c4a 100644 --- a/examples/miniapps/services/containers.py +++ b/examples/miniapps/services/containers.py @@ -15,13 +15,15 @@ import dependency_injector.providers as providers class Platform(containers.DeclarativeContainer): """IoC container of platform service providers.""" + configuration = providers.Configuration('config') + logger = providers.Singleton(logging.Logger, name='example') - database = providers.Singleton(sqlite3.connect, ':memory:') + database = providers.Singleton(sqlite3.connect, configuration.database.dsn) s3 = providers.Singleton(boto.s3.connection.S3Connection, - aws_access_key_id='KEY', - aws_secret_access_key='SECRET') + configuration.aws.access_key_id, + configuration.aws.secret_access_key) class Services(containers.DeclarativeContainer): @@ -34,7 +36,7 @@ class Services(containers.DeclarativeContainer): auth = providers.Factory(example.services.AuthService, logger=Platform.logger, db=Platform.database, - token_ttl=3600) + token_ttl=Platform.configuration.auth.token_ttl) photos = providers.Factory(example.services.PhotosService, logger=Platform.logger, diff --git a/examples/miniapps/services/run.py b/examples/miniapps/services/run.py index e416f5b8..662fb684 100644 --- a/examples/miniapps/services/run.py +++ b/examples/miniapps/services/run.py @@ -7,7 +7,11 @@ from containers import Platform, Application if __name__ == '__main__': - # Configure platform logger: + # Configure platform: + Platform.configuration.update({'database': {'dsn': ':memory:'}, + 'aws': {'access_key_id': 'KEY', + 'secret_access_key': 'SECRET'}, + 'auth': {'token_ttl': 3600}}) Platform.logger().addHandler(logging.StreamHandler(sys.stdout)) # Run application: