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.app import Sanic
from sanic.application.logo import get_logo from sanic.application.logo import get_logo
from sanic.cli.arguments import Group from sanic.cli.arguments import Group
from sanic.http.constants import HTTP
from sanic.log import error_logger from sanic.log import error_logger
from sanic.simple import create_simple_server 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: try:
app = self._get_app() app = self._get_app()
kwargs = self._build_run_kwargs() kwargs = self._build_run_kwargs()
app.run(**kwargs)
except ValueError: except ValueError:
error_logger.exception("Failed to run app") 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): def _precheck(self):
# # Custom TLS mismatch handling for better diagnostics # # 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: elif len(ssl) == 1 and ssl[0] is not None:
# Use only one cert, no TLSSelector. # Use only one cert, no TLSSelector.
ssl = ssl[0] ssl = ssl[0]
version = HTTP(self.args.http)
kwargs = { kwargs = {
"access_log": self.args.access_log, "access_log": self.args.access_log,
"debug": self.args.debug, "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, "unix": self.args.unix,
"verbosity": self.args.verbosity or 0, "verbosity": self.args.verbosity or 0,
"workers": self.args.workers, "workers": self.args.workers,
"version": version,
} }
for maybe_arg in ("auto_reload", "dev"): for maybe_arg in ("auto_reload", "dev"):

View File

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

View File

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