[conf] removed HALFORM_DSN from the env parameters, added support for the

env_file uvicorn parameter (specified by --envfile for the halfapi "run"
command), added support for default environment variables in pytest.ini
This commit is contained in:
Maxime Alves LIRMM 2020-07-09 10:23:13 +02:00
parent 644e5c5d3d
commit 44948be228
5 changed files with 51 additions and 65 deletions

View File

@ -1,3 +1,5 @@
HALFORM_SECRET="halform_secret"
HALFORM_DSN="dbname=api user=api password= host=127.0.0.1 port=5432"
HALFORM_SECRET=halform_secret
HALFORM_DSN=dbname=api user=api password= host=127.0.0.1 port=5432
DEV=1
DEBUG=1
DEBUG_ACL=public

View File

@ -79,8 +79,8 @@ def mount_domains(app: ASGIApp, domains: list):
def startup():
# This function is called at the instanciation of *app*
global app
# Mount the registered domains
try:
domains_list = [elt for elt in Domain().select()]
@ -97,9 +97,6 @@ def check_conf():
environ['HALFORM_SECRET'] = open('/etc/half_orm/secret').read()
print('Missing HALFORM_SECRET variable from configuration, seting to default')
if not environ.get('HALFORM_DSN', False):
print('Missing HALFORM_DSN variable from configuration')
CONFIG={
'DEBUG' : 'DEBUG' in environ.keys()
}

View File

