Skip to content

Instantly share code, notes, and snippets.

@zoni
Last active March 22, 2023 17:43
Show Gist options
  • Save zoni/c3b0c2afc8f40c801f18ecc5b9f00d7b to your computer and use it in GitHub Desktop.
Save zoni/c3b0c2afc8f40c801f18ecc5b9f00d7b to your computer and use it in GitHub Desktop.
from pathlib import Path
from starlite import Response, Starlite, TemplateConfig, get
from starlite.contrib.jinja import JinjaTemplateEngine
from starlite.status_codes import HTTP_307_TEMPORARY_REDIRECT
@get("/{name:str}", include_in_schema=False)
def hello(name: str = "world") -> Response:
if name == "foo":
return Response(
content="",
media_type="text/plain",
status_code=HTTP_307_TEMPORARY_REDIRECT,
headers={"location": "http://example.com/foo"},
)
# We can read the template as bytes, but then we have no templating.
# And there's no way (as far as I'm aware) to get access to the
# template engine to call a `get_template` or `render` method of some
# kind. Closest I've come so far is returning a non-templated response
return Response(
content=f"Hello {name}",
media_type="text/html",
)
app = Starlite(
route_handlers=[hello],
template_config=TemplateConfig(
directory=Path("templates"),
engine=JinjaTemplateEngine,
),
debug=True,
)
anyio==3.6.2
certifi==2022.12.7
click==8.1.3
dnspython==2.3.0
EditorConfig==0.12.3
email-validator==1.3.1
Faker==18.3.0
fast-query-parsers==0.3.1
greenlet==2.0.2
gunicorn==20.1.0
h11==0.14.0
httpcore==0.16.3
httpx==0.23.3
idna==3.4
Jinja2==3.1.2
jsbeautifier==1.14.7
Mako==1.2.4
markdown-it-py==2.2.0
MarkupSafe==2.1.2
mdurl==0.1.2
msgpack==1.0.5
msgspec==0.13.1
multidict==6.0.4
picologging==0.9.1
pydantic==1.10.6
pydantic-factories==1.17.2
pydantic-openapi-schema==1.5.1
Pygments==2.14.0
pynvim==0.4.3
python-dateutil==2.8.2
PyYAML==6.0
rfc3986==1.5.0
rich==13.3.2
six==1.16.0
sniffio==1.3.0
starlite==1.51.7
typing_extensions==4.5.0
uvicorn==0.21.1
watchfiles==0.18.1
@zoni
Copy link
Author

zoni commented Mar 22, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment