Skip to content

Commit

Permalink
[JAC-CLOUD-MINI]: Walkers endpoint now based on jac-cloud format
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Oct 9, 2024
1 parent 25f9656 commit 919e291
Show file tree
Hide file tree
Showing 2 changed files with 258 additions and 112 deletions.
17 changes: 11 additions & 6 deletions jac-cloud/jac_cloud/plugin/mini/cli_mini.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from asyncer import syncify

from fastapi import Depends, FastAPI, File, Response, UploadFile, status
from fastapi import APIRouter, Depends, FastAPI, File, Response, UploadFile, status
from fastapi.responses import ORJSONResponse

from jaclang import jac_import
Expand Down Expand Up @@ -79,7 +79,7 @@ def gen_model_field(cls: type, field: Field, is_file: bool = False) -> tuple[typ
return consts


def populate_apis(app: FastAPI, cls: Type[WalkerArchitype]) -> None:
def populate_apis(router: APIRouter, cls: Type[WalkerArchitype]) -> None:
"""Generate FastAPI endpoint based on WalkerArchitype class."""
body: dict[str, Any] = {}
files: dict[str, Any] = {}
Expand Down Expand Up @@ -140,8 +140,8 @@ def api_root(
) -> Response:
return api_entry(None, payload)

app.post(url := f"/{cls.__name__}", summary=url)(api_root)
app.post(url := f"/{cls.__name__}/{{node}}", summary=url)(api_entry)
router.post(url := f"/{cls.__name__}", summary=url)(api_root)
router.post(url := f"/{cls.__name__}/{{node}}", summary=url)(api_entry)


def serve_mini(filename: str, host: str = "0.0.0.0", port: int = 8000) -> None:
Expand Down Expand Up @@ -173,15 +173,20 @@ def serve_mini(filename: str, host: str = "0.0.0.0", port: int = 8000) -> None:
raise ValueError("Not a valid file!\nOnly supports `.jac` and `.jir`")

app = FastAPI()
healtz_router = APIRouter(prefix="/healthz", tags=["monitoring"])
walker_router = APIRouter(prefix="/walker", tags=["walker"])

@app.get("/", status_code=status.HTTP_200_OK)
@healtz_router.get("/", status_code=status.HTTP_200_OK)
def healthz() -> Response:
"""Healthz API."""
return Response()

for obj in module.__dict__.values():
if isclass(obj) and issubclass(obj, WalkerArchitype):
populate_apis(app, obj)
populate_apis(walker_router, obj)

app.include_router(healtz_router)
app.include_router(walker_router)

run(app, host=host, port=port)

Expand Down
Loading

0 comments on commit 919e291

Please sign in to comment.