Skip to content

Commit

Permalink
add pre and post hook
Browse files Browse the repository at this point in the history
  • Loading branch information
fanqingsong committed Oct 8, 2023
1 parent 815ff36 commit 9a87c9b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def get_app() -> FastAPI:

fast_app = FastAPI(title=APP_NAME, version=APP_VERSION, debug=IS_DEBUG)

def hive_pre_setup():
logger.info("------ call pre setup -------")

def hive_post_setup():
logger.info("------ call post setup -------")

ioc_framework = IoCFramework(fast_app)
ioc_framework.config.API_PREFIX = API_PREFIX
ioc_framework.config.MODULE_PACKAGE_PATHS = ["./demo/package1", "./demo/package2"]
Expand All @@ -21,6 +27,9 @@ def get_app() -> FastAPI:
# logger.info(dir(ioc_framework.config))
ioc_framework.config.HIDE_PACKAGE_IN_URL = False
ioc_framework.config.HIDE_MODULE_IN_URL = False
ioc_framework.config.PRE_SETUP = hive_pre_setup
ioc_framework.config.POST_SETUP = hive_post_setup

ioc_framework.init_modules()

# ioc_framework.delete_modules_by_packages(["./demo/package1"])
Expand Down
13 changes: 13 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,16 @@ tox

This runs tests and coverage for Python 3.6 and Flake8, Autopep8, Bandit.

## Package and Upload

For maintainer of this project, please follow:
Before these action, change version in setup.py

```bash
python3 setup.py sdist

twine upload dist/*

```


26 changes: 26 additions & 0 deletions fastapi_hive/ioc_framework/implement.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,41 @@ def _teardown(self) -> None:
self._router_mounter.unmount(self._ioc_config.API_PREFIX)

def _start_ioc_handler(self) -> Callable:
app = self._app

def startup() -> None:
logger.info("Running container start handler.")

hive_pre_stetup = self._ioc_config.PRE_SETUP
if callable(hive_pre_stetup):
logger.info("call hive pre setup")
hive_pre_stetup()

self._setup()

hive_post_stetup = self._ioc_config.POST_SETUP
if callable(hive_post_stetup):
logger.info("call hive post setup")
hive_post_stetup()

return startup

def _stop_ioc_handler(self) -> Callable:
app = self._app

def shutdown() -> None:
logger.info("Running container shutdown handler.")

hive_pre_teardown = self._ioc_config.PRE_TEARDOWN
if callable(hive_pre_teardown):
logger.info("call hive pre teardown")
hive_pre_teardown()

self._teardown()

hive_post_teardown = self._ioc_config.POST_TEARDOWN
if callable(hive_post_teardown):
logger.info("call hive post teardown")
hive_post_teardown()

return shutdown
6 changes: 5 additions & 1 deletion fastapi_hive/ioc_framework/ioc_config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@


from pydantic import BaseModel
from typing import List
from typing import List, Callable


class IoCConfig(BaseModel):
API_PREFIX: str = ""
MODULE_PACKAGE_PATHS: List[str] = ["./demo/package1"]
HIDE_PACKAGE_IN_URL: bool = False
HIDE_MODULE_IN_URL: bool = False
PRE_SETUP: Callable = None
POST_SETUP: Callable = None
PRE_TEARDOWN: Callable = None
POST_TEARDOWN: Callable = None
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.9',
version='1.0.10',
description='framework for FastAPI modules management',
long_description_content_type='text/markdown',
long_description=long_description,
Expand Down

0 comments on commit 9a87c9b

Please sign in to comment.