Skip to content

Commit

Permalink
support cornerstone
Browse files Browse the repository at this point in the history
  • Loading branch information
fanqingsong committed Oct 9, 2023
1 parent 97952b3 commit 156e5e0
Show file tree
Hide file tree
Showing 86 changed files with 554 additions and 189 deletions.
2 changes: 1 addition & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fastapi_hive/ioc_framework/di_contiainer.py
fastapi_hive/ioc_framework/implement.py
fastapi_hive/ioc_framework/ioc_config.py
fastapi_hive/ioc_framework/module_abstraction/__init__.py
fastapi_hive/ioc_framework/module_abstraction/module.py
fastapi_hive/ioc_framework/module_abstraction/endpoint.py
fastapi_hive/ioc_framework/module_container/__init__.py
fastapi_hive/ioc_framework/module_container/implement.py
fastapi_hive/ioc_framework/module_container/module_path.py
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions demo/cornerstone/onestone/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

from fastapi_hive.ioc_framework.cornerstone_model import Cornerstone, CornerstoneMeta


class CornerstoneImpl(Cornerstone):

def __init__(self):
super(CornerstoneImpl, self).__init__()

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

def post_setup(self):
print("call post setup from cornerstone!!!")

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

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

4 changes: 2 additions & 2 deletions demo/core/security.py → demo/cornerstone/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from fastapi.security.api_key import APIKeyHeader
from starlette.status import HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED

from demo.core import config
from demo.core.messages import AUTH_REQ, NO_API_KEY
from demo.cornerstone import config
from demo.cornerstone.messages import AUTH_REQ, NO_API_KEY

api_key = APIKeyHeader(name="token", auto_error=False)

Expand Down
12 changes: 7 additions & 5 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from fastapi import FastAPI
from loguru import logger
from demo.core.config import (APP_NAME, APP_VERSION, API_PREFIX,
IS_DEBUG)
from demo.cornerstone.config import (APP_NAME, APP_VERSION, API_PREFIX,
IS_DEBUG)

from fastapi_hive.ioc_framework import IoCFramework

Expand All @@ -26,8 +26,10 @@ async def hive_async_post_setup():
logger.info("------ call async post setup -------")

ioc_framework = IoCFramework(fast_app)
ioc_framework.config.CORNERSTONE_PACKAGE_PATHS = ["./demo/cornerstone/"]

ioc_framework.config.API_PREFIX = API_PREFIX
ioc_framework.config.MODULE_PACKAGE_PATHS = ["./demo/package1", "./demo/package2"]
ioc_framework.config.MODULE_PACKAGE_PATHS = ["./demo/xxx_endpoint", "./demo/yyy_endpoint"]
# logger.info("-----------------------------------------------------")
# logger.info(dir(ioc_framework))
# logger.info(dir(ioc_framework.config))
Expand All @@ -40,8 +42,8 @@ async def hive_async_post_setup():

ioc_framework.init_modules()

# ioc_framework.delete_modules_by_packages(["./demo/package1"])
# ioc_framework.add_modules_by_packages(["./demo/package2"])
# ioc_framework.delete_modules_by_packages(["./demo/xxx_endpoint"])
# ioc_framework.add_modules_by_packages(["./demo/yyy_endpoint"])

@fast_app.get("/")
def get_root():
Expand Down
4 changes: 0 additions & 4 deletions demo/package1/heart_beat/__init__.py

This file was deleted.

5 changes: 0 additions & 5 deletions demo/package1/heart_beat/service/__init__.py

This file was deleted.

4 changes: 0 additions & 4 deletions demo/package1/house_price/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions demo/package1/house_price/config.py

This file was deleted.

5 changes: 0 additions & 5 deletions demo/package1/house_price/service/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions demo/package2/heart_beat2/__init__.py

This file was deleted.

5 changes: 0 additions & 5 deletions demo/package2/heart_beat2/service/__init__.py

This file was deleted.

4 changes: 0 additions & 4 deletions demo/package2/house_price2/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions demo/package2/house_price2/config.py

This file was deleted.

6 changes: 0 additions & 6 deletions demo/package2/house_price2/service/__init__.py

