| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import argparse |
| 4 |
import ssl |
| 5 |
import sys |
| 6 |
import warnings |
| 7 |
from typing import List, Optional |
| 8 |
|
| 9 |
from .config import Config |
| 10 |
from .run import run |
| 11 |
|
| 12 |
sentinel = object() |
| 13 |
|
| 14 |
|
| 15 |
def _load_config(config_path: Optional[str]) -> Config: |
| 16 |
if config_path is None: |
| 17 |
return Config() |
| 18 |
elif config_path.startswith("python:"): |
| 19 |
return Config.from_object(config_path[len("python:") :]) |
| 20 |
elif config_path.startswith("file:"): |
| 21 |
return Config.from_pyfile(config_path[len("file:") :]) |
| 22 |
else: |
| 23 |
return Config.from_toml(config_path) |
| 24 |
|
| 25 |
|
| 26 |
def main(sys_args: Optional[List[str]] = None) -> None: |
| 27 |
parser = argparse.ArgumentParser() |
| 28 |
parser.add_argument( |
| 29 |
"application", help="The application to dispatch to as path.to.module:instance.path" |
| 30 |
) |
| 31 |
parser.add_argument("--access-log", help="Deprecated, see access-logfile", default=sentinel) |
| 32 |
parser.add_argument( |
| 33 |
"--access-logfile", |
| 34 |
help="The target location for the access log, use `-` for stdout", |
| 35 |
default=sentinel, |
| 36 |
) |
| 37 |
parser.add_argument( |
| 38 |
"--access-logformat", |
| 39 |
help="The log format for the access log, see help docs", |
| 40 |
default=sentinel, |
| 41 |
) |
| 42 |
parser.add_argument( |
| 43 |
"--backlog", help="The maximum number of pending connections", type=int, default=sentinel |
| 44 |
) |
| 45 |
parser.add_argument( |
| 46 |
"-b", |
| 47 |
"--bind", |
| 48 |
dest="binds", |
| 49 |
help=""" The TCP host/address to bind to. Should be either host:port, host, |
| 50 |
unix:path or fd://num, e.g. 127.0.0.1:5000, 127.0.0.1, |
| 51 |
unix:/tmp/socket or fd://33 respectively. """, |
| 52 |
default=[], |
| 53 |
action="append", |
| 54 |
) |
| 55 |
parser.add_argument("--ca-certs", help="Path to the SSL CA certificate file", default=sentinel) |
| 56 |
parser.add_argument("--certfile", help="Path to the SSL certificate file", default=sentinel) |
| 57 |
parser.add_argument("--cert-reqs", help="See verify mode argument", type=int, default=sentinel) |
| 58 |
parser.add_argument("--ciphers", help="Ciphers to use for the SSL setup", default=sentinel) |
| 59 |
parser.add_argument( |
| 60 |
"-c", |
| 61 |
"--config", |
| 62 |
help="Location of a TOML config file, or when prefixed with `file:` a Python file, or when prefixed with `python:` a Python module.", # noqa: E501 |
| 63 |
default=None, |
| 64 |
) |
| 65 |
parser.add_argument( |
| 66 |
"--debug", |
| 67 |
help="Enable debug mode, i.e. extra logging and checks", |
| 68 |
action="store_true", |
| 69 |
default=sentinel, |
| 70 |
) |
| 71 |
parser.add_argument("--error-log", help="Deprecated, see error-logfile", default=sentinel) |
| 72 |
parser.add_argument( |
| 73 |
"--error-logfile", |
| 74 |
"--log-file", |
| 75 |
dest="error_logfile", |
| 76 |
help="The target location for the error log, use `-` for stderr", |
| 77 |
default=sentinel, |
| 78 |
) |
| 79 |
parser.add_argument( |
| 80 |
"--graceful-timeout", |
| 81 |
help="""Time to wait after SIGTERM or Ctrl-C for any remaining requests (tasks) |
| 82 |
to complete.""", |
| 83 |
default=sentinel, |
| 84 |
type=int, |
| 85 |
) |
| 86 |
parser.add_argument( |
| 87 |
"--read-timeout", |
| 88 |
help="""Seconds to wait before timing out reads on TCP sockets""", |
| 89 |
default=sentinel, |
| 90 |
type=int, |
| 91 |
) |
| 92 |
parser.add_argument( |
| 93 |
"-g", "--group", help="Group to own any unix sockets.", default=sentinel, type=int |
| 94 |
) |
| 95 |
parser.add_argument( |
| 96 |
"-k", |
| 97 |
"--worker-class", |
| 98 |
dest="worker_class", |
| 99 |
help="The type of worker to use. " |
| 100 |
"Options include asyncio, uvloop (pip install hypercorn[uvloop]), " |
| 101 |
"and trio (pip install hypercorn[trio]).", |
| 102 |
default=sentinel, |
| 103 |
) |
| 104 |
parser.add_argument( |
| 105 |
"--keep-alive", |
| 106 |
help="Seconds to keep inactive connections alive for", |
| 107 |
default=sentinel, |
| 108 |
type=int, |
| 109 |
) |
| 110 |
parser.add_argument("--keyfile", help="Path to the SSL key file", default=sentinel) |
| 111 |
parser.add_argument( |
| 112 |
"--keyfile-password", help="Password to decrypt the SSL key file", default=sentinel |
| 113 |
) |
| 114 |
parser.add_argument( |
| 115 |
"--insecure-bind", |
| 116 |
dest="insecure_binds", |
| 117 |
help="""The TCP host/address to bind to. SSL options will not apply to these binds. |
| 118 |
See *bind* for formatting options. Care must be taken! See HTTP -> HTTPS redirection docs. |
| 119 |
""", |
| 120 |
default=[], |
| 121 |
action="append", |
| 122 |
) |
| 123 |
parser.add_argument( |
| 124 |
"--log-config", |
| 125 |
help=""""A Python logging configuration file. This can be prefixed with |
| 126 |
'json:' or 'toml:' to load the configuration from a file in |
| 127 |
that format. Default is the logging ini format.""", |
| 128 |
default=sentinel, |
| 129 |
) |
| 130 |
parser.add_argument( |
| 131 |
"--log-level", help="The (error) log level, defaults to info", default=sentinel |
| 132 |
) |
| 133 |
parser.add_argument( |
| 134 |
"-p", "--pid", help="Location to write the PID (Program ID) to.", default=sentinel |
| 135 |
) |
| 136 |
parser.add_argument( |
| 137 |
"--quic-bind", |
| 138 |
dest="quic_binds", |
| 139 |
help="""The UDP/QUIC host/address to bind to. See *bind* for formatting |
| 140 |
options. |
| 141 |
""", |
| 142 |
default=[], |
| 143 |
action="append", |
| 144 |
) |
| 145 |
parser.add_argument( |
| 146 |
"--reload", |
| 147 |
help="Enable automatic reloads on code changes", |
| 148 |
action="store_true", |
| 149 |
default=sentinel, |
| 150 |
) |
| 151 |
parser.add_argument( |
| 152 |
"--root-path", help="The setting for the ASGI root_path variable", default=sentinel |
| 153 |
) |
| 154 |
parser.add_argument( |
| 155 |
"--server-name", |
| 156 |
dest="server_names", |
| 157 |
help="""The hostnames that can be served, requests to different hosts |
| 158 |
will be responded to with 404s. |
| 159 |
""", |
| 160 |
default=[], |
| 161 |
action="append", |
| 162 |
) |
| 163 |
parser.add_argument( |
| 164 |
"--statsd-host", help="The host:port of the statsd server", default=sentinel |
| 165 |
) |
| 166 |
parser.add_argument("--statsd-prefix", help="Prefix for all statsd messages", default="") |
| 167 |
parser.add_argument( |
| 168 |
"-m", |
| 169 |
"--umask", |
| 170 |
help="The permissions bit mask to use on any unix sockets.", |
| 171 |
default=sentinel, |
| 172 |
type=int, |
| 173 |
) |
| 174 |
parser.add_argument( |
| 175 |
"-u", "--user", help="User to own any unix sockets.", default=sentinel, type=int |
| 176 |
) |
| 177 |
|
| 178 |
def _convert_verify_mode(value: str) -> ssl.VerifyMode: |
| 179 |
try: |
| 180 |
return ssl.VerifyMode[value] |
| 181 |
except KeyError: |
| 182 |
raise argparse.ArgumentTypeError(f"'{value}' is not a valid verify mode") |
| 183 |
|
| 184 |
parser.add_argument( |
| 185 |
"--verify-mode", |
| 186 |
help="SSL verify mode for peer's certificate, see ssl.VerifyMode enum for possible values.", |
| 187 |
type=_convert_verify_mode, |
| 188 |
default=sentinel, |
| 189 |
) |
| 190 |
parser.add_argument( |
| 191 |
"--websocket-ping-interval", |
| 192 |
help="""If set this is the time in seconds between pings sent to the client. |
| 193 |
This can be used to keep the websocket connection alive.""", |
| 194 |
default=sentinel, |
| 195 |
type=int, |
| 196 |
) |
| 197 |
parser.add_argument( |
| 198 |
"-w", |
| 199 |
"--workers", |
| 200 |
dest="workers", |
| 201 |
help="The number of workers to spawn and use", |
| 202 |
default=sentinel, |
| 203 |
type=int, |
| 204 |
) |
| 205 |
args = parser.parse_args(sys_args or sys.argv[1:]) |
| 206 |
config = _load_config(args.config) |
| 207 |
config.application_path = args.application |
| 208 |
|
| 209 |
if args.log_level is not sentinel: |
| 210 |
config.loglevel = args.log_level |
| 211 |
if args.access_logformat is not sentinel: |
| 212 |
config.access_log_format = args.access_logformat |
| 213 |
if args.access_log is not sentinel: |
| 214 |
warnings.warn( |
| 215 |
"The --access-log argument is deprecated, use `--access-logfile` instead", |
| 216 |
DeprecationWarning, |
| 217 |
) |
| 218 |
config.accesslog = args.access_log |
| 219 |
if args.access_logfile is not sentinel: |
| 220 |
config.accesslog = args.access_logfile |
| 221 |
if args.backlog is not sentinel: |
| 222 |
config.backlog = args.backlog |
| 223 |
if args.ca_certs is not sentinel: |
| 224 |
config.ca_certs = args.ca_certs |
| 225 |
if args.certfile is not sentinel: |
| 226 |
config.certfile = args.certfile |
| 227 |
if args.cert_reqs is not sentinel: |
| 228 |
config.cert_reqs = args.cert_reqs |
| 229 |
if args.ciphers is not sentinel: |
| 230 |
config.ciphers = args.ciphers |
| 231 |
if args.debug is not sentinel: |
| 232 |
config.debug = args.debug |
| 233 |
if args.error_log is not sentinel: |
| 234 |
warnings.warn( |
| 235 |
"The --error-log argument is deprecated, use `--error-logfile` instead", |
| 236 |
DeprecationWarning, |
| 237 |
) |
| 238 |
config.errorlog = args.error_log |
| 239 |
if args.error_logfile is not sentinel: |
| 240 |
config.errorlog = args.error_logfile |
| 241 |
if args.graceful_timeout is not sentinel: |
| 242 |
config.graceful_timeout = args.graceful_timeout |
| 243 |
if args.read_timeout is not sentinel: |
| 244 |
config.read_timeout = args.read_timeout |
| 245 |
if args.group is not sentinel: |
| 246 |
config.group = args.group |
| 247 |
if args.keep_alive is not sentinel: |
| 248 |
config.keep_alive_timeout = args.keep_alive |
| 249 |
if args.keyfile is not sentinel: |
| 250 |
config.keyfile = args.keyfile |
| 251 |
if args.keyfile_password is not sentinel: |
| 252 |
config.keyfile_password = args.keyfile_password |
| 253 |
if args.log_config is not sentinel: |
| 254 |
config.logconfig = args.log_config |
| 255 |
if args.pid is not sentinel: |
| 256 |
config.pid_path = args.pid |
| 257 |
if args.root_path is not sentinel: |
| 258 |
config.root_path = args.root_path |
| 259 |
if args.reload is not sentinel: |
| 260 |
config.use_reloader = args.reload |
| 261 |
if args.statsd_host is not sentinel: |
| 262 |
config.statsd_host = args.statsd_host |
| 263 |
if args.statsd_prefix is not sentinel: |
| 264 |
config.statsd_prefix = args.statsd_prefix |
| 265 |
if args.umask is not sentinel: |
| 266 |
config.umask = args.umask |
| 267 |
if args.user is not sentinel: |
| 268 |
config.user = args.user |
| 269 |
if args.worker_class is not sentinel: |
| 270 |
config.worker_class = args.worker_class |
| 271 |
if args.verify_mode is not sentinel: |
| 272 |
config.verify_mode = args.verify_mode |
| 273 |
if args.websocket_ping_interval is not sentinel: |
| 274 |
config.websocket_ping_interval = args.websocket_ping_interval |
| 275 |
if args.workers is not sentinel: |
| 276 |
config.workers = args.workers |
| 277 |
|
| 278 |
if len(args.binds) > 0: |
| 279 |
config.bind = args.binds |
| 280 |
if len(args.insecure_binds) > 0: |
| 281 |
config.insecure_bind = args.insecure_binds |
| 282 |
if len(args.quic_binds) > 0: |
| 283 |
config.quic_bind = args.quic_binds |
| 284 |
if len(args.server_names) > 0: |
| 285 |
config.server_names = args.server_names |
| 286 |
|
| 287 |
run(config) |
| 288 |
|
| 289 |
|
| 290 |
if __name__ == "__main__": |
| 291 |
main() |