22Run your existing WSGI application using `python -m kubernetes_wsgi myapp`.
33"""
44
5+ import argparse
56import importlib
67import 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
1112from .server import serve
1213
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+
1858def 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
4080def 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
5186if __name__ == "__main__" :
0 commit comments