phi/src/phi/api/app.py

41 lines
900 B
Python
Raw Normal View History

2020-08-29 20:14:55 +02:00
# -*- encoding: utf-8 -*-
import logging
2017-12-16 23:03:03 +01:00
from aiohttp import web
from phi.logging import get_logger
2020-08-29 20:14:55 +02:00
from phi.async_ldap.client import AsyncClient
from phi.async_ldap.model import Hackers, Robots, Congregations
2017-12-16 23:03:03 +01:00
from phi.api.routes import api_routes
log = get_logger(__name__)
2020-08-29 20:14:55 +02:00
alog = logging.getLogger("asyncio")
2017-12-16 23:03:03 +01:00
def api_startup(app):
2020-08-29 20:14:55 +02:00
app["ldap_client"].open()
def api_shutdown(app):
2020-08-29 20:14:55 +02:00
app["ldap_client"].close()
def api_app(config):
2017-12-16 23:03:03 +01:00
log.info("Initializing API sub-app.")
2017-12-16 23:03:03 +01:00
app = web.Application()
2020-08-29 20:14:55 +02:00
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)
2017-12-16 23:03:03 +01:00
return app