Adding base logic for config handling. Adding example config to root

This commit is contained in:
Pascal Scheiben
2025-09-18 09:33:55 +02:00
parent 19e9cd6625
commit 76c5353afa
9 changed files with 57 additions and 8 deletions

View File

@@ -1,2 +1,2 @@
# contains a constant definition
FOO: int = 42
FOO: int = 42

View File

@@ -4,6 +4,7 @@ from .schema import ExampleSchema
router = APIRouter(tags=["example"])
@router.get("/example")
async def example_endpoint() -> ExampleSchema:
return ExampleSchema(example_field="foo", another_field=42)
return ExampleSchema(example_field="foo", another_field=42)

View File

@@ -1,6 +1,7 @@
# contains the schema definitions for the example service
from pydantic import BaseModel
class ExampleSchema(BaseModel):
example_field: str
another_field: int

View File

@@ -1,3 +1,3 @@
# contains the business logic for the example service
async def example_service() -> str:
return "This is an example service"
return "This is an example service"

View File

@@ -1,5 +1,21 @@
def main() -> None:
print("Hello, World!")
from src.service import load_config
from fastapi import FastAPI
import logging
if __name__ == "__main__":
main()
logger = logging.getLogger("uvicorn")
logger.info("Starting application")
config = load_config()
app = FastAPI()
@app.get("/")
async def main():
return {"Hello": "World"}
@app.get("/config")
async def get_config():
"""Endpoint to get the current configuration."""
return config.model_dump()

7
src/schema.py Normal file
View File

@@ -0,0 +1,7 @@
from pydantic import BaseModel
class ConfigSchema(BaseModel):
hostname: str
username: str
password: str

16
src/service.py Normal file
View File

@@ -0,0 +1,16 @@
from dotenv import dotenv_values
import logging
from src.schema import ConfigSchema
logger = logging.getLogger("uvicorn")
def load_config() -> ConfigSchema:
logger.info("Loading config from .env file")
config = dotenv_values(".env")
return ConfigSchema(
hostname=config["CLUSTER1_HOSTNAME"],
username=config["CLUSTER1_USERNAME"],
password=config["CLUSTER1_PASSWORD"],
)