Skip to content

Commit

Permalink
use config.settings attributes instead of os.getenv
Browse files Browse the repository at this point in the history
APISettings() will load all values from .env file as
APISettings's attributes. These can be imported in all
other modules, instead of using load_dotenv in every module.
ref: https://fastapi.tiangolo.com/advanced/settings/#reading-a-env-file

This commit also fixes the issue of missing value of PADDLES_URL
in services/helpers.py

Signed-off-by: Vallari Agrawal <val.agl002@gmail.com>
  • Loading branch information
VallariAg committed Nov 8, 2023
1 parent f92d3f3 commit 82946dc
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A REST API to execute [teuthology commands](https://docs.ceph.com/projects/teuth
teuthology_api:
build:
context: ../../../teuthology-api
env_file: ../../../teuthology-api/.env
ports:
- 8082:8080
environment:
Expand Down
3 changes: 2 additions & 1 deletion gh-actions/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ if [ ! -d "$folder" ] ; then
echo " teuthology_api:
build:
context: ../../../../
env_file: ../../../../.env
ports:
- 8082:8080
environment:
environment:
TEUTHOLOGY_API_SERVER_HOST: 0.0.0.0
TEUTHOLOGY_API_SERVER_PORT: 8080
depends_on:
Expand Down
12 changes: 12 additions & 0 deletions src/teuthology_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ class APISettings(BaseSettings):
Class for API settings.
"""

deployment: str = ""
pulpito_url: str = ""
paddles_url: str = ""

gh_client_id: str = ""
gh_client_secret: str = ""
gh_token_url: str = ""
gh_authorization_base_url: str = ""
gh_fetch_membership_url: str = ""

session_secret_key: str = ""

model_config = SettingsConfigDict(
env_file=".env", env_file_encoding="utf-8", extra="ignore"
)
Expand Down
11 changes: 5 additions & 6 deletions src/teuthology_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
from dotenv import load_dotenv

from teuthology_api.config import settings
from teuthology_api.routes import suite, kill, login, logout

load_dotenv()

DEPLOYMENT = os.getenv("DEPLOYMENT")
SESSION_SECRET_KEY = os.getenv("SESSION_SECRET_KEY")
PULPITO_URL = os.getenv("PULPITO_URL")
PADDLES_URL = os.getenv("PADDLES_URL")
DEPLOYMENT = settings.deployment
SESSION_SECRET_KEY = settings.session_secret_key
PULPITO_URL = settings.pulpito_url
PADDLES_URL = settings.paddles_url

log = logging.getLogger(__name__)
app = FastAPI()
Expand Down
15 changes: 7 additions & 8 deletions src/teuthology_api/routes/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import os
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import RedirectResponse
from dotenv import load_dotenv
import httpx
from teuthology_api.config import settings

load_dotenv()

GH_CLIENT_ID = os.getenv("GH_CLIENT_ID")
GH_CLIENT_SECRET = os.getenv("GH_CLIENT_SECRET")
GH_AUTHORIZATION_BASE_URL = os.getenv("GH_AUTHORIZATION_BASE_URL")
GH_TOKEN_URL = os.getenv("GH_TOKEN_URL")
GH_FETCH_MEMBERSHIP_URL = os.getenv("GH_FETCH_MEMBERSHIP_URL")
PULPITO_URL = os.getenv("PULPITO_URL")
GH_CLIENT_ID = settings.gh_client_id
GH_CLIENT_SECRET = settings.gh_client_secret
GH_AUTHORIZATION_BASE_URL = settings.gh_authorization_base_url
GH_TOKEN_URL = settings.gh_token_url
GH_FETCH_MEMBERSHIP_URL = settings.gh_fetch_membership_url
PULPITO_URL = settings.pulpito_url

log = logging.getLogger(__name__)
router = APIRouter(
Expand Down
5 changes: 3 additions & 2 deletions src/teuthology_api/routes/logout.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging, os
import logging
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import RedirectResponse
from teuthology_api.config import settings

PULPITO_URL = os.getenv("PULPITO_URL")
PULPITO_URL = settings.pulpito_url
log = logging.getLogger(__name__)

router = APIRouter(
Expand Down
2 changes: 1 addition & 1 deletion src/teuthology_api/services/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests # Note: import requests after teuthology
from requests.exceptions import HTTPError

PADDLES_URL = os.getenv("PADDLES_URL")
PADDLES_URL = settings.paddles_url

log = logging.getLogger(__name__)

Expand Down

0 comments on commit 82946dc

Please sign in to comment.