From 49b05b1af13d0a6696e55d279d27f5791e4bc783 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Tue, 27 May 2025 22:16:57 +0000 Subject: [PATCH] Implement environment variable service with FastAPI --- README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++++-- main.py | 58 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 5 ++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 main.py create mode 100644 requirements.txt diff --git a/README.md b/README.md index e8acfba..3c15d4e 100644 --- a/README.md +++ b/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 +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 \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..966bd49 --- /dev/null +++ b/main.py @@ -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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b0f48e4 --- /dev/null +++ b/requirements.txt @@ -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 \ No newline at end of file