Improve CLI DX
This commit is contained in:
parent
3ef1a72418
commit
3b6134de2e
3 changed files with 29 additions and 9 deletions
|
|
@ -2,28 +2,37 @@
|
|||
import re
|
||||
from importlib import import_module
|
||||
from sys import argv
|
||||
|
||||
import uvicorn
|
||||
from uvicorn.config import LOGGING_CONFIG
|
||||
|
||||
from great_ai.great_ai.context import get_context
|
||||
from .great_ai.exceptions import MissingArgumentError
|
||||
from .great_ai.context import get_context
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
||||
def main():
|
||||
if len(argv) < 2:
|
||||
raise ValueError(f"Provide a filename such as: {argv[0]} my_app.py")
|
||||
raise MissingArgumentError(f"Provide a filename such as: {argv[0]} my_app.py")
|
||||
|
||||
module_name = re.sub(r"\.py\b", "", argv[1])
|
||||
|
||||
base = module_name.split(":")[0]
|
||||
|
||||
import_module(base)
|
||||
get_context().logger.info("Starting server")
|
||||
|
||||
if ":" not in module_name:
|
||||
module_name += ":app"
|
||||
|
||||
base = module_name.split(":")[0]
|
||||
module = import_module(base)
|
||||
get_context().logger.info("Starting server")
|
||||
get_context().logger.warning(
|
||||
'Service name (name of variable) is assumed to be "app",'
|
||||
+ ' such as: `app = create_service(predict_domain)`'
|
||||
)
|
||||
|
||||
uvicorn.run(
|
||||
module_name,
|
||||
host="0.0.0.0",
|
||||
port=6060,
|
||||
timeout_keep_alive=600,
|
||||
workers=1,
|
||||
reload=not get_context().is_production,
|
||||
log_config={
|
||||
|
|
@ -40,3 +49,11 @@ if __name__ == "__main__":
|
|||
},
|
||||
},
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (MissingArgumentError, ModuleNotFoundError) as e:
|
||||
get_context().logger.error(e)
|
||||
except KeyboardInterrupt:
|
||||
exit()
|
||||
|
|
|
|||
|
|
@ -32,5 +32,8 @@ if __name__ == "__main__":
|
|||
if args.delete:
|
||||
for f in args.delete:
|
||||
LargeFile(f).delete()
|
||||
except KeyboardInterrupt:
|
||||
logger.warning("Exiting")
|
||||
exit()
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue