Skip to content

Commit

Permalink
support endpoint model
Browse files Browse the repository at this point in the history
  • Loading branch information
fanqingsong committed Oct 10, 2023
1 parent 45f52bf commit 76b8661
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 20 deletions.
2 changes: 1 addition & 1 deletion demo/cornerstone/onestone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def post_teardown(self):
print("call pre teardown from cornerstone!!!")


class CornerstoneAsyncImpl(Cornerstone):
class CornerstoneAsyncImpl(CornerstoneAsync):

def __init__(self, app: FastAPI):
super(CornerstoneAsyncImpl, self).__init__(app)
Expand Down
30 changes: 29 additions & 1 deletion demo/yyy_endpoint/house_price2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@


from fastapi import FastAPI
from fastapi_hive.ioc_framework.endpoint_model import Endpoint, EndpointAsync
from demo.yyy_endpoint.house_price2.router import router
from demo.yyy_endpoint.house_price2.service import service


class EndpointImpl(Endpoint):

def __init__(self, app: FastAPI):
super(EndpointImpl, self).__init__(app)

def setup(self):
print("call pre setup from EndpointImpl!!!")
print("---- get fastapi app ------")
print(self._app)

def teardown(self):
print("call pre teardown from EndpointImpl!!!")


class EndpointAsyncImpl(EndpointAsync):

def __init__(self, app: FastAPI):
super(EndpointAsyncImpl, self).__init__(app)

async def setup(self):
print("call pre setup from cornerstone EndpointAsyncImpl!!!")

async def teardown(self):
print("call pre teardown from cornerstone EndpointAsyncImpl!!!")

2 changes: 1 addition & 1 deletion docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@

---

FastAPI Hive Framework loads modules of packages, It extracts the abstraction Endpoint properties, including router and service, which will be mounted into app during app startup.
FastAPI Hive Framework loads modules of packages, It extracts the abstraction EndpointMeta properties, including router and service, which will be mounted into app during app startup.

---
1 change: 1 addition & 0 deletions fastapi_hive/ioc_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"fastapi_hive.ioc_framework.endpoint_container_mounter.implement",
"fastapi_hive.ioc_framework.endpoint_router_mounter.implement",
"fastapi_hive.ioc_framework.cornerstone_hook_caller.implement",
"fastapi_hive.ioc_framework.endpoint_hook_caller.implement",
"fastapi_hive.ioc_framework.implement",
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def run_pre_setup_hook(self):
one_entity: CornerstoneMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'CornerstoneImpl'):
if not hasattr(imported_module, 'CornerstoneAsyncImpl'):
continue

cornerstone: CornerstoneAsync = imported_module.CornerstoneAsyncImpl(self._app)
Expand All @@ -121,7 +121,7 @@ async def run_post_setup_hook(self):
one_entity: CornerstoneMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'CornerstoneImpl'):
if not hasattr(imported_module, 'CornerstoneAsyncImpl'):
continue

cornerstone: CornerstoneAsync = imported_module.CornerstoneAsyncImpl(self._app)
Expand All @@ -134,7 +134,7 @@ async def run_pre_teardown_hook(self):
one_entity: CornerstoneMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'CornerstoneImpl'):
if not hasattr(imported_module, 'CornerstoneAsyncImpl'):
continue

cornerstone: CornerstoneAsync = imported_module.CornerstoneAsyncImpl(self._app)
Expand All @@ -147,7 +147,7 @@ async def run_post_teardown_hook(self):
one_entity: CornerstoneMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'CornerstoneImpl'):
if not hasattr(imported_module, 'CornerstoneAsyncImpl'):
continue

cornerstone: CornerstoneAsync = imported_module.CornerstoneAsyncImpl(self._app)
Expand Down
4 changes: 2 additions & 2 deletions fastapi_hive/ioc_framework/cornerstone_model/implement.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cornerstone:
In your cornerstone modules `__init__.py` create a subclass of `Cornerstone`
```python
from fastapi_hive.ioc_framework import Cornerstone
from fastapi_hive.ioc_framework.cornerstone_model import Cornerstone
class CornerstoneImpl(Cornerstone):
Expand Down Expand Up @@ -47,7 +47,7 @@ class CornerstoneAsync:
In your cornerstone modules `__init__.py` create a subclass of `CornerstoneAsync`
```python
from fastapi_hive.ioc_framework import CornerstoneAsync
from fastapi_hive.ioc_framework.cornerstone_model import CornerstoneAsync
class CornerstoneAsyncImpl(CornerstoneAsync):
Expand Down
4 changes: 2 additions & 2 deletions fastapi_hive/ioc_framework/endpoint_container/implement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from loguru import logger
# import sys
# from .module_path import ModulePath
from fastapi_hive.ioc_framework.endpoint_model import Endpoint
from fastapi_hive.ioc_framework.endpoint_model import EndpointMeta


