Skip to content

Commit

Permalink
feat: Add support for yaml configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Reillyhewitson committed Sep 22, 2022
1 parent 8eb3ea4 commit 02c3e41
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
25 changes: 25 additions & 0 deletions datagateway_api/config-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
datagateway_api:
extension: "/datagateway-api"
backend: "python_icat"
client_cache_size: 5
client_pool_init_size: 2
client_pool_max_size: 5
db_url: "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb"
icat_url: "https://localhost:8181"
icat_check_cert: false
search_api:
extension: "/search-api"
icat_url: "https://localhost:8181"
icat_check_cert: false
mechanism: "anon"
username: ""
password: ""
flask_reloader: false
log_level: "DEBUG"
log_location: "/root/datagateway-api/logs.log"
debug_mode: true
generate_swagger: true
host: "127.0.0.1"
port: "5000"
test_user_credentials: {"username": "root", "password": "pw"}
test_mechanism: "simple"
18 changes: 13 additions & 5 deletions datagateway_api/src/common/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import json, yaml
import logging
from pathlib import Path
import sys
Expand Down Expand Up @@ -176,7 +176,7 @@ class APIConfig(BaseModel):
test_user_credentials: Optional[TestUserCredentials]

@classmethod
def load(cls, path=Path(__file__).parent.parent.parent / "config.json"):
def load(cls, path=Path(__file__).parent.parent.parent / "config.yaml"):
"""
Loads the config data from the JSON file and returns it as a APIConfig pydantic
model. Exits the application if it fails to locate the JSON config file or
Expand All @@ -186,10 +186,18 @@ def load(cls, path=Path(__file__).parent.parent.parent / "config.json"):
:param path: path to the configuration file
:return: APIConfig model object that contains the config data
"""
log = logging.getLogger()
try:
with open(path, encoding="utf-8") as target:
data = json.load(target)
return cls(**data)
jpath = path.parent / "config.json" #Maybe move this to parameters? Will doing it like this break anything?
if path.exists():
with open(path, encoding="utf-8") as target:
data = yaml.safe_load(target)

elif jpath.exists():
log.warn("DEPRECATED: Using a JSON config file is deprecated and may be removed in an upcoming version. Please use a yaml config file instead.") #Does this message need to exist?
with open(jpath, encoding="utf-8") as target:
data = json.load(target)
return cls(**data)
except (IOError, ValidationError) as error:
sys.exit(f"An error occurred while trying to load the config data: {error}")

Expand Down

0 comments on commit 02c3e41

Please sign in to comment.