Debian Codegraph @

docs/examples/plugins/init_plugin_protocol.py

raw
1 from typing import Dict
2
3 from litestar import Litestar, get
4 from litestar.config.app import AppConfig
5 from litestar.di import NamedDependency, Provide
6 from litestar.plugins import InitPlugin
7
8
9 @get("/", sync_to_thread=False)
10 def route_handler(name: NamedDependency[str]) -> Dict[str, str]:
11 return {"hello": name}
12
13
14 def get_name() -> str:
15 return "world"
16
17
18 class MyPlugin(InitPlugin):
19 def on_app_init(self, app_config: AppConfig) -> AppConfig:
20 app_config.dependencies["name"] = Provide(get_name, sync_to_thread=False)
21 app_config.route_handlers.append(route_handler)
22 return app_config
23
24
25 app = Litestar(plugins=[MyPlugin()])
26
27 # run: /