Add endpoint to return NAME environment variable

This commit is contained in:
Automated Action 2025-06-05 12:07:08 +00:00
parent 5b6b608b80
commit 8cc28b369f
3 changed files with 25 additions and 3 deletions

View File

@ -27,7 +27,14 @@ This is a REST API built with FastAPI and SQLite. It provides endpoints for mana
pip install -r requirements.txt pip install -r requirements.txt
``` ```
3. Create and migrate database: 3. Set up environment variables (optional):
```bash
# Create a .env file or set these in your environment
NAME=YourNameValue
```
4. Create and migrate database:
```bash ```bash
# The storage directory and database will be created automatically when running the app # The storage directory and database will be created automatically when running the app
@ -53,6 +60,7 @@ Once the application is running, you can access:
- `GET /`: API information - `GET /`: API information
- `GET /health`: Health check endpoint - `GET /health`: Health check endpoint
- `GET /env/name`: Returns the value of the NAME environment variable
- `GET /api/v1/items`: List all items - `GET /api/v1/items`: List all items
- `POST /api/v1/items`: Create a new item - `POST /api/v1/items`: Create a new item
- `GET /api/v1/items/{id}`: Get an item by ID - `GET /api/v1/items/{id}`: Get an item by ID

View File

@ -1,5 +1,5 @@
from pathlib import Path from pathlib import Path
from typing import List from typing import List, Optional
from pydantic import validator from pydantic import validator
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
@ -9,6 +9,9 @@ class Settings(BaseSettings):
API_V1_STR: str = "/api/v1" API_V1_STR: str = "/api/v1"
PROJECT_NAME: str = "Generic REST API" PROJECT_NAME: str = "Generic REST API"
# Environment variables
NAME: Optional[str] = None
# CORS Configuration # CORS Configuration
BACKEND_CORS_ORIGINS: List[str] = ["*"] BACKEND_CORS_ORIGINS: List[str] = ["*"]

13
main.py
View File

@ -26,7 +26,12 @@ app.include_router(api_router, prefix=settings.API_V1_STR)
@app.get("/") @app.get("/")
async def root(): async def root():
"""Root endpoint with API information.""" """Root endpoint with API information."""
return {"name": settings.PROJECT_NAME, "docs": "/docs", "health_check": "/health"} return {
"name": settings.PROJECT_NAME,
"docs": "/docs",
"health_check": "/health",
"env_name": "/env/name",
}
@app.get("/health", status_code=200) @app.get("/health", status_code=200)
@ -35,6 +40,12 @@ async def health_check():
return {"status": "healthy"} return {"status": "healthy"}
@app.get("/env/name")
async def get_name_env():
"""Return the value of the NAME environment variable."""
return {"name": settings.NAME}
def custom_openapi(): def custom_openapi():
if app.openapi_schema: if app.openapi_schema:
return app.openapi_schema return app.openapi_schema