class EndpointContainer:
Expand Down Expand Up @@ -46,7 +46,7 @@ def resolve_modules(self):

one_module_entity = importlib.import_module(one_module_path)

module_instance = Endpoint()
module_instance = EndpointMeta()
module_instance.name = one_module
module_instance.package = package_name
module_instance.imported_module = one_module_entity
Expand Down
5 changes: 5 additions & 0 deletions fastapi_hive/ioc_framework/endpoint_hook_caller/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

from fastapi_hive.ioc_framework.endpoint_hook_caller.implement import EndpointHookCaller, EndpointHookAsyncCaller


__all__ = ["EndpointHookCaller", "EndpointHookAsyncCaller"]
93 changes: 93 additions & 0 deletions fastapi_hive/ioc_framework/endpoint_hook_caller/implement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@


from fastapi import FastAPI
from loguru import logger
from fastapi_hive.ioc_framework.endpoint_container import EndpointContainer
from fastapi_hive.ioc_framework.endpoint_model import Endpoint, EndpointAsync, EndpointMeta
from dependency_injector.wiring import Provide, inject
from fastapi_hive.ioc_framework.di_contiainer import DIContainer
from fastapi_hive.ioc_framework.ioc_config import IoCConfig


class EndpointHookCaller:
@inject
def __init__(
self,
app: FastAPI,
endpoint_container: EndpointContainer = Provide[DIContainer.endpoint_container],
ioc_config: IoCConfig = Provide[DIContainer.ioc_config],
):
logger.info("endpoint hook caller is starting.")

self._app = app
self._endpoint_container = endpoint_container
self._ioc_config = ioc_config

def run_setup_hook(self):

modules = self._endpoint_container.modules
for one_module, one_entity in modules.items():
one_entity: EndpointMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'EndpointImpl'):
continue

endpoint: Endpoint = imported_module.EndpointImpl(self._app)

endpoint.setup()

def run_teardown_hook(self):
modules = self._endpoint_container.modules
for one_module, one_entity in modules.items():
one_entity: EndpointMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'EndpointImpl'):
continue

cornerstone: Endpoint = imported_module.EndpointImpl(self._app)

cornerstone.teardown()


class EndpointHookAsyncCaller:
@inject
def __init__(
self,
app: FastAPI,
endpoint_container: EndpointContainer = Provide[DIContainer.endpoint_container],
ioc_config: IoCConfig = Provide[DIContainer.ioc_config],
):
logger.info("endpoint hook async caller is starting.")

self._app = app
self._endpoint_container = endpoint_container
self._ioc_config = ioc_config

async def run_setup_hook(self):
modules = self._endpoint_container.modules
for one_module, one_entity in modules.items():
one_entity: EndpointMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'EndpointAsyncImpl'):
continue

endpoint: EndpointAsync = imported_module.EndpointAsyncImpl(self._app)

await endpoint.setup()

async def run_teardown_hook(self):
modules = self._endpoint_container.modules
for one_module, one_entity in modules.items():
one_entity: EndpointMeta = one_entity
imported_module = one_entity.imported_module

if not hasattr(imported_module, 'EndpointAsyncImpl'):
continue

endpoint: EndpointAsync = imported_module.EndpointAsyncImpl(self._app)

await endpoint.teardown()

5 changes: 3 additions & 2 deletions fastapi_hive/ioc_framework/endpoint_model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from fastapi_hive.ioc_framework.endpoint_model.implement import Endpoint
from fastapi_hive.ioc_framework.endpoint_model.implement import EndpointMeta, Endpoint, EndpointAsync


__all__ = ["Endpoint"]
__all__ = ["EndpointMeta", "Endpoint", "EndpointAsync"]

60 changes: 59 additions & 1 deletion fastapi_hive/ioc_framework/endpoint_model/implement.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@

from fastapi import APIRouter
from fastapi import APIRouter, FastAPI
from typing import Optional


