|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import sentry_sdk |
| 5 | +from aiohttp import web |
| 6 | +from configmanager import Config |
| 7 | + |
| 8 | +import aleph.config |
| 9 | +from aleph.db.connection import make_engine, make_session_factory |
| 10 | +from aleph.services.cache.node_cache import NodeCache |
| 11 | +from aleph.services.ipfs import IpfsService |
| 12 | +from aleph.services.ipfs.common import make_ipfs_client |
| 13 | +from aleph.services.p2p import init_p2p_client |
| 14 | +from aleph.services.storage.fileystem_engine import FileSystemStorageEngine |
| 15 | +from aleph.storage import StorageService |
| 16 | +from aleph.toolkit.monitoring import setup_sentry |
| 17 | +from aleph.web import create_aiohttp_app |
| 18 | +from aleph.web.controllers.app_state_getters import ( |
| 19 | + APP_STATE_CONFIG, |
| 20 | + APP_STATE_MQ_CONN, |
| 21 | + APP_STATE_NODE_CACHE, |
| 22 | + APP_STATE_P2P_CLIENT, |
| 23 | + APP_STATE_SESSION_FACTORY, |
| 24 | + APP_STATE_STORAGE_SERVICE, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +async def configure_aiohttp_app( |
| 29 | + config: Config, |
| 30 | +) -> web.Application: |
| 31 | + with sentry_sdk.start_transaction(name=f"init-api-server"): |
| 32 | + p2p_client = await init_p2p_client(config, service_name=f"api-server-aiohttp") |
| 33 | + |
| 34 | + engine = make_engine( |
| 35 | + config, |
| 36 | + echo=config.logging.level.value == logging.DEBUG, |
| 37 | + application_name=f"aleph-api", |
| 38 | + ) |
| 39 | + session_factory = make_session_factory(engine) |
| 40 | + |
| 41 | + node_cache = NodeCache( |
| 42 | + redis_host=config.redis.host.value, redis_port=config.redis.port.value |
| 43 | + ) |
| 44 | + |
| 45 | + ipfs_client = make_ipfs_client(config) |
| 46 | + ipfs_service = IpfsService(ipfs_client=ipfs_client) |
| 47 | + storage_service = StorageService( |
| 48 | + storage_engine=FileSystemStorageEngine(folder=config.storage.folder.value), |
| 49 | + ipfs_service=ipfs_service, |
| 50 | + node_cache=node_cache, |
| 51 | + ) |
| 52 | + |
| 53 | + app = create_aiohttp_app() |
| 54 | + |
| 55 | + app[APP_STATE_CONFIG] = config |
| 56 | + app[APP_STATE_P2P_CLIENT] = p2p_client |
| 57 | + # Reuse the connection of the P2P client to avoid opening two connections |
| 58 | + app[APP_STATE_MQ_CONN] = p2p_client.mq_client.connection |
| 59 | + app[APP_STATE_NODE_CACHE] = node_cache |
| 60 | + app[APP_STATE_STORAGE_SERVICE] = storage_service |
| 61 | + app[APP_STATE_SESSION_FACTORY] = session_factory |
| 62 | + |
| 63 | + return app |
| 64 | + |
| 65 | + |
| 66 | +async def create_app() -> web.Application: |
| 67 | + config = aleph.config.app_config |
| 68 | + |
| 69 | + # TODO: make the config file path configurable |
| 70 | + config_file = Path.cwd() / "config.yml" |
| 71 | + config.yaml.load(str(config_file)) |
| 72 | + |
| 73 | + logging.basicConfig(level=config.logging.level.value) |
| 74 | + |
| 75 | + if config.sentry.dsn.value: |
| 76 | + setup_sentry(config) |
| 77 | + |
| 78 | + return await configure_aiohttp_app(config=config) |
0 commit comments