[cli-conf] halfapi domain : the file provided as argument is a toml file of the format of .halfapi/config, + better config handling

This commit is contained in:
maxime 2023-08-02 08:16:30 +02:00
parent 4856f80b99
commit 87856cfb42
6 changed files with 38 additions and 39 deletions

View File

@ -1,9 +1,11 @@
import os
from .halfapi import HalfAPI
from .conf import CONFIG, SCHEMA
from .logging import logger
from .conf import read_config
logger.info('CONFIG: %s', CONFIG)
logger.info('SCHEMA: %s', SCHEMA)
def application():
config_file = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config')
application = HalfAPI(
CONFIG, SCHEMA or None).application
CONFIG = read_config([config_file])
return HalfAPI(CONFIG).application

View File

@ -9,6 +9,7 @@ import importlib
import subprocess
import json
import toml
import click
import orjson
@ -16,7 +17,6 @@ import uvicorn
from .cli import cli
from ..conf import CONFIG
from ..half_domain import HalfDomain
@ -145,6 +145,14 @@ def domain(domain, config_file, delete, update, create, read, run, dry_run): #,
# TODO: Connect to the create_domain function
raise NotImplementedError
raise Exception('Missing domain name')
if config_file:
CONFIG = toml.load(config_file.name)
os.environ['HALFAPI_CONF_FILE'] = config_file.name
else:
from halfapi.conf import CONFIG
if create:
raise NotImplementedError
elif update:
@ -152,14 +160,8 @@ def domain(domain, config_file, delete, update, create, read, run, dry_run): #,
elif delete:
raise NotImplementedError
elif read:
from ..conf import CONFIG
from ..halfapi import HalfAPI
if config_file:
CONFIG = json.loads(''.join(
[ line.decode() for line in config_file.readlines() ]
))
halfapi = HalfAPI(CONFIG)
click.echo(orjson.dumps(
halfapi.domains[domain].schema(),
@ -168,25 +170,13 @@ def domain(domain, config_file, delete, update, create, read, run, dry_run): #,
)
else:
from ..conf import CONFIG
if 'domain' not in CONFIG:
CONFIG['domain'] = {}
if domain not in CONFIG['domain']:
CONFIG['domain'][domain] = {
'enabled': True,
'name': domain
}
if dry_run:
CONFIG['dryrun'] = True
CONFIG['domain'][domain]['enabled'] = True
port = CONFIG['domain'][domain].get('port', 3000)
port = CONFIG.get('port',
CONFIG.get('domain', {}).get('port')
)
uvicorn.run(
'halfapi.app:application',
port=port
port=port,
factory=True
)

View File

@ -36,7 +36,7 @@ It follows the following format :
"""
import logging
from .logging import logger
import os
from os import environ
import sys
@ -46,8 +46,6 @@ import uuid
import toml
from .logging import logger
PRODUCTION = True
LOGLEVEL = 'info'
CONF_FILE = os.environ.get('HALFAPI_CONF_FILE', '.halfapi/config')
@ -77,17 +75,18 @@ except FileNotFoundError:
logger.error('Cannot find a configuration file under %s', HALFAPI_DOT_FILE)
def read_config():
def read_config(filenames=HALFAPI_CONFIG_FILES):
"""
The highest index in "filenames" are the highest priorty
"""
d_res = {}
logger.info('Reading config files %s', HALFAPI_CONFIG_FILES)
for CONF_FILE in HALFAPI_CONFIG_FILES:
d_res.update( toml.load(HALFAPI_CONFIG_FILES) )
logger.info('Reading config files %s', filenames)
for CONF_FILE in filenames:
if os.path.isfile(CONF_FILE):
d_res.update( toml.load(CONF_FILE) )
logger.info('Reading config files (result) %s', d_res)
logger.info('Read config files (result) %s', d_res)
return { **d_res.get('project', {}), 'domain': d_res.get('domain', {}) }
CONFIG = read_config()

View File

@ -9,6 +9,7 @@ It defines the following globals :
- application (the asgi application itself - a starlette object)
"""
import sys
import logging
import time
import importlib
@ -48,6 +49,7 @@ class HalfAPI(Starlette):
d_routes=None):
config_logging(logging.DEBUG)
self.config = config
logger.debug('HalfAPI.config: %s', self.config)
SECRET = self.config.get('secret')
PRODUCTION = self.config.get('production', True)

View File

@ -4,6 +4,7 @@ import os
import sys
import json
from json.decoder import JSONDecodeError
import toml
from unittest import TestCase
from starlette.testclient import TestClient
from click.testing import CliRunner
@ -79,7 +80,7 @@ class TestDomain(TestCase):
_, self.config_file = tempfile.mkstemp()
with open(self.config_file, 'w') as fh:
fh.write(json.dumps(self.halfapi_conf))
fh.write(toml.dumps(self.halfapi_conf))
self.halfapi = HalfAPI(self.halfapi_conf)

View File

@ -5,6 +5,7 @@ import importlib
import tempfile
from unittest.mock import patch
import json
import toml
import pytest
from click.testing import CliRunner
@ -29,12 +30,16 @@ class TestCliProj():
_, tmp_conf = tempfile.mkstemp()
with open(tmp_conf, 'w') as fh:
fh.write(
json.dumps({
toml.dumps({
'domain': {
'dummy_domain': {
'port': 4242,
'name': 'dummy_domain',
'enabled': True
}
},
'project': {
'dryrun': True
}
})
)