forked from Sunbird-AIAssistant/sakhi-api-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_util.py
27 lines (20 loc) · 904 Bytes
/
config_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
from configparser import ConfigParser
from fastapi import HTTPException, status
from logger import logger
config_file_path = 'config.ini' # Update with your config file path
config = ConfigParser()
config.read(config_file_path)
def get_config_value(section, key, default=None):
# Check if the key exists in the environment variables
value = os.getenv(key, default)
# If the key is not in the environment variables, try reading from a config file
if value is None or value == "":
# Attempt to read the config file
try:
value = config.get(section, key, fallback=default)
except Exception as e:
logger.error(
{"Exception": f"Error reading config file: {e}"})
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Error while reading configuration: "+key)
return value