from . import templates from guillotina.entrypoint import app as guillo from starlette.applications import Starlette from starlette.responses import PlainTextResponse from starlette.responses import JSONResponse from guillotina.component import get_utility from guillotina.interfaces import IApplication from guillotina.utils.content import get_database from guillotina.utils.content import navigate_to from guillotina import task_vars import logging import httpx import os logger = logging.getLogger("writing") folder = os.path.dirname(__file__) template_folder = os.path.join(folder, "templates") template_folder = os.path.abspath(template_folder) templates.global_init(template_folder, auto_reload=True) app = Starlette() @app.route("/") @templates.template("index.pt") async def home(request): root = get_utility(IApplication, name="root") db = await get_database("db", root) task_vars.db.set(db) tm = db.get_transaction_manager() await tm.begin() task_vars.tm.set(tm) obj = await navigate_to(db, "v1/folder") await tm.abort() return {"param": "world", "obj": obj} @app.route("/demo") async def demo(request): return PlainTextResponse("demo, hola") @app.route("/httpx") async def calling_with_httpx(request): # this uses a feature around httpx, that allos to # make requests to an asgi app without network async with httpx.AsyncClient( app=app, base_url="http://localhost/" ) as client: resp = await client.get("/db/v1/", auth=("root", "root")) return JSONResponse(resp.json()) app.add_event_handler("startup", guillo.startup) app.add_event_handler("shutdown", guillo.shutdown) app.mount("/", guillo) # run it: G_CONFIG_FILE=config.yml uvicorn writing:app --port 8765 --reload