-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add environment variable support and Docker configuration\n\n- …
…Added .env support for the application using python-dotenv.\n- Updated Dockerfile to copy .env file for Docker configuration.\n- Added configuration module to handle environment variables.\n- Refactored database configuration to use environment variables.\n- Updated README.md to include information about environment variable configuration.\n- Removed Python version backport for importlib.metadata.\n- Added new run_gull_api module for running the app with uvicorn with CLI arguments.\n- Added tests for the new changes.
- Loading branch information
Showing
12 changed files
with
148 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DB_URI=sqlite:////app/data/database.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
try: | ||
from importlib.metadata import version, PackageNotFoundError | ||
except ImportError: | ||
# For Python versions < 3.8, use the backport module | ||
from importlib_metadata import version, PackageNotFoundError | ||
from importlib.metadata import version, PackageNotFoundError | ||
|
||
try: | ||
# Change 'gull-api' to the name of your package as it appears in pyproject.toml | ||
__version__ = version('gull-api') | ||
except PackageNotFoundError: | ||
# Package is not installed | ||
__version__ = None | ||
__version__ = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
# Load environment variables from .env file | ||
load_dotenv() | ||
|
||
# Define configuration variables | ||
CLI_JSON_PATH = os.getenv("CLI_JSON_PATH", "cli.json") | ||
DB_URI = os.getenv("DB_URI", "sqlite:///./database.db") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import argparse | ||
|
||
|
||
def create_parser(): | ||
parser = argparse.ArgumentParser(description='Run GULL-API using uvicorn.') | ||
|
||
# Add arguments similar to what uvicorn accepts | ||
parser.add_argument('--host', default='0.0.0.0', type=str, help='Bind socket to this host (default: 0.0.0.0)') | ||
parser.add_argument('--port', default=8000, type=int, help='Bind socket to this port (default: 8000)') | ||
parser.add_argument('--log-level', default='info', type=str, help='Log level (default: info)') | ||
parser.add_argument('--workers', default=1, type=int, help='Number of worker processes (default: 1)') | ||
parser.add_argument('--reload', action='store_true', help='Enable auto-reload') | ||
parser.add_argument('--reload-dir', default=None, type=str, help='Set reload directories explicitly, instead of using the current working directory') | ||
|
||
return parser | ||
|
||
|
||
def run_uvicorn(args, uvicorn_module): | ||
# Call uvicorn.run with the user-specified options | ||
uvicorn_module.run( | ||
"gull_api.main:app", | ||
host=args.host, | ||
port=args.port, | ||
log_level=args.log_level, | ||
workers=args.workers, | ||
reload=args.reload, | ||
reload_dirs=[args.reload_dir] if args.reload_dir else None | ||
) | ||
|
||
|
||
def main(): | ||
parser = create_parser() | ||
args = parser.parse_args() | ||
|
||
# Import uvicorn here so it can be mocked in tests | ||
import uvicorn | ||
run_uvicorn(args, uvicorn) | ||
|
||
|
||
if __name__ == "__main__": # pragma: no cover | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
import importlib | ||
from importlib.metadata import PackageNotFoundError | ||
import gull_api | ||
|
||
|
||
def raise_package_not_found_error(name): | ||
raise PackageNotFoundError("Simulated PackageNotFoundError") | ||
|
||
|
||
def test_package_not_found(monkeypatch): | ||
# Use monkeypatch to override importlib.metadata.version function to raise PackageNotFoundError | ||
monkeypatch.setattr('importlib.metadata.version', raise_package_not_found_error) | ||
|
||
# Reload gull_api to force re-execution of its __init__.py with the mocked version function | ||
importlib.reload(gull_api) | ||
|
||
# Check that __version__ is set to None when PackageNotFoundError is raised | ||
assert gull_api.__version__ is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pytest | ||
from unittest.mock import Mock, patch, ANY | ||
from gull_api.run_gull_api import create_parser, run_uvicorn, main | ||
|
||
|
||
def test_run_uvicorn(): | ||
parser = create_parser() | ||
args = parser.parse_args(['--host', '127.0.0.1', '--port', '8080']) | ||
|
||
mock_uvicorn = Mock() | ||
|
||
run_uvicorn(args, mock_uvicorn) | ||
|
||
mock_uvicorn.run.assert_called_once_with( | ||
"gull_api.main:app", | ||
host='127.0.0.1', | ||
port=8080, | ||
log_level='info', | ||
workers=1, | ||
reload=False, | ||
reload_dirs=None | ||
) | ||
|
||
|
||
def test_main_function(): | ||
with patch('gull_api.run_gull_api.run_uvicorn') as mock_run_uvicorn: | ||
with patch('argparse.ArgumentParser.parse_args') as mock_parse_args: | ||
mock_args = Mock() | ||
mock_args.host = '0.0.0.0' | ||
mock_args.port = 8000 | ||
mock_args.log_level = 'info' | ||
mock_args.workers = 1 | ||
mock_args.reload = False | ||
mock_args.reload_dir = None | ||
mock_parse_args.return_value = mock_args | ||
|
||
main() | ||
|
||
# Assert that the run_uvicorn function was called with the correct arguments | ||
mock_run_uvicorn.assert_called_once_with(mock_args, ANY) |