Add multi-serve for http3 and http1

This commit is contained in:
Adam Hopkins 2022-01-19 21:57:08 +02:00
parent 65459fdeb6
commit cab7453791
No known key found for this signature in database
GPG Key ID: 9F85EE6C807303FB
3 changed files with 18 additions and 16 deletions

View File

@ -11,7 +11,6 @@ from typing import Any, List, Union
from sanic.app import Sanic
from sanic.application.logo import get_logo
from sanic.cli.arguments import Group
from sanic.http.constants import HTTP
from sanic.log import error_logger
from sanic.simple import create_simple_server
@ -78,9 +77,13 @@ Or, a path to a directory to run as a simple HTTP server:
try:
app = self._get_app()
kwargs = self._build_run_kwargs()
app.run(**kwargs)
except ValueError:
error_logger.exception("Failed to run app")
else:
for http_version in self.args.http:
app.prepare(**kwargs, version=http_version)
Sanic.serve()
def _precheck(self):
# # Custom TLS mismatch handling for better diagnostics
@ -159,7 +162,6 @@ Or, a path to a directory to run as a simple HTTP server:
elif len(ssl) == 1 and ssl[0] is not None:
# Use only one cert, no TLSSelector.
ssl = ssl[0]
version = HTTP(self.args.http)
kwargs = {
"access_log": self.args.access_log,
"debug": self.args.debug,
@ -172,7 +174,6 @@ Or, a path to a directory to run as a simple HTTP server:
"unix": self.args.unix,
"verbosity": self.args.verbosity or 0,
"workers": self.args.workers,
"version": version,
}
for maybe_arg in ("auto_reload", "dev"):

View File

@ -1,12 +1,12 @@
from __future__ import annotations
from argparse import ArgumentParser, _ArgumentGroup
from os import getpid
from typing import List, Optional, Type, Union
from sanic_routing import __version__ as __routing_version__ # type: ignore
from sanic import __version__
from sanic.http.constants import HTTP
class Group:
@ -91,27 +91,27 @@ class HTTPVersionGroup(Group):
name = "HTTP version"
def attach(self):
group = self.container.add_mutually_exclusive_group()
group.add_argument(
http_values = [http.value for http in HTTP.__members__.values()]
self.container.add_argument(
"--http",
dest="http",
action="append",
choices=http_values,
type=int,
default=0,
help=(
"Which HTTP version to use: HTTP/1.1 or HTTP/3. Value should\n"
"be either 0, 1, or 3, where '0' means use whatever versions\n"
"are available [default 0]"
"be either 1, or 3. [default 1]"
),
)
group.add_argument(
self.container.add_argument(
"-1",
dest="http",
action="append_const",
const=1,
help=("Run Sanic server using HTTP/1.1"),
)
group.add_argument(
self.container.add_argument(
"-3",
dest="http",
action="append_const",
@ -120,7 +120,9 @@ class HTTPVersionGroup(Group):
)
def prepare(self, args):
print(args.http)
if not args.http:
args.http = [1]
args.http = tuple(sorted(set(map(HTTP, args.http)), reverse=True))
class SocketGroup(Group):

View File

@ -1,4 +1,4 @@
from enum import Enum
from enum import Enum, IntEnum
class Stage(Enum):
@ -20,7 +20,6 @@ class Stage(Enum):
FAILED = 100 # Unrecoverable state (error while sending response)
class HTTP(Enum):
AUTO = 0
class HTTP(IntEnum):
VERSION_1 = 1
VERSION_3 = 3