Step 8 - Web Layer & Plugin System
Scaffold standard secure endpoints (including dynamic search, lookups, and schema introspection) instantly and register your domain with the application Kernel.
Instead of writing redundant route functions for every model, ZCore's BaseRouter generates standard secure endpoints out-of-the-box (including dynamic search, field-projected lookups, and role-aware schema introspection). We then register everything cleanly via the Plugin system.
Configure the Router
Open tasks/routers.py and link your schemas, model, and service:
# tasks/routers.py
from zcore import BaseRouter, RouteKey
from .models import Task
from .schemas import TaskCreate, TaskUpdate, TaskResponse
from .services import TaskService
class TaskRouter(BaseRouter[TaskCreate, TaskUpdate]):
model = Task
create_schema = TaskCreate
update_schema = TaskUpdate
schema_out = TaskResponse
lookup_schema = TaskResponse # Required to activate the /lookup endpoint
service = TaskService
prefix = "/tasks"
tags = ["Tasks"]
# Example: Exclude the DELETE endpoint
exclude = {RouteKey.DELETE}
# Optional: Add custom domain endpoints without route shadowing
def register_custom_routes(self) -> None:
@self.router.get("/summary")
async def get_task_summary():
return {"status": "summary generated"}
router_instance = TaskRouter()Auto-Sorting & Anti-Shadowing: In ZCore, you never need to worry about dynamic path parameters like /{id:uuid} shadowing static paths like /tasks/summary. BaseRouter binds automatic route-sorting hooks so that custom endpoints added via register_custom_routes() or after super().__init__() are sorted by path specificity, preventing 422 RequestValidationError mismatches.
Tip: When the DELETE endpoint is enabled, it automatically supports the ?force=true query parameter to execute permanent physical hard-deletions when using soft-delete models.
Wire the Plugin
Open tasks/plugin.py. The Plugin protocol manages the lifecycle of your domain. Ensure it includes your router and implements all asynchronous lifecycle hooks required by the Kernel:
# tasks/plugin.py
from fastapi import FastAPI
from zcore import Plugin
from .routers import router_instance
class TaskPlugin(Plugin):
name = "tasks"
version = "0.1.0"
dependencies = [] # Can list upstream plugin names if needed
def setup(self, app: FastAPI) -> None:
app.include_router(router_instance.router)
async def before_startup(self) -> None:
"""Executed before core application startup routines."""
pass
async def on_startup(self) -> None:
"""Executed during application startup."""
pass
async def after_startup(self) -> None:
"""Executed after application startup concludes."""
pass
async def on_shutdown(self) -> None:
"""Executed in reverse dependency order during application shutdown."""
passLifecycle Contract: All four asynchronous hooks (before_startup, on_startup, after_startup, on_shutdown) are strictly required by the Plugin protocol. The Kernel.lifespan context manager calls these hooks sequentially based on topological dependency order.
Register with the Kernel
Finally, open your root main.py and register the plugin with the Kernel. The Kernel uses topological sorting to ensure plugins start in the correct order.
# main.py (Add this before kernel.setup(app))
from tasks.plugin import TaskPlugin
# ... existing setup code ...
kernel.add_plugin(TaskPlugin())
kernel.setup(app)Check it out: Run zc run and navigate to http://127.0.0.1:8000/docs. You will see 8 fully functional, secure endpoints (POST, GET, GET_ALL, SEARCH, LOOKUP, UPDATE, PATCH, and GET /_schema) plus your custom /tasks/summary endpoint generated and sorted automatically!
We have a working API. But how do we secure it and make the Zchema masking actually work? Let's add authentication.