v0.152.0
 1#
 2#
 3# This file is part of gunicorn released under the MIT license.
 4# See the LICENSE for more information.
 5#
 6# Vendored and modified for Plain.
 7
 8from __future__ import annotations
 9
10import sys
11from typing import Any
12
13from . import util
14from .arbiter import Arbiter
15
16
17class ServerApplication:
18    """Plain's server application."""
19
20    def __init__(
21        self,
22        *,
23        bind: list[str],
24        workers: int,
25        threads: int,
26        timeout: int,
27        reload: bool,
28        certfile: str | None,
29        keyfile: str | None,
30        accesslog: bool,
31    ) -> None:
32        self.bind = bind
33        self.workers = workers
34        self.threads = threads
35        self.timeout = timeout
36        self.reload = reload
37        self.certfile = certfile
38        self.keyfile = keyfile
39        self.accesslog = accesslog
40
41    @property
42    def address(self) -> list[tuple[str, int] | str]:
43        return [util.parse_address(util.bytes_to_str(bind)) for bind in self.bind]
44
45    @property
46    def is_ssl(self) -> bool:
47        return self.certfile is not None or self.keyfile is not None
48
49    def load(self) -> Any:
50        """Load the request handler."""
51        from plain.internal.handlers.base import BaseHandler
52
53        handler = BaseHandler()
54        handler.load_middleware()
55        return handler
56
57    def run(self) -> None:
58        """Run the server."""
59
60        try:
61            Arbiter(self).run()
62        except RuntimeError as e:
63            print(f"\nError: {e}\n", file=sys.stderr)
64            sys.stderr.flush()
65            sys.exit(1)