Update services miniapp with configuration provider

This commit is contained in:
Roman Mogilatov 2016-12-02 20:43:39 +02:00
parent e03729958d
commit 7ab471fe49
3 changed files with 22 additions and 10 deletions

View File

@ -126,13 +126,15 @@ several IoC containers for some example application:
class Platform(containers.DeclarativeContainer): class Platform(containers.DeclarativeContainer):
"""IoC container of platform service providers.""" """IoC container of platform service providers."""
configuration = providers.Configuration('config')
logger = providers.Singleton(logging.Logger, name='example') 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, s3 = providers.Singleton(boto.s3.connection.S3Connection,
aws_access_key_id='KEY', configuration.aws.access_key_id,
aws_secret_access_key='SECRET') configuration.aws.secret_access_key)
class Services(containers.DeclarativeContainer): class Services(containers.DeclarativeContainer):
@ -145,7 +147,7 @@ several IoC containers for some example application:
auth = providers.Factory(example.services.AuthService, auth = providers.Factory(example.services.AuthService,
logger=Platform.logger, logger=Platform.logger,
db=Platform.database, db=Platform.database,
token_ttl=3600) token_ttl=Platform.configuration.auth.token_ttl)
photos = providers.Factory(example.services.PhotosService, photos = providers.Factory(example.services.PhotosService,
logger=Platform.logger, logger=Platform.logger,
@ -174,7 +176,11 @@ Next example demonstrates run of example application defined above:
if __name__ == '__main__': 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)) Platform.logger().addHandler(logging.StreamHandler(sys.stdout))
# Run application: # Run application:

View File

@ -15,13 +15,15 @@ import dependency_injector.providers as providers
class Platform(containers.DeclarativeContainer): class Platform(containers.DeclarativeContainer):
"""IoC container of platform service providers.""" """IoC container of platform service providers."""
configuration = providers.Configuration('config')
logger = providers.Singleton(logging.Logger, name='example') 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, s3 = providers.Singleton(boto.s3.connection.S3Connection,
aws_access_key_id='KEY', configuration.aws.access_key_id,
aws_secret_access_key='SECRET') configuration.aws.secret_access_key)
class Services(containers.DeclarativeContainer): class Services(containers.DeclarativeContainer):
@ -34,7 +36,7 @@ class Services(containers.DeclarativeContainer):
auth = providers.Factory(example.services.AuthService, auth = providers.Factory(example.services.AuthService,
logger=Platform.logger, logger=Platform.logger,
db=Platform.database, db=Platform.database,
token_ttl=3600) token_ttl=Platform.configuration.auth.token_ttl)
photos = providers.Factory(example.services.PhotosService, photos = providers.Factory(example.services.PhotosService,
logger=Platform.logger, logger=Platform.logger,

View File

@ -7,7 +7,11 @@ from containers import Platform, Application
if __name__ == '__main__': 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)) Platform.logger().addHandler(logging.StreamHandler(sys.stdout))
# Run application: # Run application: