phi/src/phi/api/app.py

41 lines
900 B
Python

# -*- encoding: utf-8 -*-
import logging
from aiohttp import web
from phi.logging import get_logger
from phi.async_ldap.client import AsyncClient
from phi.async_ldap.model import Hackers, Robots, Congregations
from phi.api.routes import api_routes
log = get_logger(__name__)
alog = logging.getLogger("asyncio")
def api_startup(app):
app["ldap_client"].open()
def api_shutdown(app):
app["ldap_client"].close()
def api_app(config):
log.info("Initializing API sub-app.")
app = web.Application()
ldap_client = AsyncClient(**config.get("ldap", {}))
app["ldap_client"] = ldap_client
app["users"] = Hackers(ldap_client)
app["services"] = Robots(ldap_client)
app["groups"] = Congregations(ldap_client)
app["alog"] = alog
app.on_startup.append(api_startup)
app.on_shutdown.append(api_shutdown)
app.router.add_routes(api_routes)
return app