2025-05-27 22:16:57 +00:00

59 lines
1.5 KiB
Python

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)