From cab745379100bda12927778e6d6b5e90c2e5b7c7 Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Wed, 19 Jan 2022 21:57:08 +0200 Subject: [PATCH] Add multi-serve for http3 and http1 --- sanic/cli/app.py | 9 +++++---- sanic/cli/arguments.py | 20 +++++++++++--------- sanic/http/constants.py | 5 ++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/sanic/cli/app.py b/sanic/cli/app.py index 19847dee..179fdbf2 100644 --- a/sanic/cli/app.py +++ b/sanic/cli/app.py @@ -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"): diff --git a/sanic/cli/arguments.py b/sanic/cli/arguments.py index f443dbda..74ef7d85 100644 --- a/sanic/cli/arguments.py +++ b/sanic/cli/arguments.py @@ -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): diff --git a/sanic/http/constants.py b/sanic/http/constants.py index 6b914c1a..62d8f46f 100644 --- a/sanic/http/constants.py +++ b/sanic/http/constants.py @@ -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