Skip to content

Commit

Permalink
Read from database ini
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshhewabi committed Dec 5, 2023
1 parent 645d9dc commit 2d772a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
14 changes: 2 additions & 12 deletions app/routes/pride.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from app.models.spectrumidentificationprotocol import SpectrumIdentificationProtocol
from index import get_session
from process_dataset import convert_pxd_accession_from_pride
from db_config_parser import security_API_key

import logging
import logging.config
Expand All @@ -35,18 +36,7 @@


def get_api_key(key: str = Security(api_key_header)) -> str:
# Get the path to the mounted config directory from the environment variable, if not get default
database_ini_path = os.environ.get('DATABASE_INI_PATH', 'database.ini')
logger.info("database_ini_path: " + database_ini_path)
API_KEY = ""
if database_ini_path:
config.read(database_ini_path)
API_KEY = config.get('security', 'apikey')
logger.info(API_KEY)
else:
print("Error: DATABASE_INI_PATH environment variable not set.")

if key == API_KEY:
if key == security_API_key():
return key
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down
17 changes: 12 additions & 5 deletions db_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@
import os


def parse_database_info(filename, section='postgresql'):
def parse_info(filename, section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)

# get section, default to postgresql
db = {}
section_info = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
section_info[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
return section_info


def get_conn_str():
config = os.environ.get('DB_CONFIG', 'database.ini')
db_info = parse_database_info(config)
db_info = parse_info(config, 'postgresql')
hostname = db_info.get("host")
database = db_info.get("database")
username = db_info.get("user")
password = db_info.get("password")
port = db_info.get("port")
conn_str = f"postgresql://{username}:{password}@{hostname}:{port}/{database}"
return conn_str


def security_API_key():
config = os.environ.get('DB_CONFIG', 'database.ini')
security_info = parse_info(config, 'security')
apikey = security_info.get("apikey")
return apikey

0 comments on commit 2d772a3

Please sign in to comment.