Skip to content

Commit 6047717

Browse files
committed
✨ Use argparse for the CLI version so more options can be set.
1 parent 746d896 commit 6047717

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/kubernetes_wsgi/__main__.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
Run your existing WSGI application using `python -m kubernetes_wsgi myapp`.
33
"""
44

5+
import argparse
56
import importlib
67
import sys
78

89
# This is a fake import, only used during type checking.
9-
from typing import TYPE_CHECKING, Optional
10+
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Text
1011

1112
from .server import serve
1213

@@ -15,6 +16,45 @@
1516
from wsgiref.types import WSGIApplication
1617

1718

19+
def parse_args(argv: Sequence[Text]) -> Dict[str, Any]:
20+
parser = argparse.ArgumentParser(
21+
prog="kubernetes_wsgi",
22+
description="Start a kubernetes-wsgi web server",
23+
)
24+
parser.add_argument(
25+
"application",
26+
metavar="MODULE:APP",
27+
help="the WSGI application to run in a dotted import form "
28+
"(eg. myapp or myapp.wsgi) with an optional :function_name if needed",
29+
)
30+
parser.add_argument(
31+
"--port",
32+
type=int,
33+
default=8000,
34+
help="port to run the web application on",
35+
)
36+
parser.add_argument(
37+
"--metrics-port",
38+
metavar="PORT",
39+
type=int,
40+
default=9000,
41+
help="port to run the Prometheus metrics on",
42+
)
43+
parser.add_argument(
44+
"--health-check-path",
45+
metavar="PATH",
46+
default="/healthz",
47+
help="URL path to the health check endpoint",
48+
)
49+
args = parser.parse_args(argv)
50+
return {
51+
"application": args.application,
52+
"port": args.port,
53+
"metrics_port": args.metrics_port,
54+
"health_check_path": args.health_check_path,
55+
}
56+
57+
1858
def load_application(app_str: str) -> "WSGIApplication":
1959
func_name = None # type: Optional[str]
2060
if ":" in app_str:
@@ -38,14 +78,9 @@ def load_application(app_str: str) -> "WSGIApplication":
3878

3979

4080
def main():
41-
app, port = (sys.argv[1:] + ([None] * 2))[:2]
42-
if app is None:
43-
app = "wsgi"
44-
if port is None:
45-
port = 8000
46-
app = load_application(app)
47-
port = int(port)
48-
serve(app, port=port)
81+
args = parse_args(sys.argv[1:])
82+
args["application"] = load_application(args["application"])
83+
serve(**args)
4984

5085

5186
if __name__ == "__main__":

tests/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_app1_fail(launch_server):
5858

5959

6060
def test_app1_port(launch_server):
61-
p = launch_server("app1.wsgi", "8080")
61+
p = launch_server("app1.wsgi", "--port", "8080")
6262
assert p.poll() is None
6363
r = requests.get("http://localhost:8080/")
6464
assert r.text == "Hello world\n"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy