1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
APP_FILE_CONTENT = """
from litestar import Litestar
app = Litestar([])
"""
CREATE_APP_FILE_CONTENT = """
from litestar import Litestar
def create_app():
return Litestar([])
def func():
return False
"""
GENERIC_APP_FACTORY_FILE_CONTENT = """
from litestar import Litestar
def any_name() -> Litestar:
return Litestar([])
def func():
return False
"""
GENERIC_APP_FACTORY_FILE_CONTENT_STRING_ANNOTATION = """
from litestar import Litestar
def any_name() -> "Litestar":
return Litestar([])
def func():
return False
"""
GENERIC_APP_FACTORY_FILE_CONTENT_FUTURE_ANNOTATIONS = """
from __future__ import annotations
from litestar import Litestar
def any_name() -> Litestar:
return Litestar([])
def func():
return False
"""
APP_FACTORY_FILE_CONTENT_SERVER_LIFESPAN_PLUGIN = """
from contextlib import contextmanager
from typing import Generator
from litestar import Litestar
from litestar.config.app import AppConfig
from litestar.plugins.base import CLIPlugin
class StartupPrintPlugin(CLIPlugin):
@contextmanager
def server_lifespan(self, app: Litestar) -> Generator[None, None, None]:
print("i_run_before_startup_plugin") # noqa: T201
try:
yield
finally:
print("i_run_after_shutdown_plugin") # noqa: T201
def create_app() -> Litestar:
return Litestar(route_handlers=[], plugins=[StartupPrintPlugin()])
"""
APP_FILE_CONTENT_ROUTES_EXAMPLE = """
from litestar import Litestar, get
from litestar.openapi import OpenAPIConfig
from typing import Dict
@get("/")
def hello_world() -> Dict[str, str]:
return {"hello": "world"}
@get("/foo")
def foo() -> str:
return "bar"
@get("/schema/all/foo/bar/schema/")
def long_api() -> Dict[str, str]:
return {"test": "api"}
app = Litestar(
openapi_config=OpenAPIConfig(
title="test_app",
version="0",
path="/api-docs",
),
route_handlers=[hello_world, foo, long_api]
)
"""
|