Implement environment variable service with FastAPI
This commit is contained in:
parent
50f81c2fbc
commit
49b05b1af1
66
README.md
66
README.md
@ -1,3 +1,65 @@
|
|||||||
# FastAPI Application
|
# Environment Variable Service
|
||||||
|
|
||||||
This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform.
|
A simple FastAPI application that returns the value of environment variables, specifically designed to return the value of the `NAME` environment variable.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Get the value of the `NAME` environment variable
|
||||||
|
- Get the value of any environment variable by name
|
||||||
|
- Health check endpoint to verify service status
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
1. Clone the repository:
|
||||||
|
```bash
|
||||||
|
git clone <repository-url>
|
||||||
|
cd environmentvariableservice
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Install dependencies:
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
1. Set environment variables (optional):
|
||||||
|
```bash
|
||||||
|
export NAME="Your Name"
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Run the application:
|
||||||
|
```bash
|
||||||
|
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Access the API:
|
||||||
|
- API documentation: http://localhost:8000/docs
|
||||||
|
- Alternative API documentation: http://localhost:8000/redoc
|
||||||
|
- OpenAPI specification: http://localhost:8000/openapi.json
|
||||||
|
|
||||||
|
## API Endpoints
|
||||||
|
|
||||||
|
### Get NAME environment variable
|
||||||
|
```
|
||||||
|
GET /env/name
|
||||||
|
```
|
||||||
|
Returns the value of the `NAME` environment variable.
|
||||||
|
|
||||||
|
### Get any environment variable
|
||||||
|
```
|
||||||
|
GET /env/{variable_name}
|
||||||
|
```
|
||||||
|
Returns the value of the specified environment variable.
|
||||||
|
|
||||||
|
### Health Check
|
||||||
|
```
|
||||||
|
GET /health
|
||||||
|
```
|
||||||
|
Returns the status of the service.
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
- The application uses FastAPI for the REST API
|
||||||
|
- Environment variables are accessed via the Python `os` module
|
||||||
|
- Ruff is used for linting and code formatting
|
58
main.py
Normal file
58
main.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import os
|
||||||
|
from typing import Dict, Optional
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
title="Environment Variable Service",
|
||||||
|
description="A simple service that returns the value of environment variables",
|
||||||
|
version="0.1.0",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class EnvVarResponse(BaseModel):
|
||||||
|
name: str
|
||||||
|
value: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/env/{variable_name}",
|
||||||
|
response_model=EnvVarResponse,
|
||||||
|
tags=["Environment Variables"],
|
||||||
|
)
|
||||||
|
async def get_env_var(variable_name: str) -> EnvVarResponse:
|
||||||
|
"""
|
||||||
|
Get the value of the specified environment variable.
|
||||||
|
|
||||||
|
Returns the value if it exists, or None if the variable is not set.
|
||||||
|
"""
|
||||||
|
value = os.environ.get(variable_name)
|
||||||
|
return EnvVarResponse(name=variable_name, value=value)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/env/name", response_model=EnvVarResponse, tags=["Environment Variables"])
|
||||||
|
async def get_name_var() -> EnvVarResponse:
|
||||||
|
"""
|
||||||
|
Get the value of the NAME environment variable.
|
||||||
|
|
||||||
|
Returns the value if it exists, or None if the variable is not set.
|
||||||
|
"""
|
||||||
|
value = os.environ.get("NAME")
|
||||||
|
return EnvVarResponse(name="NAME", value=value)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/health", response_model=Dict[str, str], tags=["Health"])
|
||||||
|
async def health_check() -> Dict[str, str]:
|
||||||
|
"""
|
||||||
|
Health check endpoint.
|
||||||
|
|
||||||
|
Returns a status of "ok" if the service is running properly.
|
||||||
|
"""
|
||||||
|
return {"status": "ok"}
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
5
requirements.txt
Normal file
5
requirements.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
fastapi>=0.100.0
|
||||||
|
uvicorn>=0.22.0
|
||||||
|
pydantic>=2.0.0
|
||||||
|
python-dotenv>=1.0.0
|
||||||
|
ruff>=0.0.278
|
Loading…
x
Reference in New Issue
Block a user