2018-06-22 09:52:58 +03:00
|
|
|
"""Example of dependency injection in Python."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
|
2018-07-02 16:43:23 +03:00
|
|
|
from dependency_injector import containers, providers
|
|
|
|
from example import services, main
|
2018-06-22 09:52:58 +03:00
|
|
|
|
|
|
|
|
|
|
|
class IocContainer(containers.DeclarativeContainer):
|
|
|
|
"""Application IoC container."""
|
|
|
|
|
|
|
|
config = providers.Configuration('config')
|
|
|
|
logger = providers.Singleton(logging.Logger, name='example')
|
|
|
|
|
|
|
|
# Gateways
|
|
|
|
|
|
|
|
database_client = providers.Singleton(sqlite3.connect, config.database.dsn)
|
|
|
|
|
|
|
|
s3_client = providers.Singleton(
|
|
|
|
boto3.client, 's3',
|
|
|
|
aws_access_key_id=config.aws.access_key_id,
|
2018-07-03 12:39:43 +03:00
|
|
|
aws_secret_access_key=config.aws.secret_access_key,
|
2018-07-02 16:43:23 +03:00
|
|
|
)
|
2018-06-22 09:52:58 +03:00
|
|
|
|
|
|
|
# Services
|
|
|
|
|
|
|
|
users_service = providers.Factory(
|
2018-07-02 16:43:23 +03:00
|
|
|
services.UsersService,
|
2018-06-22 09:52:58 +03:00
|
|
|
db=database_client,
|
2018-07-03 12:39:43 +03:00
|
|
|
logger=logger,
|
2018-07-02 16:43:23 +03:00
|
|
|
)
|
2018-06-22 09:52:58 +03:00
|
|
|
|
|
|
|
auth_service = providers.Factory(
|
2018-07-02 16:43:23 +03:00
|
|
|
services.AuthService,
|
2018-06-22 09:52:58 +03:00
|
|
|
token_ttl=config.auth.token_ttl,
|
|
|
|
db=database_client,
|
2018-07-03 12:39:43 +03:00
|
|
|
logger=logger,
|
2018-07-02 16:43:23 +03:00
|
|
|
)
|
2018-06-22 09:52:58 +03:00
|
|
|
|
|
|
|
photos_service = providers.Factory(
|
2018-07-02 16:43:23 +03:00
|
|
|
services.PhotosService,
|
2018-06-22 09:52:58 +03:00
|
|
|
db=database_client,
|
|
|
|
s3=s3_client,
|
2018-07-03 12:39:43 +03:00
|
|
|
logger=logger,
|
2018-07-02 16:43:23 +03:00
|
|
|
)
|
2018-06-22 09:52:58 +03:00
|
|
|
|
|
|
|
# Misc
|
|
|
|
|
|
|
|
main = providers.Callable(
|
2018-07-02 16:43:23 +03:00
|
|
|
main.main,
|
2018-06-22 09:52:58 +03:00
|
|
|
users_service=users_service,
|
|
|
|
auth_service=auth_service,
|
2018-07-03 12:39:43 +03:00
|
|
|
photos_service=photos_service,
|
2018-07-02 16:43:23 +03:00
|
|
|
)
|