[wip] 0.7.0

This commit is contained in:
Maxime Alves LIRMM 2022-04-19 16:08:20 +02:00
parent 6bb6abcbd4
commit 0d0d49d257
8 changed files with 54 additions and 12 deletions

View File

@ -16,7 +16,7 @@ virtualenv = "*"
[packages] [packages]
click = ">=7.1,<8" click = ">=7.1,<8"
starlette = ">=0.17,<0.18" starlette = ">=0.19,<0.20"
uvicorn = ">=0.13,<1" uvicorn = ">=0.13,<1"
orjson = ">=3.4.7,<4" orjson = ">=3.4.7,<4"
pyjwt = ">=2.3.0,<2.4.0" pyjwt = ">=2.3.0,<2.4.0"

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
__version__ = '0.6.21-rc0' __version__ = '0.7.0'
def version(): def version():
return f'HalfAPI version:{__version__}' return f'HalfAPI version:{__version__}'

View File

@ -18,7 +18,7 @@ from ..half_domain import HalfDomain
@click.option('--port', default=CONFIG.get('port')) @click.option('--port', default=CONFIG.get('port'))
@click.option('--reload', default=False) @click.option('--reload', default=False)
@click.option('--secret', default=CONFIG.get('secret')) @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('--loglevel', default=CONFIG.get('loglevel'))
@click.option('--prefix', default='/') @click.option('--prefix', default='/')
@click.option('--check', default=True) @click.option('--check', default=True)

View File

@ -53,6 +53,9 @@ LOGLEVEL = 'info'
CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config') CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config')
DRYRUN = bool(os.environ.get('HALFAPI_DRYRUN', False)) DRYRUN = bool(os.environ.get('HALFAPI_DRYRUN', False))
REDIS_HOST = ''
REDIS_PORT = '6379'
SCHEMA = {} SCHEMA = {}
CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api') CONF_DIR = environ.get('HALFAPI_CONF_DIR', '/etc/half_api')
@ -146,9 +149,15 @@ try:
except FileNotFoundError as exc: except FileNotFoundError as exc:
logger.info('Running without secret file: %s', SECRET or 'no file specified') logger.info('Running without secret file: %s', SECRET or 'no file specified')
PRODUCTION = bool(CONFIG.get( PRODUCTION = CONFIG.get('production', None)
'production', if PRODUCTION is None:
environ.get('HALFAPI_PROD', True))) if environ.get('HALFAPI_PROD', True):
PRODUCTION = not (
environ.get('HALFAPI_PROD') in ('False', '0', 0, '', 'false'))
else:
PRODUCTION = True
LOGLEVEL = CONFIG.get( LOGLEVEL = CONFIG.get(
'loglevel', 'loglevel',
@ -158,9 +167,21 @@ BASE_DIR = CONFIG.get(
'base_dir', 'base_dir',
environ.get('HALFAPI_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['project_name'] = PROJECT_NAME
CONFIG['production'] = PRODUCTION CONFIG['production'] = PRODUCTION
CONFIG['secret'] = SECRET CONFIG['secret'] = SECRET
CONFIG['host'] = HOST CONFIG['host'] = HOST
CONFIG['port'] = PORT CONFIG['port'] = PORT
CONFIG['dryrun'] = DRYRUN CONFIG['dryrun'] = DRYRUN
if len(REDIS_HOST):
CONFIG['redis_url'] = f'redis://{REDIS_HOST}:{REDIS_PORT}'

View File

@ -52,6 +52,7 @@ class HalfAPI(Starlette):
SECRET = self.config.get('secret') SECRET = self.config.get('secret')
PRODUCTION = self.config.get('production', True) PRODUCTION = self.config.get('production', True)
DRYRUN = self.config.get('dryrun', False) DRYRUN = self.config.get('dryrun', False)
REDIS = self.config.get('redis_url', False)
self.PRODUCTION = PRODUCTION self.PRODUCTION = PRODUCTION
self.SECRET = SECRET self.SECRET = SECRET
@ -85,6 +86,11 @@ class HalfAPI(Starlette):
startup_fcts.append( startup_fcts.append(
HalfAPI.wait_quit() HalfAPI.wait_quit()
) )
if REDIS:
startup_fcts.append(
HalfAPI.connect_redis(REDIS)
)
super().__init__( super().__init__(
debug=not PRODUCTION, debug=not PRODUCTION,
@ -207,10 +213,21 @@ class HalfAPI(Starlette):
def wait_quit(): def wait_quit():
""" sleeps 1 second and quits. used in dry-run mode """ sleeps 1 second and quits. used in dry-run mode
""" """
import time def wrapped():
import sys import time
time.sleep(1) import sys
sys.exit(0) 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): def acls_route(self):
module = None module = None

View File

@ -1,7 +1,10 @@
import logging 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 # When run by 'uvicorn ...', a root handler is already
# configured and the basicConfig below does nothing. # configured and the basicConfig below does nothing.

View File

@ -44,7 +44,7 @@ setup(
python_requires=">=3.8", python_requires=">=3.8",
install_requires=[ install_requires=[
"PyJWT>=2.3.0,<2.4.0", "PyJWT>=2.3.0,<2.4.0",
"starlette>=0.17,<0.18", "starlette>=0.19,<0.20",
"click>=7.1,<8", "click>=7.1,<8",
"uvicorn>=0.13,<1", "uvicorn>=0.13,<1",
"orjson>=3.4.7,<4", "orjson>=3.4.7,<4",

View File

@ -4,3 +4,4 @@ def test_methods():
assert 'application' in dir(HalfAPI) assert 'application' in dir(HalfAPI)
assert 'version' in dir(HalfAPI) assert 'version' in dir(HalfAPI)
assert 'version_async' in dir(HalfAPI) assert 'version_async' in dir(HalfAPI)
assert 'connect_redis' in dir(HalfAPI)