Add endpoint to return NAME environment variable
This commit is contained in:
parent
5b6b608b80
commit
8cc28b369f
10
README.md
10
README.md
@ -27,7 +27,14 @@ This is a REST API built with FastAPI and SQLite. It provides endpoints for mana
|
||||
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
|
||||
# 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 /health`: Health check endpoint
|
||||
- `GET /env/name`: Returns the value of the NAME environment variable
|
||||
- `GET /api/v1/items`: List all items
|
||||
- `POST /api/v1/items`: Create a new item
|
||||
- `GET /api/v1/items/{id}`: Get an item by ID
|
||||
|
@ -1,5 +1,5 @@
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import validator
|
||||
from pydantic_settings import BaseSettings
|
||||
@ -9,6 +9,9 @@ class Settings(BaseSettings):
|
||||
API_V1_STR: str = "/api/v1"
|
||||
PROJECT_NAME: str = "Generic REST API"
|
||||
|
||||
# Environment variables
|
||||
NAME: Optional[str] = None
|
||||
|
||||
# CORS Configuration
|
||||
BACKEND_CORS_ORIGINS: List[str] = ["*"]
|
||||
|
||||
|
13
main.py
13
main.py
@ -26,7 +26,12 @@ app.include_router(api_router, prefix=settings.API_V1_STR)
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""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)
|
||||
@ -35,6 +40,12 @@ async def health_check():
|
||||
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():
|
||||
if app.openapi_schema:
|
||||
return app.openapi_schema
|
||||
|
Loading…
x
Reference in New Issue
Block a user