Skip to content

Commit 98dafba

Browse files
Brain256Brain256
andauthored
Create Backend Config Manager (#614)
# Purpose Closes #335 Create a BackendConfiguration class to store backend config info (logger excluded params and CORS config settings) # New Changes - added .*.env to the .gitignore - created Pydantic classes to represent CORS and logger configurations - created a BackendConfiguration class to store CORS and logger config settings - changed middleware and setup files to take info from an instance of the BackendConfiguration class # Testing Explain tests that you ran to verify code functionality. - [x] I have unit-tested this PR. Otherwise, explain why it cannot be unit-tested. - [x] I have included screenshots of the tests performed below. <img width="555" height="54" alt="image" src="https://github.com/user-attachments/assets/6cda094c-4923-4637-ae5a-0890af3757c9" /> # Outstanding Changes If there are non-critical changes (i.e. additional features) that can be made to this feature in the future, indicate them here. --------- Co-authored-by: Brain256 <brian@BrianYoga7.localdomain>
1 parent a07cca9 commit 98dafba

File tree

8 files changed

+84
-5
lines changed

8 files changed

+84
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ compile_commands.json
6464
*.sw?
6565

6666
.env
67+
.*.env
6768
*.db
6869

6970
/build*

gs/backend/api/backend_setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from gs.backend.api.v1.mcc.endpoints.commands import commands_router
1010
from gs.backend.api.v1.mcc.endpoints.main_commands import main_commands_router
1111
from gs.backend.api.v1.mcc.endpoints.telemetry import telemetry_router
12+
from gs.backend.config.config import backend_config
1213

1314

1415
def setup_routes(app: FastAPI) -> None:
@@ -32,4 +33,7 @@ def setup_middlewares(app: FastAPI) -> None:
3233
"""Adds the middlewares to the app"""
3334
add_cors_middleware(app) # Cors middleware should be added first
3435
app.add_middleware(AuthMiddleware)
35-
app.add_middleware(LoggerMiddleware, excluded_endpoints=[])
36+
app.add_middleware(
37+
LoggerMiddleware,
38+
excluded_endpoints=backend_config.logger_config.excluded_endpoints,
39+
)
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from fastapi import FastAPI
22
from fastapi.middleware.cors import CORSMiddleware
33

4+
from gs.backend.config.config import backend_config
5+
46

57
def add_cors_middleware(app: FastAPI) -> None:
68
"""
@@ -10,8 +12,8 @@ def add_cors_middleware(app: FastAPI) -> None:
1012
"""
1113
app.add_middleware(
1214
CORSMiddleware,
13-
allow_origins=["http://localhost:5173"],
14-
allow_credentials=True,
15-
allow_methods=["*"],
16-
allow_headers=["*"],
15+
allow_origins=backend_config.cors_config.allow_origins,
16+
allow_credentials=backend_config.cors_config.allow_credentials,
17+
allow_methods=backend_config.cors_config.allow_methods,
18+
allow_headers=backend_config.cors_config.allow_headers,
1719
)

gs/backend/config/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@
44

55
from dotenv import load_dotenv
66

7+
from .cors_config import CORSConfig
8+
from .logger_config import LoggerConfig
9+
710
load_dotenv()
811

912

13+
class BackendConfiguration:
14+
"""
15+
Class for storing backend configuration settings
16+
"""
17+
18+
def __init__(self) -> None:
19+
self.cors_config = CORSConfig()
20+
self.logger_config = LoggerConfig()
21+
22+
23+
backend_config = BackendConfiguration()
24+
1025
# TODO: Make these throw an exception if they are None
1126
GS_DATABASE_USER = environ.get("GS_DATABASE_USER")
1227
GS_DATABASE_PASSWORD = environ.get("GS_DATABASE_PASSWORD")

gs/backend/config/cors_config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pydantic_settings import BaseSettings, SettingsConfigDict
2+
3+
4+
class CORSConfig(BaseSettings):
5+
"""
6+
Pydantic class for storing CORS middleware configuration settings
7+
"""
8+
9+
model_config = SettingsConfigDict(env_prefix="CORS_")
10+
11+
allow_origins: list[str] = ["http://localhost:5173"]
12+
allow_credentials: bool = True
13+
allow_methods: list[str] = ["*"]
14+
allow_headers: list[str] = ["*"]

gs/backend/config/logger_config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from collections.abc import Sequence
2+
3+
from pydantic_settings import BaseSettings, SettingsConfigDict
4+
5+
6+
class LoggerConfig(BaseSettings):
7+
"""
8+
Pydantic class for storing logger middleware configuration settings
9+
"""
10+
11+
model_config = SettingsConfigDict(env_prefix="LOGGER_")
12+
13+
excluded_endpoints: Sequence[str] = []

python_test/test_config_manager.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import importlib
2+
3+
from gs.backend.config import config
4+
from gs.backend.config.cors_config import CORSConfig
5+
from gs.backend.config.logger_config import LoggerConfig
6+
7+
8+
def test_logger_config_default():
9+
cfg = LoggerConfig()
10+
assert cfg.excluded_endpoints == []
11+
12+
13+
def test_cors_config_default():
14+
cfg = CORSConfig()
15+
16+
assert cfg.allow_origins == ["http://localhost:5173"]
17+
assert cfg.allow_credentials == True
18+
assert cfg.allow_methods == ["*"]
19+
assert cfg.allow_headers == ["*"]
20+
21+
22+
def test_backend_configuration_from_env(monkeypatch):
23+
monkeypatch.setenv("LOGGER_EXCLUDED_ENDPOINTS", '["/test"]')
24+
monkeypatch.setenv("CORS_ALLOW_ORIGINS", '["http://test.com"]')
25+
26+
importlib.reload(config)
27+
cfg = config.backend_config
28+
assert "/test" in cfg.logger_config.excluded_endpoints
29+
assert "http://test.com" in cfg.cors_config.allow_origins

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ hypothesis==6.131.30
1515
psycopg2-binary==2.9.10
1616
python-dotenv==1.1.0
1717
tqdm==4.67
18+
pydantic-settings==2.4.0
1819

1920
# Typed packages
2021
types-requests==2.31.0

0 commit comments

Comments
 (0)