@ -35,26 +35,21 @@ def cli(ctx):
@click.option('--envfile', default=None)
@click.option('--host', default='127.0.0.1')
@click.option('--port', default='8000')
@click.option('--debug', default=False)
@click.option('--dev', default=True)
@click.option('--dbname', default='api')
@click.option('--dbhost', default='127.0.0.1')
@click.option('--dbport', default=5432)
@click.option('--dbuser', default='api')
@click.option('--dbpassword', default='')
@cli.command()
def run(envfile, host, port, debug, dev, dbname, dbhost, dbport, dbuser, dbpassword):
def run(envfile, host, port):
local_env = {}
if envfile:
try:
with open(envfile) as f:
print('Will use the following env parameters')
print(f.readlines())
pass
print('Will use the following env parameters :')
local_env = dict([ tuple(line.strip().split('=', 1))
for line in f.readlines() ])
print(local_env)
except FileNotFoundError:
print(f'No file named {envfile}')
envfile = None
if dev:
if 'DEV' in local_env.keys():
debug = True
reload = True
log_level = 'debug'
@ -62,41 +57,13 @@ def run(envfile, host, port, debug, dev, dbname, dbhost, dbport, dbuser, dbpassw
reload = False
log_level = 'info'
# Helper function to convert the string-based dsn to a dict
dsntodict = lambda dsn: dict(
map(lambda x:
map(lambda y: y.strip("'\""),
x.split('=')
),
dsn.split()))
dicttodsn = lambda dsn_d: (' '.join(
[ '{key}={val}'.format(key=key, val=dsn_d[key])
for key in dsn_d.keys()
]
))
click.echo('Launching application with default parameters')
click.echo(f'''Parameters : \n
Host : {host}
Port : {port}
Debug : {debug}
Dev : {dev}''')
HALFORM_DSN=os.environ.get('HALFORM_DSN', '')
db_params = dsntodict(HALFORM_DSN)
db_params['dbname'] = db_params.get('dbname', dbname)
db_params['host'] = db_params.get('host', dbhost)
db_params['port'] = db_params.get('port', dbport)
db_params['user'] = db_params.get('user', dbuser)
db_params['password'] = db_params.get('password', dbpassword)
os.environ['HALFORM_DSN'] = dicttodsn(db_params)
click.echo('Launching application')
check_conf()
sys.path.insert(0, os.getcwd())
click.echo(sys.path)
click.echo(f'Current PYTHON_PATH : {sys.path}')
uvicorn.run('halfapi.app:app',
env_file=envfile,
host=host,

50
poetry.lock generated
View File

@ -54,14 +54,6 @@ optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
version = "0.4.3"
[[package]]
category = "main"
description = "Handle .env files"
name = "dotenv"
optional = false
python-versions = "*"
version = "0.0.5"
[[package]]
category = "main"
description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
@ -158,15 +150,15 @@ python-versions = "^3.7"
version = "0.1.0"
[package.dependencies]
fastapi = "*"
fastapi = "^0"
half-orm = "branch master"
halfapi = "branch master"
sidb = "branch master"
starlette = "^0.13.0"
uvicorn = "*"
starlette = "^0"
uvicorn = "^0"
[package.source]
reference = "4f9cf92253b2ee526a528724877840c8ec158d3a"
reference = "f26fc6dcde165bfbb88d4664f842de33aacfb1f8"
type = "git"
url = "git@gite.lirmm.fr:newsi/api/organigramme.git"
@ -274,6 +266,28 @@ version = ">=0.12"
checkqa-mypy = ["mypy (v0.761)"]
testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"]
[[package]]
category = "dev"
description = "py.test plugin that allows you to add environment variables."
name = "pytest-env"
optional = false
python-versions = "*"
version = "0.6.2"
[package.dependencies]
pytest = ">=2.6.0"
[[package]]
category = "main"
description = "Add .env support to your django/flask apps in development and deployments"
name = "python-dotenv"
optional = false
python-versions = "*"
version = "0.14.0"
[package.extras]
cli = ["click (>=5.0)"]
[[package]]
category = "main"
description = "YAML parser and emitter for Python"
@ -408,7 +422,7 @@ testing = ["jaraco.itertools", "func-timeout"]
organigramme = ["fastapi", "organigramme"]
[metadata]
content-hash = "56e1f269ca07039fa6898fa427261f61cab9ef871c5a82edb592cbfb197c2d03"
content-hash = "b801de5aea1ab2defb9265cc25773b55f7c00e36e7206e19173cef5a0ee99eb4"
python-versions = "^3.7"
[metadata.files]
@ -436,9 +450,6 @@ colorama = [
{file = "colorama-0.4.3-py2.py3-none-any.whl", hash = "sha256:7d73d2a99753107a36ac6b455ee49046802e59d9d076ef8e47b61499fa29afff"},
{file = "colorama-0.4.3.tar.gz", hash = "sha256:e96da0d330793e2cb9485e9ddfd918d456036c7149416295932478192f4436a1"},
]
dotenv = [
{file = "dotenv-0.0.5.tar.gz", hash = "sha256:b58d2ab3f83dbd4f8a362b21158a606bee87317a9444485566b3c8f0af847091"},
]
fastapi = [
{file = "fastapi-0.58.1-py3-none-any.whl", hash = "sha256:d7499761d5ca901cdf5b6b73018d14729593f8ab1ea22d241f82fa574fc406ad"},
{file = "fastapi-0.58.1.tar.gz", hash = "sha256:92e59b77eef7d6eaa80b16d275adda06b5f33b12d777e3fc5521b2f7f4718e13"},
@ -550,6 +561,13 @@ pytest = [
{file = "pytest-5.4.3-py3-none-any.whl", hash = "sha256:5c0db86b698e8f170ba4582a492248919255fcd4c79b1ee64ace34301fb589a1"},
{file = "pytest-5.4.3.tar.gz", hash = "sha256:7979331bfcba207414f5e1263b5a0f8f521d0f457318836a7355531ed1a4c7d8"},
]
pytest-env = [
{file = "pytest-env-0.6.2.tar.gz", hash = "sha256:7e94956aef7f2764f3c147d216ce066bf6c42948bb9e293169b1b1c880a580c2"},
]
python-dotenv = [
{file = "python-dotenv-0.14.0.tar.gz", hash = "sha256:8c10c99a1b25d9a68058a1ad6f90381a62ba68230ca93966882a4dbc3bc9c33d"},
{file = "python_dotenv-0.14.0-py2.py3-none-any.whl", hash = "sha256:c10863aee750ad720f4f43436565e4c1698798d763b63234fb5021b6c616e423"},
]
pyyaml = [
{file = "PyYAML-5.3.1-cp27-cp27m-win32.whl", hash = "sha256:74809a57b329d6cc0fdccee6318f44b9b8649961fa73144a98735b0aaf029f1f"},
{file = "PyYAML-5.3.1-cp27-cp27m-win_amd64.whl", hash = "sha256:240097ff019d7c70a4922b6869d8a86407758333f02203e0fc6ff79c5dcede76"},

View File

@ -14,10 +14,12 @@ starlette = "^0"
uvicorn = { version = "^0" }
fastapi = { version = "^0", optional = true }
organigramme = { git = "git@gite.lirmm.fr:newsi/api/organigramme.git", optional = true }
python-dotenv = "^0.14.0"
[tool.poetry.dev-dependencies]
pytest = "^5"
requests = "^2"
pytest-env = "^0.6.2"
[tool.poetry.extras]
organigramme = [ "fastapi", "organigramme" ]