This file was deleted.

File renamed without changes.
4 changes: 4 additions & 0 deletions demo/xxx_endpoint/heart_beat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

from demo.xxx_endpoint.heart_beat.router import router
from demo.xxx_endpoint.heart_beat.service import service

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import APIRouter

from demo.package1.house_price.router.implement import router
from demo.xxx_endpoint.heart_beat.router.implement import router

'''
If you need nested router, use the blow code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from fastapi import APIRouter

from demo.package2.heart_beat2.schema.heartbeat import HearbeatResult
from demo.xxx_endpoint.heart_beat.schema.heartbeat import HearbeatResult

router = APIRouter()

Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions demo/xxx_endpoint/heart_beat/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


# from demo.xxx_endpoint.heart_beat2.service.implement import MODEL

service = None
4 changes: 4 additions & 0 deletions demo/xxx_endpoint/house_price/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


from demo.xxx_endpoint.house_price.router import router
from demo.xxx_endpoint.house_price.service import service
3 changes: 3 additions & 0 deletions demo/xxx_endpoint/house_price/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


DEFAULT_MODEL_PATH = "./demo/xxx_endpoint/house_price/model/lin_reg_california_housing_model.joblib"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import APIRouter

from demo.package1.heart_beat.router.implement import router
from demo.xxx_endpoint.house_price.router.implement import router

'''
If you need nested router, use the blow code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from fastapi import APIRouter, Depends
from starlette.requests import Request

from demo.core import security
from demo.cornerstone import security

