diff --git a/halfapi/cli/run.py b/halfapi/cli/run.py index 6ebc0be..461bb73 100644 --- a/halfapi/cli/run.py +++ b/halfapi/cli/run.py @@ -22,11 +22,11 @@ from ..lib.schemas import schema_csv_dict @click.option('--loglevel', default=LOGLEVEL) @click.option('--prefix', default='/') @click.option('--check', default=True) +@click.option('--dryrun', default=False, is_flag=True) @click.argument('schema', type=click.File('r'), required=False) -@click.argument('router', required=False) @click.argument('domain', required=False) @cli.command() -def run(host, port, reload, secret, production, loglevel, prefix, check, schema, router, domain): +def run(host, port, reload, secret, production, loglevel, prefix, check, dryrun, schema, domain): """ The "halfapi run" command """ @@ -51,7 +51,6 @@ def run(host, port, reload, secret, production, loglevel, prefix, check, schema, click.echo(f'Launching application {PROJECT_NAME}') CONFIG.get('domain')['name'] = domain - CONFIG.get('domain')['router'] = router if schema: # Populate the SCHEMA global with the data from the given file @@ -67,6 +66,9 @@ def run(host, port, reload, secret, production, loglevel, prefix, check, schema, f'reload: {reload}\n' ) + if dryrun: + CONFIG['dryrun'] = True + uvicorn.run('halfapi.app:application', host=host, port=int(port), diff --git a/halfapi/conf.py b/halfapi/conf.py index 06611a3..2c17b42 100644 --- a/halfapi/conf.py +++ b/halfapi/conf.py @@ -17,6 +17,7 @@ It defines the following globals : - HOST (str) - HALFAPI_HOST - PORT (int) - HALFAPI_PORT - CONF_DIR (str) - HALFAPI_CONF_DIR + - DRYRUN (bool) - HALFAPI_DRYRUN - config (ConfigParser) It reads the following ressource : @@ -53,6 +54,7 @@ HOST = '127.0.0.1' PORT = '3000' SECRET = '' CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config') +DRYRUN = bool(os.environ.get('HALFAPI_DRYRUN', False)) DOMAIN = None ROUTER = None @@ -143,6 +145,7 @@ CONFIG = { 'secret': SECRET, 'host': HOST, 'port': PORT, + 'dryrun': DRYRUN, 'domain': { 'name': None, 'router': None diff --git a/halfapi/halfapi.py b/halfapi/halfapi.py index 0a73591..8ed1e57 100644 --- a/halfapi/halfapi.py +++ b/halfapi/halfapi.py @@ -52,11 +52,12 @@ class HalfAPI: SECRET = config.get('secret') PRODUCTION = config.get('production', True) CONFIG = config.get('config', {}) + DRYRUN = config.get('dryrun', False) domain = config.get('domain')['name'] router = config.get('domain').get('router', None) - if not (domain and router): + if not (domain): raise NoDomainsException() self.PRODUCTION = PRODUCTION @@ -95,6 +96,13 @@ class HalfAPI: for route in gen_domain_routes(m_domain_router): routes.append(route) + startup_fcts = [] + + if DRYRUN: + startup_fcts.append( + HalfAPI.wait_quit() + ) + self.__application = Starlette( debug=not PRODUCTION, routes=routes, @@ -104,7 +112,8 @@ class HalfAPI: 500: InternalServerErrorResponse, 501: NotImplementedResponse, 503: ServiceUnavailableResponse - } + }, + on_startup=startup_fcts ) self.__application.add_middleware( @@ -181,3 +190,12 @@ class HalfAPI: @staticmethod def api_schema(domain): pass + + @staticmethod + def wait_quit(): + """ sleeps 1 second and quits. used in dry-run mode + """ + import time + import sys + time.sleep(1) + sys.exit(0) diff --git a/halfapi/testing/test_domain.py b/halfapi/testing/test_domain.py index 26681a9..5c995a8 100644 --- a/halfapi/testing/test_domain.py +++ b/halfapi/testing/test_domain.py @@ -48,4 +48,8 @@ class TestDomain(TestCase): result = self.runner.invoke(cli, ['domain', self.DOMAIN]) self.assertEqual(result.exit_code, 0) result_d = json.loads(result.stdout) - return result.stdout + result = self.runner.invoke(cli, ['run', '--dryrun', self.DOMAIN]) + print(result.stdout) + self.assertEqual(result.exit_code, 0) + + return result_d