2
2
Run your existing WSGI application using `python -m kubernetes_wsgi myapp`.
3
3
"""
4
4
5
+ import argparse
5
6
import importlib
6
7
import sys
7
8
8
9
# 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
10
11
11
12
from .server import serve
12
13
15
16
from wsgiref .types import WSGIApplication
16
17
17
18
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
+
18
58
def load_application (app_str : str ) -> "WSGIApplication" :
19
59
func_name = None # type: Optional[str]
20
60
if ":" in app_str :
@@ -38,14 +78,9 @@ def load_application(app_str: str) -> "WSGIApplication":
38
78
39
79
40
80
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 )
49
84
50
85
51
86
if __name__ == "__main__" :
0 commit comments