from demo.package1.house_price.schema.payload import (
from demo.xxx_endpoint.house_price.schema.payload import (
HousePredictionPayload)
from demo.package1.house_price.schema.prediction import HousePredictionResult
from demo.xxx_endpoint.house_price.schema.prediction import HousePredictionResult

from demo.package1.house_price.service import HousePriceModel
from demo.xxx_endpoint.house_price.service import HousePriceModel

from fastapi_hive.ioc_framework.module_container import ModuleContainer
from fastapi_hive.ioc_framework.endpoint_container import EndpointContainer

router = APIRouter()

Expand All @@ -21,7 +21,7 @@ def post_predict(
block_data: HousePredictionPayload = None
) -> HousePredictionResult:

module_container: ModuleContainer = request.app.state.module_container
module_container: EndpointContainer = request.app.state.endpoint_container
model: HousePriceModel = module_container.get_module("house_price").service
prediction: HousePredictionResult = model.predict(block_data)

Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions demo/xxx_endpoint/house_price/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

from demo.xxx_endpoint.house_price.service.implement import HousePriceModel
from demo.xxx_endpoint.house_price.config import DEFAULT_MODEL_PATH

service = HousePriceModel(DEFAULT_MODEL_PATH)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import numpy as np
from loguru import logger

from demo.core.messages import NO_VALID_PAYLOAD
from demo.cornerstone.messages import NO_VALID_PAYLOAD

from demo.package1.house_price.schema.payload import (
from demo.xxx_endpoint.house_price.schema.payload import (
HousePredictionPayload, payload_to_list)
from demo.package1.house_price.schema.prediction import HousePredictionResult
from demo.xxx_endpoint.house_price.schema.prediction import HousePredictionResult


class HousePriceModel(object):
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions demo/yyy_endpoint/heart_beat2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

from demo.yyy_endpoint.heart_beat2.router import router
from demo.yyy_endpoint.heart_beat2.service import service
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import APIRouter

from demo.package2.house_price2.router.implement import router
from demo.yyy_endpoint.heart_beat2.router.implement import router

'''
If you need nested router, use the blow code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from fastapi import APIRouter

from demo.package1.heart_beat.schema.heartbeat import HearbeatResult
from demo.yyy_endpoint.heart_beat2.schema.heartbeat import HearbeatResult

router = APIRouter()

Expand Down
5 changes: 5 additions & 0 deletions demo/yyy_endpoint/heart_beat2/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


# from demo.yyy_endpoint.heart_beat2.service.implement import MODEL

service = None
4 changes: 4 additions & 0 deletions demo/yyy_endpoint/house_price2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


from demo.yyy_endpoint.house_price2.router import router
from demo.yyy_endpoint.house_price2.service import service
3 changes: 3 additions & 0 deletions demo/yyy_endpoint/house_price2/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


DEFAULT_MODEL_PATH = "./demo/yyy_endpoint/house_price2/model/lin_reg_california_housing_model.joblib"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from fastapi import APIRouter

from demo.package2.heart_beat2.router.implement import router
from demo.yyy_endpoint.house_price2.router.implement import router

'''
If you need nested router, use the blow code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from fastapi import APIRouter, Depends
from starlette.requests import Request

from demo.core import security
from demo.cornerstone import security

from demo.package2.house_price2.schema.payload import (
from demo.yyy_endpoint.house_price2.schema.payload import (
HousePredictionPayload)
from demo.package2.house_price2.schema.prediction import HousePredictionResult
from demo.yyy_endpoint.house_price2.schema.prediction import HousePredictionResult

from demo.package2.house_price2.service import HousePriceModel
from demo.yyy_endpoint.house_price2.service import HousePriceModel


router = APIRouter()
Expand All @@ -20,7 +20,7 @@ def post_predict(
block_data: HousePredictionPayload = None
) -> HousePredictionResult:

model: HousePriceModel = request.app.state.module_container.get_module(
model: HousePriceModel = request.app.state.endpoint_container.get_module(
"house_price2").service
prediction: HousePredictionResult = model.predict(block_data)

Expand Down
6 changes: 6 additions & 0 deletions demo/yyy_endpoint/house_price2/service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

from demo.yyy_endpoint.house_price2.service.implement import HousePriceModel

from demo.yyy_endpoint.house_price2.config import DEFAULT_MODEL_PATH

service = HousePriceModel(DEFAULT_MODEL_PATH)
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import numpy as np
from loguru import logger

from demo.core.messages import NO_VALID_PAYLOAD
from demo.cornerstone.messages import NO_VALID_PAYLOAD

from demo.package2.house_price2.schema.payload import (
from demo.yyy_endpoint.house_price2.schema.payload import (
HousePredictionPayload, payload_to_list)
from demo.package2.house_price2.schema.prediction import HousePredictionResult
from demo.yyy_endpoint.house_price2.schema.prediction import HousePredictionResult


class HousePriceModel(object):
Expand Down
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 Module 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 Endpoint properties, including router and service, which will be mounted into app during app startup.

---
6 changes: 3 additions & 3 deletions docs/how.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Second, setup the init sentence of ioc_framework in main.py
```Python
from fastapi import FastAPI
from loguru import logger
from demo.core.config import (APP_NAME, APP_VERSION, API_PREFIX,
IS_DEBUG)
from demo.cornerstone.config import (APP_NAME, APP_VERSION, API_PREFIX,
IS_DEBUG)

from fastapi_hive.ioc_framework import IoCFramework

Expand All @@ -54,7 +54,7 @@ def get_app() -> FastAPI:

ioc_framework = IoCFramework(fast_app)
ioc_framework.config.API_PREFIX = API_PREFIX
ioc_framework.config.MODULE_PACKAGE_PATHS = ["./demo/package1", "./demo/package2"]
ioc_framework.config.MODULE_PACKAGE_PATHS = ["./demo/xxx_endpoint", "./demo/yyy_endpoint"]
ioc_framework.config.HIDE_PACKAGE_IN_URL = False
ioc_framework.config.HIDE_MODULE_IN_URL = False
ioc_framework.init_modules()
Expand Down
5 changes: 3 additions & 2 deletions fastapi_hive/ioc_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
di_container: DIContainer = DIContainer()
di_container.wire(
modules=[
"fastapi_hive.ioc_framework.module_mounter.implement",
"fastapi_hive.ioc_framework.router_mounter.implement",
"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.implement",
]
)
5 changes: 5 additions & 0 deletions fastapi_hive/ioc_framework/cornerstone_container/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

from fastapi_hive.ioc_framework.cornerstone_container.implement import CornerstoneContainer


__all__ = ["CornerstoneContainer"]
Loading

0 comments on commit 156e5e0

Please sign in to comment.