Refactor to composable KayaMixin architecture
Replace wrapper-based SessionMiddleware/OIDCApp with KayaMixin subclasses applied via KayaApp(mixins=[...]). Mixins hook into handle_request and handle_websocket via before/after hooks, so both ASGI and RSGI keep working. Mixin dependencies are applied automatically and deduplicated.
This commit is contained in:
+10
-10
@@ -1,30 +1,30 @@
|
||||
import os
|
||||
|
||||
from kaya.core import HttpContext, KayaApp
|
||||
from kaya.oidc import OIDCConfig, OIDCApp
|
||||
from kaya.session import InMemorySessionStore, SessionMiddleware
|
||||
from kaya.oidc import OIDCConfig, OIDCMixin
|
||||
from kaya.session import InMemorySessionStore, SessionMixin
|
||||
|
||||
app = KayaApp()
|
||||
session_app = SessionMiddleware(app, InMemorySessionStore())
|
||||
|
||||
oidc = OIDCApp(
|
||||
session_app,
|
||||
session = SessionMixin(InMemorySessionStore())
|
||||
oidc = OIDCMixin(
|
||||
OIDCConfig(
|
||||
issuer=os.environ.get('OIDC_ISSUER', 'https://accounts.google.com'),
|
||||
client_id=os.environ.get('OIDC_CLIENT_ID', 'replace-me'),
|
||||
client_secret=os.environ.get('OIDC_CLIENT_SECRET'),
|
||||
redirect_uri=os.environ.get('OIDC_REDIRECT_URI', 'http://localhost:8000/auth/callback'),
|
||||
fetch_userinfo=True,
|
||||
)
|
||||
),
|
||||
session=session,
|
||||
)
|
||||
|
||||
app = KayaApp(mixins=[session, oidc])
|
||||
|
||||
@oidc.GET('/')
|
||||
|
||||
@app.GET('/')
|
||||
async def home(ctx: HttpContext) -> None:
|
||||
await ctx.send_str(200, 'public home')
|
||||
|
||||
|
||||
@oidc.GET('/profile')
|
||||
@app.GET('/profile')
|
||||
@oidc.require_auth
|
||||
async def profile(ctx: HttpContext) -> None:
|
||||
user = oidc.get_user(ctx)
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
from kaya.core import HttpContext, KayaApp
|
||||
from kaya.session import InMemorySessionStore, SessionMiddleware
|
||||
from kaya.session import InMemorySessionStore, SessionMixin
|
||||
|
||||
app = SessionMiddleware(KayaApp(), InMemorySessionStore())
|
||||
app = KayaApp(mixins=[SessionMixin(InMemorySessionStore())])
|
||||
|
||||
|
||||
@app.GET('/')
|
||||
|
||||
Reference in New Issue
Block a user