Coverage for .tox / coverage / lib / python3.11 / site-packages / wuttaweb / cli / webapp.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.1, created at 2025-12-28 15:23 -0600
« prev ^ index » next coverage.py v7.13.1, created at 2025-12-28 15:23 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# wuttaweb -- Web App for Wutta Framework
5# Copyright © 2024-2025 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24See also: :ref:`wutta-webapp`
25"""
27import os
28import sys
29from typing_extensions import Annotated
31import typer
32from pyramid.scripts import pserve
34from wuttjamaican.cli import wutta_typer
37@wutta_typer.command()
38def webapp( # pylint: disable=unused-argument
39 ctx: typer.Context,
40 auto_reload: Annotated[
41 bool,
42 typer.Option("--reload", "-r", help="Auto-reload web app when files change."),
43 ] = False,
44):
45 """
46 Run the configured web app
47 """
48 config = ctx.parent.wutta_config
50 # we'll need config file(s) to specify for web app
51 if not config.files_read:
52 sys.stderr.write("no config files found!\n")
53 sys.exit(1)
55 runner = config.get(f"{config.appname}.web.app.runner", default="pserve")
56 if runner == "pserve":
58 # run pserve
59 argv = ["pserve", f"file+ini:{config.files_read[0]}"]
60 if ctx.params["auto_reload"]:
61 argv.append("--reload")
62 pserve.main(argv=argv)
64 elif runner == "uvicorn":
66 import uvicorn # pylint: disable=import-error,import-outside-toplevel
68 # need service details from config
69 spec = config.require(f"{config.appname}.web.app.spec")
70 kw = {
71 "host": config.get(f"{config.appname}.web.app.host", default="127.0.0.1"),
72 "port": config.get_int(f"{config.appname}.web.app.port", default=8000),
73 "reload": ctx.params["auto_reload"],
74 "reload_dirs": config.get_list(f"{config.appname}.web.app.reload_dirs"),
75 "factory": config.get_bool(
76 f"{config.appname}.web.app.factory", default=False
77 ),
78 "interface": config.get(
79 f"{config.appname}.web.app.interface", default="auto"
80 ),
81 "root_path": config.get(f"{config.appname}.web.app.root_path", default=""),
82 }
84 # also must inject our config files to env, since there is no
85 # other way to specify when running via uvicorn
86 os.environ["WUTTA_CONFIG_FILES"] = os.pathsep.join(config.files_read)
88 # run uvicorn
89 uvicorn.run(spec, **kw)
91 else:
92 sys.stderr.write(f"unknown web app runner: {runner}\n")
93 sys.exit(2)