Change code layout of services_v2 miniapp

This commit is contained in:
Roman Mogylatov 2018-07-02 16:43:23 +03:00
parent a8dd335d71
commit f1c1a4ed0c
3 changed files with 62 additions and 44 deletions

View File

@ -285,11 +285,8 @@ great opportunity to control & manage application's structure in one place.
import boto3 import boto3
import example.main from dependency_injector import containers, providers
import example.services from example import services, main
import dependency_injector.containers as containers
import dependency_injector.providers as providers
class IocContainer(containers.DeclarativeContainer): class IocContainer(containers.DeclarativeContainer):
@ -305,34 +302,39 @@ great opportunity to control & manage application's structure in one place.
s3_client = providers.Singleton( s3_client = providers.Singleton(
boto3.client, 's3', boto3.client, 's3',
aws_access_key_id=config.aws.access_key_id, aws_access_key_id=config.aws.access_key_id,
aws_secret_access_key=config.aws.secret_access_key) aws_secret_access_key=config.aws.secret_access_key
)
# Services # Services
users_service = providers.Factory( users_service = providers.Factory(
example.services.UsersService, services.UsersService,
db=database_client, db=database_client,
logger=logger) logger=logger
)
auth_service = providers.Factory( auth_service = providers.Factory(
example.services.AuthService, services.AuthService,
token_ttl=config.auth.token_ttl, token_ttl=config.auth.token_ttl,
db=database_client, db=database_client,
logger=logger) logger=logger
)
photos_service = providers.Factory( photos_service = providers.Factory(
example.services.PhotosService, services.PhotosService,
db=database_client, db=database_client,
s3=s3_client, s3=s3_client,
logger=logger) logger=logger
)
# Misc # Misc
main = providers.Callable( main = providers.Callable(
example.main.main, main.main,
users_service=users_service, users_service=users_service,
auth_service=auth_service, auth_service=auth_service,
photos_service=photos_service) photos_service=photos_service
)
Next example demonstrates run of example application defined above: Next example demonstrates run of example application defined above:
@ -347,18 +349,25 @@ Next example demonstrates run of example application defined above:
if __name__ == '__main__': if __name__ == '__main__':
# Configure platform: # Configure container:
container = IocContainer( container = IocContainer(
config={'database': {'dsn': ':memory:'}, config={
'aws': {'access_key_id': 'KEY', 'database': {
'secret_access_key': 'SECRET'}, 'dsn': ':memory:'
'auth': {'token_ttl': 3600}}) },
'aws': {
'access_key_id': 'KEY',
'secret_access_key': 'SECRET'
},
'auth': {
'token_ttl': 3600
}
}
)
container.logger().addHandler(logging.StreamHandler(sys.stdout)) container.logger().addHandler(logging.StreamHandler(sys.stdout))
# Run application: # Run application:
container.main(uid=sys.argv[1], container.main(*sys.argv[1:])
password=sys.argv[2],
photo=sys.argv[3])
You can get more *Dependency Injector* examples in ``/examples`` directory on You can get more *Dependency Injector* examples in ``/examples`` directory on
GitHub: GitHub:

View File

@ -5,11 +5,8 @@ import sqlite3
import boto3 import boto3
import example.main from dependency_injector import containers, providers
import example.services from example import services, main
import dependency_injector.containers as containers
import dependency_injector.providers as providers
class IocContainer(containers.DeclarativeContainer): class IocContainer(containers.DeclarativeContainer):
@ -25,31 +22,36 @@ class IocContainer(containers.DeclarativeContainer):
s3_client = providers.Singleton( s3_client = providers.Singleton(
boto3.client, 's3', boto3.client, 's3',
aws_access_key_id=config.aws.access_key_id, aws_access_key_id=config.aws.access_key_id,
aws_secret_access_key=config.aws.secret_access_key) aws_secret_access_key=config.aws.secret_access_key
)
# Services # Services
users_service = providers.Factory( users_service = providers.Factory(
example.services.UsersService, services.UsersService,
db=database_client, db=database_client,
logger=logger) logger=logger
)
auth_service = providers.Factory( auth_service = providers.Factory(
example.services.AuthService, services.AuthService,
token_ttl=config.auth.token_ttl, token_ttl=config.auth.token_ttl,
db=database_client, db=database_client,
logger=logger) logger=logger
)
photos_service = providers.Factory( photos_service = providers.Factory(
example.services.PhotosService, services.PhotosService,
db=database_client, db=database_client,
s3=s3_client, s3=s3_client,
logger=logger) logger=logger
)
# Misc # Misc
main = providers.Callable( main = providers.Callable(
example.main.main, main.main,
users_service=users_service, users_service=users_service,
auth_service=auth_service, auth_service=auth_service,
photos_service=photos_service) photos_service=photos_service
)

View File

@ -7,15 +7,22 @@ from container import IocContainer
if __name__ == '__main__': if __name__ == '__main__':
# Configure platform: # Configure container:
container = IocContainer( container = IocContainer(
config={'database': {'dsn': ':memory:'}, config={
'aws': {'access_key_id': 'KEY', 'database': {
'secret_access_key': 'SECRET'}, 'dsn': ':memory:'
'auth': {'token_ttl': 3600}}) },
'aws': {
'access_key_id': 'KEY',
'secret_access_key': 'SECRET'
},
'auth': {
'token_ttl': 3600
}
}
)
container.logger().addHandler(logging.StreamHandler(sys.stdout)) container.logger().addHandler(logging.StreamHandler(sys.stdout))
# Run application: # Run application:
container.main(uid=sys.argv[1], container.main(*sys.argv[1:])
password=sys.argv[2],
photo=sys.argv[3])