1- import argparse
21import logging
32import os
43import pprint
98
109from .app import build_app
1110from .config import construct_build_app_kwargs , parse_configs
12- from .settings import get_settings
13- from .utils import get_authenticators
11+ from .server . server_args import server_arg_parser
12+ from .server . server_utils import print_admin_api_key_if_generated
1413
1514logger = logging .getLogger (__name__ )
1615
@@ -24,78 +23,28 @@ def start_server():
2423 logging .basicConfig (level = logging .WARNING )
2524 logging .getLogger ("bluesky_httpserver" ).setLevel ("INFO" )
2625
27- def formatter (prog ):
28- # Set maximum width such that printed help mostly fits in the RTD theme code block (documentation).
29- return argparse .RawDescriptionHelpFormatter (prog , max_help_position = 20 , width = 90 )
30-
31- parser = argparse .ArgumentParser (
32- description = "Start Bluesky HTTP Server.\n " f"bluesky-httpserver version { qserver_version } .\n " ,
33- formatter_class = formatter ,
34- )
35-
36- parser .add_argument (
37- "--host" ,
38- dest = "http_server_host" ,
39- action = "store" ,
40- default = None ,
41- help = "HTTP server host name, e.g. '127.0.0.1' or 'localhost' " f"(default: { default_http_server_host !r} )." ,
42- )
43-
44- parser .add_argument (
45- "--port" ,
46- dest = "http_server_port" ,
47- action = "store" ,
48- default = None ,
49- help = "HTTP server port, e.g. '127.0.0.1' or 'localhost' " f"(default: { default_http_server_port !r} )." ,
50- )
51-
52- parser .add_argument (
53- "--public" ,
54- dest = "public" ,
55- action = "store_true" ,
56- default = False ,
57- help = "Explicitly allows public access to the server and disables authorization/authentication." ,
58- )
59-
60- parser .add_argument (
61- "--config_path" ,
62- dest = "config_path" ,
63- action = "store" ,
64- default = None ,
65- help = "Path to configuration file or directory with configuration files. The path overrides "
66- "the path defined in QSERVER_HTTP_SERVER_CONFIG environment variable. If the parameter and "
67- "the environemnt variable is not specified, then no configuration file is loaded." ,
68- )
69-
70- args = parser .parse_args ()
71-
72- public = args .public
73- config_path = args .config_path
74-
75- http_server_host = args .http_server_host
76- http_server_port = args .http_server_port
77- http_server_port = int (http_server_port ) if http_server_port else http_server_port
26+ args = server_arg_parser ()
7827
7928 logger .info ("Preparing to start Bluesky HTTP Server ..." )
8029
81- config_path = config_path or os .getenv ("QSERVER_HTTP_SERVER_CONFIG" , None )
30+ config_path = args . config_path or os .getenv ("QSERVER_HTTP_SERVER_CONFIG" , None )
8231 try :
8332 parsed_config = parse_configs (config_path ) if config_path else {}
8433 except Exception as ex :
8534 logger .error (ex )
8635 raise
8736
8837 # Let --public flag override settings in config.
89- if public :
38+ if args . public :
9039 if "authentication" not in parsed_config :
9140 parsed_config ["authentication" ] = {}
9241 parsed_config ["authentication" ]["allow_anonymous_access" ] = True
9342
9443 # Extract config for uvicorn.
9544 uvicorn_kwargs = parsed_config .pop ("uvicorn" , {})
9645 # 'host' and 'port' from CLI parameters overrides the parameters from config.
97- uvicorn_kwargs ["host" ] = http_server_host or uvicorn_kwargs .get ("host" , default_http_server_host )
98- uvicorn_kwargs ["port" ] = http_server_port or uvicorn_kwargs .get ("port" , default_http_server_port )
46+ uvicorn_kwargs ["host" ] = args . http_server_host or uvicorn_kwargs .get ("host" , default_http_server_host )
47+ uvicorn_kwargs ["port" ] = args . http_server_port or uvicorn_kwargs .get ("port" , default_http_server_port )
9948
10049 # This config was already validated when it was parsed. Do not re-validate.
10150 kwargs = construct_build_app_kwargs (parsed_config , source_filepath = config_path )
@@ -113,31 +62,6 @@ def formatter(prog):
11362
11463 uvicorn .run (web_app , ** uvicorn_kwargs )
11564
116-
117- def print_admin_api_key_if_generated (web_app , host , port ):
118- # host = host or "127.0.0.1"
119- # port = port or 8000
120- settings = web_app .dependency_overrides .get (get_settings , get_settings )()
121-
122- logger .info ("APP settings: %s" , pprint .pformat (dict (settings )))
123- authenticators = web_app .dependency_overrides .get (get_authenticators , get_authenticators )()
124- if settings .allow_anonymous_access :
125- print (
126- "The server is running in 'public' mode, permitting open, anonymous access\n "
127- "for reading. Any data that is not specifically controlled with an access\n "
128- "policy will be visible to anyone who can connect to this server.\n " ,
129- file = sys .stderr ,
130- )
131- if (not authenticators ) and settings .single_user_api_key_generated :
132- print (
133- "Navigate a web browser to:\n \n "
134- f"http://{ host } :{ port } ?api_key={ settings .single_user_api_key } \n \n "
135- "or connect an HTTP client to:\n \n "
136- f"http://{ host } :{ port } /api?api_key={ settings .single_user_api_key } \n " ,
137- file = sys .stderr ,
138- )
139-
140-
14165def app_factory ():
14266 """
14367 Return an ASGI app instance.
0 commit comments