pysxLearn by building

Build and operate · Lesson 26 of 28

Hosting and deployment

Validate a host configuration and distinguish local teaching defaults from production policy.

An application needs operational boundaries as well as attractive markup. HostConfig owns budgets, assets, lifecycle hooks and security policy. Standalone and optional ASGI adapters serve the same application engine.

A loopback teaching server is not a production authentication/TLS setup. The production flag validates required security choices instead of silently accepting an unsafe collection of defaults.

Understand the moving parts

The configuration controls below instantiate real HostConfig values. Select an invalid production profile and inspect its rejection, then choose the complete profile. No remote host is contacted and no credentials are collected.

Read the course runbook before exposing the application: use a real identity provider, explicit HTTPS origins, secure cookies and a deliberate authorization policy. Health checks and graceful shutdown are part of deployment. Keep the state/key directory stable across the restarts you expect to recover from.

Walk through the code

from pysx import Fragment, HostConfig, RequestContext, SecurityPolicy, pysx, signal


def app() -> Fragment:
    result = signal("Local host: render capacity 25")

    def invalid(_: object) -> None:
        try:
            HostConfig(production=True)
        except ValueError as error:
            result.set(f"Rejected: {error}")

    def complete(_: object) -> None:
        def authorize(
            request: RequestContext,
            action: str,
            target: str,
        ) -> bool:
            writer = request.principal == "writer"

            return writer and (action != "write" or target == "course-demo")

        config = HostConfig(
            production=True,
            secure_cookies=True,
            security=SecurityPolicy(
                origins=("https://course.example",),
                require_principal=True,
                authorize=authorize,
            ),
        )
        result.set(f"Accepted complete production configuration: {config.production}")

    return pysx(t"""
        div:
            button(id="host-invalid" onClick={invalid}):
                "Validate incomplete production profile"
            button(id="host-complete" onClick={complete}): "Validate complete profile"
        output(id="host-result" role="status"): {result}
        p: "This validates configuration only; no remote service is contacted."
    """)

The controls instantiate the same real HostConfig profiles as the live validator: incomplete production is rejected, while the complete profile declares HTTPS origin, secure cookies, identity and authorization. Reset demo restores the local-mode readout; validation does not start or deploy a server.

Try it live

Try the incomplete production configuration, select the complete profile, then reset to local mode.

What to observe

The validator rejects missing security requirements and accepts explicitly complete configuration.

Local host: render capacity 25

This validates configuration only; no remote service is contacted.

Take this with you

Treat deployment policy, budgets and lifecycle as application behavior that can be tested.