class Endpoint:
'''
Base class for Endpoint modules.
Usage
===
In your Endpoint modules `__init__.py` create a subclass of `Endpoint`
```python
from fastapi_hive.ioc_framework.endpoint_model import Endpoint
class EndpointImpl(Endpoint):
def setup(self):
pass
```
'''

def __init__(self, app: FastAPI) -> None:
self._app = app

def setup(self):
pass

def teardown(self):
pass


class EndpointAsync:
'''
Base class for Endpoint modules in async mode.
Usage
===
In your Endpoint modules `__init__.py` create a subclass of `EndpointAsync`
```python
from fastapi_hive.ioc_framework.endpoint_model import EndpointAsync
class EndpointAsyncImpl(EndpointAsync):
async def setup(self):
pass
```
'''

def __init__(self, app: FastAPI) -> None:
self._app = app

async def setup(self):
pass

async def teardown(self):
pass


class EndpointMeta:
def __init__(self):
self._name: Optional[str] = None
self._package: Optional[str] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import FastAPI
from loguru import logger
from fastapi_hive.ioc_framework.endpoint_container import EndpointContainer
from fastapi_hive.ioc_framework.endpoint_model import Endpoint
from fastapi_hive.ioc_framework.endpoint_model import EndpointMeta
from fastapi import APIRouter
from dependency_injector.wiring import Provide, inject
from fastapi_hive.ioc_framework.di_contiainer import DIContainer
Expand Down Expand Up @@ -47,7 +47,7 @@ def _collect_router(self) -> APIRouter:
# print(dir(self._endpoint_container))

for one_module, one_entity in modules.items():
one_entity: Endpoint = one_entity
one_entity: EndpointMeta = one_entity
module_router = one_entity.router
package_name = one_entity.package

Expand Down
13 changes: 11 additions & 2 deletions fastapi_hive/ioc_framework/implement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi_hive.ioc_framework.endpoint_router_mounter import EndpointRouterMounter
from fastapi_hive.ioc_framework.endpoint_container_mounter import EndpointContainerMounter
from fastapi_hive.ioc_framework.cornerstone_hook_caller import CornerstoneHookCaller, CornerstoneHookAsyncCaller
from fastapi_hive.ioc_framework.endpoint_hook_caller import EndpointHookCaller, EndpointHookAsyncCaller
from fastapi_hive.ioc_framework.ioc_config import IoCConfig
from dependency_injector.wiring import Provide, inject
from fastapi_hive.ioc_framework.di_contiainer import DIContainer
Expand Down Expand Up @@ -43,6 +44,10 @@ def __init__(

self._cornerstone_hook_async_caller = CornerstoneHookAsyncCaller(app)

self._endpoint_hook_caller = EndpointHookCaller(app)

self._endpoint_hook_async_caller = EndpointHookAsyncCaller(app)

# print("------- IoCFramework init **** after call self._endpoint_container222 ------ ")
# print(self._cornerstone_container)

Expand Down Expand Up @@ -85,18 +90,22 @@ def _add_async_event_handler(self):
app.add_event_handler("shutdown", self._stop_ioc_async_handler())

def _sync_setup(self) -> None:
self._endpoint_hook_caller.run_setup_hook()

self._endpoint_container_mounter.mount()
self._endpoint_router_mounter.mount(self._ioc_config.API_PREFIX)

def _sync_teardown(self) -> None:
self._endpoint_hook_caller.run_teardown_hook()

self._endpoint_container_mounter.unmount()
self._endpoint_router_mounter.unmount(self._ioc_config.API_PREFIX)

async def _async_setup(self) -> None:
pass
await self._endpoint_hook_async_caller.run_setup_hook()

async def _async_teardown(self) -> None:
pass
await self._endpoint_hook_async_caller.run_teardown_hook()

def _start_ioc_sync_handler(self) -> Callable:
app = self._app
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
site_name: FastAPI Hive
site_description: FastAPI Endpoint Framework, packages&xxx_endpoint code structure, developer-friendly, easy to be integrated.
site_description: FastAPI EndpointMeta Framework, packages&xxx_endpoint code structure, developer-friendly, easy to be integrated.
site_url: https://fanqingsong.github.io/fastapi-hive/
theme:
name: material
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='fastapi_hive',
version='1.0.15',
version='1.0.16',
description='framework for FastAPI modules management',
long_description_content_type='text/markdown',
long_description=long_description,
Expand Down

0 comments on commit 76b8661

Please sign in to comment.