Skip to content

Commit d100d5f

Browse files
Sehat1137sehat1137
andauthored
feature: add --factory param (#1440)
Co-authored-by: sehat1137 <[email protected]>
1 parent 9f21e72 commit d100d5f

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

faststream/cli/docs/app.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def serve(
4444
" Defaults to the current working directory."
4545
),
4646
),
47+
is_factory: bool = typer.Option(
48+
False,
49+
"--factory", help="Treat APP as an application factory"
50+
),
4751
) -> None:
4852
"""Serve project AsyncAPI schema."""
4953
if ":" in app:
@@ -66,18 +70,18 @@ def serve(
6670

6771
except ImportError:
6872
warnings.warn(INSTALL_WATCHFILES, category=ImportWarning, stacklevel=1)
69-
_parse_and_serve(app, host, port)
73+
_parse_and_serve(app, host, port, is_factory)
7074

7175
else:
7276
WatchReloader(
7377
target=_parse_and_serve,
74-
args=(app, host, port),
78+
args=(app, host, port, is_factory),
7579
reload_dirs=(str(module_parent),),
7680
extra_extensions=extra_extensions,
7781
).run()
7882

7983
else:
80-
_parse_and_serve(app, host, port)
84+
_parse_and_serve(app, host, port, is_factory)
8185

8286

8387
@docs_app.command(name="gen")
@@ -104,12 +108,18 @@ def gen(
104108
" Defaults to the current working directory."
105109
),
106110
),
111+
is_factory: bool = typer.Option(
112+
False,
113+
"--factory", help="Treat APP as an application factory"
114+
),
107115
) -> None:
108116
"""Generate project AsyncAPI schema."""
109117
if app_dir: # pragma: no branch
110118
sys.path.insert(0, app_dir)
111119

112120
_, app_obj = import_from_string(app)
121+
if callable(app_obj) and is_factory:
122+
app_obj = app_obj()
113123
raw_schema = get_app_schema(app_obj)
114124

115125
if yaml:
@@ -138,9 +148,12 @@ def _parse_and_serve(
138148
app: str,
139149
host: str = "localhost",
140150
port: int = 8000,
151+
is_factory: bool = False,
141152
) -> None:
142153
if ":" in app:
143154
_, app_obj = import_from_string(app)
155+
if callable(app_obj) and is_factory:
156+
app_obj = app_obj()
144157
raw_schema = get_app_schema(app_obj)
145158

146159
else:

faststream/cli/main.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def run(
9494
" Defaults to the current working directory."
9595
),
9696
),
97+
is_factory: bool = typer.Option(
98+
False,
99+
"--factory",
100+
is_flag=True,
101+
help="Treat APP as an application factory",
102+
),
97103
) -> None:
98104
"""Run [MODULE:APP] FastStream application."""
99105
if watch_extensions and not reload:
@@ -108,7 +114,7 @@ def run(
108114
if app_dir: # pragma: no branch
109115
sys.path.insert(0, app_dir)
110116

111-
args = (app, extra, casted_log_level)
117+
args = (app, extra, is_factory, casted_log_level)
112118

113119
if reload and workers > 1:
114120
raise SetupError("You can't use reload option with multiprocessing")
@@ -151,11 +157,14 @@ def _run(
151157
# NOTE: we should pass `str` due FastStream is not picklable
152158
app: str,
153159
extra_options: Dict[str, "SettingField"],
160+
is_factory: bool,
154161
log_level: int = logging.INFO,
155162
app_level: int = logging.INFO,
156163
) -> None:
157164
"""Runs the specified application."""
158165
_, app_obj = import_from_string(app)
166+
if is_factory and callable(app_obj):
167+
app_obj = app_obj()
159168

160169
if not isinstance(app_obj, FastStream):
161170
raise typer.BadParameter(
@@ -200,6 +209,10 @@ def publish(
200209
app: str = typer.Argument(..., help="FastStream app instance, e.g., main:app"),
201210
message: str = typer.Argument(..., help="Message to be published"),
202211
rpc: bool = typer.Option(False, help="Enable RPC mode and system output"),
212+
is_factory: bool = typer.Option(
213+
False,
214+
"--factory", help="Treat APP as an application factory"
215+
),
203216
) -> None:
204217
"""Publish a message using the specified broker in a FastStream application.
205218
@@ -218,6 +231,9 @@ def publish(
218231
raise ValueError("Message parameter is required.")
219232

220233
_, app_obj = import_from_string(app)
234+
if callable(app_obj) and is_factory:
235+
app_obj = app_obj()
236+
221237
if not app_obj.broker:
222238
raise ValueError("Broker instance not found in the app.")
223239

0 commit comments

Comments
 (0)