From 0d0d49d25728d73649cb5c0b8c30190658ccf9da Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Tue, 19 Apr 2022 16:08:20 +0200 Subject: [PATCH] [wip] 0.7.0 --- Pipfile | 2 +- halfapi/__init__.py | 2 +- halfapi/cli/run.py | 2 +- halfapi/conf.py | 27 ++++++++++++++++++++++++--- halfapi/halfapi.py | 25 +++++++++++++++++++++---- halfapi/logging.py | 5 ++++- setup.py | 2 +- tests/test_halfapi.py | 1 + 8 files changed, 54 insertions(+), 12 deletions(-) diff --git a/Pipfile b/Pipfile index 7f00c88..932648e 100644 --- a/Pipfile +++ b/Pipfile @@ -16,7 +16,7 @@ virtualenv = "*" [packages] click = ">=7.1,<8" -starlette = ">=0.17,<0.18" +starlette = ">=0.19,<0.20" uvicorn = ">=0.13,<1" orjson = ">=3.4.7,<4" pyjwt = ">=2.3.0,<2.4.0" diff --git a/halfapi/__init__.py b/halfapi/__init__.py index a362c75..167382d 100644 --- a/halfapi/__init__.py +++ b/halfapi/__init__.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -__version__ = '0.6.21-rc0' +__version__ = '0.7.0' def version(): return f'HalfAPI version:{__version__}' diff --git a/halfapi/cli/run.py b/halfapi/cli/run.py index 164f871..060d850 100644 --- a/halfapi/cli/run.py +++ b/halfapi/cli/run.py @@ -18,7 +18,7 @@ from ..half_domain import HalfDomain @click.option('--port', default=CONFIG.get('port')) @click.option('--reload', default=False) @click.option('--secret', default=CONFIG.get('secret')) -@click.option('--production', default=CONFIG.get('secret')) +@click.option('--production', default=CONFIG.get('production')) @click.option('--loglevel', default=CONFIG.get('loglevel')) @click.option('--prefix', default='/') @click.option('--check', default=True) diff --git a/halfapi/conf.py b/halfapi/conf.py index 3a57aff..6396081 100644 --- a/halfapi/conf.py +++ b/halfapi/conf.py @@ -53,6 +53,9 @@ LOGLEVEL = 'info' CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config') DRYRUN = bool(os.environ.get('HALFAPI_DRYRUN', False)) +REDIS_HOST = '' +REDIS_PORT = '6379' + SCHEMA = {} CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api') @@ -146,9 +149,15 @@ try: except FileNotFoundError as exc: logger.info('Running without secret file: %s', SECRET or 'no file specified') -PRODUCTION = bool(CONFIG.get( - 'production', - environ.get('HALFAPI_PROD', True))) +PRODUCTION = CONFIG.get('production', None) +if PRODUCTION is None: + if environ.get('HALFAPI_PROD', True): + PRODUCTION = not ( + environ.get('HALFAPI_PROD') in ('False', '0', 0, '', 'false')) + else: + PRODUCTION = True + + LOGLEVEL = CONFIG.get( 'loglevel', @@ -158,9 +167,21 @@ BASE_DIR = CONFIG.get( 'base_dir', environ.get('HALFAPI_BASE_DIR', '.')) +# Redis + +REDIS_HOST = CONFIG.get( + 'redis_host', + environ.get('HALFAPI_REDIS_HOST', REDIS_HOST)) + +REDIS_PORT = CONFIG.get( + 'redis_port', + environ.get('HALFAPI_REDIS_PORT', REDIS_PORT)) + CONFIG['project_name'] = PROJECT_NAME CONFIG['production'] = PRODUCTION CONFIG['secret'] = SECRET CONFIG['host'] = HOST CONFIG['port'] = PORT CONFIG['dryrun'] = DRYRUN +if len(REDIS_HOST): + CONFIG['redis_url'] = f'redis://{REDIS_HOST}:{REDIS_PORT}' diff --git a/halfapi/halfapi.py b/halfapi/halfapi.py index 74c627c..71a1794 100644 --- a/halfapi/halfapi.py +++ b/halfapi/halfapi.py @@ -52,6 +52,7 @@ class HalfAPI(Starlette): SECRET = self.config.get('secret') PRODUCTION = self.config.get('production', True) DRYRUN = self.config.get('dryrun', False) + REDIS = self.config.get('redis_url', False) self.PRODUCTION = PRODUCTION self.SECRET = SECRET @@ -85,6 +86,11 @@ class HalfAPI(Starlette): startup_fcts.append( HalfAPI.wait_quit() ) + + if REDIS: + startup_fcts.append( + HalfAPI.connect_redis(REDIS) + ) super().__init__( debug=not PRODUCTION, @@ -207,10 +213,21 @@ class HalfAPI(Starlette): def wait_quit(): """ sleeps 1 second and quits. used in dry-run mode """ - import time - import sys - time.sleep(1) - sys.exit(0) + def wrapped(): + import time + import sys + time.sleep(1) + sys.exit(0) + + return wrapped + + @staticmethod + def connect_redis(redis_url): + def wrapped(): + import redis + connection = redis.from_url(redis_url) + return wrapped + def acls_route(self): module = None diff --git a/halfapi/logging.py b/halfapi/logging.py index 0e34e02..508a264 100644 --- a/halfapi/logging.py +++ b/halfapi/logging.py @@ -1,7 +1,10 @@ import logging +import os +LOGLEVEL = os.environ.get('HALFAPI_LOGLEVEL', 'INFO') +DEFAULT_LEVEL = getattr(logging, LOGLEVEL, logging.INFO) -def config_logging(level=logging.INFO): +def config_logging(level=DEFAULT_LEVEL): # When run by 'uvicorn ...', a root handler is already # configured and the basicConfig below does nothing. diff --git a/setup.py b/setup.py index 82a48b2..ae92daf 100755 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ setup( python_requires=">=3.8", install_requires=[ "PyJWT>=2.3.0,<2.4.0", - "starlette>=0.17,<0.18", + "starlette>=0.19,<0.20", "click>=7.1,<8", "uvicorn>=0.13,<1", "orjson>=3.4.7,<4", diff --git a/tests/test_halfapi.py b/tests/test_halfapi.py index eb6ca97..1fde86b 100644 --- a/tests/test_halfapi.py +++ b/tests/test_halfapi.py @@ -4,3 +4,4 @@ def test_methods(): assert 'application' in dir(HalfAPI) assert 'version' in dir(HalfAPI) assert 'version_async' in dir(HalfAPI) + assert 'connect_redis' in dir(HalfAPI)