@@ -469,8 +469,26 @@ def diff(
469469 console .print (result .changelog_str ())
470470
471471
472- @app .command ()
472+ def _get_uvicorn_arguments (port : int , host : str , context : typer .Context ) -> dict :
473+ """
474+ Take the default datacontract uvicorn arguments and merge them with the
475+ extra arguments passed to the command to start the API.
476+ """
477+ default_args = {
478+ "app" : "datacontract.api:app" ,
479+ "port" : port ,
480+ "host" : host ,
481+ "reload" : True ,
482+ }
483+
484+ # Create a list of the extra arguments, remove the leading -- from the cli arguments
485+ trimmed_keys = list (map (lambda x : str (x ).replace ("--" , "" ),context .args [::2 ]))
486+ # Merge the two dicts and return them as one dict
487+ return default_args | dict (zip (trimmed_keys , context .args [1 ::2 ]))
488+
489+ @app .command (context_settings = {"allow_extra_args" : True , "ignore_unknown_options" : True })
473490def api (
491+ ctx : Annotated [typer .Context , typer .Option (help = "Extra arguments to pass to uvicorn.run()." )],
474492 port : Annotated [int , typer .Option (help = "Bind socket to this port." )] = 4242 ,
475493 host : Annotated [
476494 str , typer .Option (help = "Bind socket to this host. Hint: For running in docker, set it to 0.0.0.0" )
@@ -488,14 +506,21 @@ def api(
488506
489507 To connect to servers (such as a Snowflake data source), set the credentials as environment variables as documented in
490508 https://cli.datacontract.com/#test
509+
510+ It is possible to run the API with extra arguments for `uvicorn.run()` as keyword arguments, e.g.:
511+ `datacontract api --port 1234 --root_path /datacontract`.
491512 """
492513 import uvicorn
493514 from uvicorn .config import LOGGING_CONFIG
494515
495516 log_config = LOGGING_CONFIG
496517 log_config ["root" ] = {"level" : "INFO" }
497518
498- uvicorn .run (app = "datacontract.api:app" , port = port , host = host , reload = True , log_config = LOGGING_CONFIG )
519+ uvicorn_args = _get_uvicorn_arguments (port , host , ctx )
520+ # Add the log config
521+ uvicorn_args ["log_config" ] = log_config
522+ # Run uvicorn
523+ uvicorn .run (** uvicorn_args )
499524
500525
501526def _print_logs (run ):
0 commit comments