webcompy-server 0.0.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,91 @@
1
+ """Server-side rendering entry points for WebComPy.
2
+
3
+ LLM-friendly documentation is published at https://webcompy.net/llms.txt
4
+ (compact: https://webcompy.net/llms-small.txt, full: https://webcompy.net/llms-full.txt).
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING
10
+
11
+ from webcompy_server._asgi import WEBCOMPY_PREBUILT_DIR_ENV, create_prebuilt_asgi_app
12
+ from webcompy_server._context import ServerRenderContext
13
+ from webcompy_server._html import generate_html
14
+ from webcompy_server._prebuild import (
15
+ PREBUILD_MANIFEST_FILENAME,
16
+ PREBUILD_SCHEMA_VERSION,
17
+ PrebuildManifest,
18
+ PrebuildStaticRouteMeta,
19
+ load_prebuild_manifest,
20
+ )
21
+ from webcompy_server._static_route import StaticRoute
22
+ from webcompy_server.ports._fetch import ServerFetchPort
23
+ from webcompy_server.ports._resource import InMemoryServerResourcePort, ServerResourcePort
24
+
25
+ if TYPE_CHECKING:
26
+ from starlette.types import ASGIApp
27
+
28
+ from webcompy.app import WebComPyApp
29
+ from webcompy.ports._resource import ResourcePort
30
+
31
+
32
+ def configure_server_context(
33
+ app: WebComPyApp,
34
+ *,
35
+ resource_port: ResourcePort | None = None,
36
+ root_app: ASGIApp | None = None,
37
+ ) -> None:
38
+ """Switch a ``WebComPyApp`` to server-side rendering.
39
+
40
+ Sets ``ServerRenderContext`` as the application's render context
41
+ class and installs a fresh ``ServerFetchPort``, preserving any
42
+ external HTTP client configured on a previously installed server
43
+ fetch port.
44
+
45
+ Args:
46
+ app: Application to switch to server-side rendering.
47
+ resource_port: Optional ``ResourcePort`` serving app-package
48
+ resources; when ``None``, any existing resource port is left
49
+ unchanged.
50
+ root_app: Optional host ASGI application used for self-site
51
+ fetches. When given, page routes under the configured base
52
+ URL are blocked during SSR to prevent recursive self-fetches,
53
+ and the fetch port is marked as embedded.
54
+
55
+ """
56
+ app._render_context_class = ServerRenderContext
57
+ old_port = app._server_fetch_port
58
+ fetch_port = ServerFetchPort(external_client=old_port._external_client if old_port is not None else None)
59
+ app._server_fetch_port = fetch_port
60
+ if resource_port is not None:
61
+ app._server_resource_port = resource_port
62
+ if root_app is not None:
63
+ prefix = app.config.base_url.strip("/")
64
+ blocked_paths = [
65
+ f"{prefix}/{route[0].lstrip('/')}" if route[0] else prefix
66
+ for route in (app.routes or [])
67
+ if route[3] is not None
68
+ ]
69
+ fetch_port.configure(
70
+ root_app,
71
+ blocked_paths,
72
+ base_url=app.config.base_url,
73
+ embedded=True,
74
+ )
75
+
76
+
77
+ __all__ = [
78
+ "PREBUILD_MANIFEST_FILENAME",
79
+ "PREBUILD_SCHEMA_VERSION",
80
+ "WEBCOMPY_PREBUILT_DIR_ENV",
81
+ "InMemoryServerResourcePort",
82
+ "PrebuildManifest",
83
+ "PrebuildStaticRouteMeta",
84
+ "ServerRenderContext",
85
+ "ServerResourcePort",
86
+ "StaticRoute",
87
+ "configure_server_context",
88
+ "create_prebuilt_asgi_app",
89
+ "generate_html",
90
+ "load_prebuild_manifest",
91
+ ]