feat: initialize blog app with blueprints and templates
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Extract blog-specific code from the coop monolith into a standalone repository. Includes auth, blog, post, admin, menu_items, snippets blueprints, associated templates, Dockerfile (APP_MODULE=app:app), entrypoint, and Gitea CI workflow. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
105
app.py
Normal file
105
app.py
Normal file
@@ -0,0 +1,105 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import g, request
|
||||
from sqlalchemy import select
|
||||
|
||||
from shared.factory import create_base_app
|
||||
from config import config
|
||||
from models import KV
|
||||
|
||||
from suma_browser.app.bp import (
|
||||
register_auth_bp,
|
||||
register_blog_bp,
|
||||
register_admin,
|
||||
register_menu_items,
|
||||
register_snippets,
|
||||
register_coop_api,
|
||||
)
|
||||
|
||||
|
||||
async def coop_context() -> dict:
|
||||
"""
|
||||
Coop app context processor.
|
||||
|
||||
- menu_items: direct DB query (coop owns this data)
|
||||
- cart_count/cart_total: fetched from cart internal API
|
||||
"""
|
||||
from shared.context import base_context
|
||||
from suma_browser.app.bp.menu_items.services.menu_items import get_all_menu_items
|
||||
from shared.internal_api import get as api_get
|
||||
|
||||
ctx = await base_context()
|
||||
|
||||
# Coop owns menu_items — query directly
|
||||
ctx["menu_items"] = await get_all_menu_items(g.s)
|
||||
|
||||
# Cart data from cart app API
|
||||
cart_data = await api_get("cart", "/internal/cart/summary", forward_session=True)
|
||||
if cart_data:
|
||||
ctx["cart_count"] = cart_data.get("count", 0)
|
||||
ctx["cart_total"] = cart_data.get("total", 0)
|
||||
else:
|
||||
ctx["cart_count"] = 0
|
||||
ctx["cart_total"] = 0
|
||||
|
||||
return ctx
|
||||
|
||||
|
||||
def create_app() -> "Quart":
|
||||
app = create_base_app("coop", context_fn=coop_context)
|
||||
|
||||
# --- blueprints ---
|
||||
app.register_blueprint(register_auth_bp())
|
||||
|
||||
app.register_blueprint(
|
||||
register_blog_bp(
|
||||
url_prefix=config()["blog_root"],
|
||||
title=config()["blog_title"],
|
||||
),
|
||||
url_prefix=config()["blog_root"],
|
||||
)
|
||||
|
||||
app.register_blueprint(register_admin("/settings"))
|
||||
app.register_blueprint(register_menu_items())
|
||||
app.register_blueprint(register_snippets())
|
||||
|
||||
# Internal API (server-to-server, CSRF-exempt)
|
||||
app.register_blueprint(register_coop_api())
|
||||
|
||||
# --- KV admin endpoints ---
|
||||
@app.get("/settings/kv/<key>")
|
||||
async def kv_get(key: str):
|
||||
row = (
|
||||
await g.s.execute(select(KV).where(KV.key == key))
|
||||
).scalar_one_or_none()
|
||||
return {"key": key, "value": (row.value if row else None)}
|
||||
|
||||
@app.post("/settings/kv/<key>")
|
||||
async def kv_set(key: str):
|
||||
data = await request.get_json() or {}
|
||||
val = data.get("value", "")
|
||||
obj = await g.s.get(KV, key)
|
||||
if obj is None:
|
||||
obj = KV(key=key, value=val)
|
||||
g.s.add(obj)
|
||||
else:
|
||||
obj.value = val
|
||||
return {"ok": True, "key": key, "value": val}
|
||||
|
||||
# --- debug: url rules ---
|
||||
@app.get("/__rules")
|
||||
async def dump_rules():
|
||||
rules = []
|
||||
for r in app.url_map.iter_rules():
|
||||
rules.append({
|
||||
"endpoint": r.endpoint,
|
||||
"rule": repr(r.rule),
|
||||
"methods": sorted(r.methods - {"HEAD", "OPTIONS"}),
|
||||
"strict_slashes": r.strict_slashes,
|
||||
})
|
||||
return {"rules": rules}
|
||||
|
||||
return app
|
||||
|
||||
|
||||
app = create_app()
|
||||
Reference in New Issue
Block a user