Add FastAPI service, Dockerfile and compose

This commit is contained in:
rezayaghobi-dev
2026-10-03 19:08:48 +03:30
commit a1973b97ff
10 changed files with 119 additions and 0 deletions

4
webapp/.dockerignore Normal file
View File

@@ -0,0 +1,4 @@
.venv/
__pycache__/
*.pyc
.pytest_cache/

4
webapp/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.venv/
__pycache__/
*.pyc
.pytest_cache/

18
webapp/Dockerfile Normal file
View File

@@ -0,0 +1,18 @@
FROM python:3.12-slim
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app ./app
RUN useradd --system --uid 1001 appuser
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=15s --timeout=3s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

0
webapp/app/__init__.py Normal file
View File

44
webapp/app/main.py Normal file
View File

@@ -0,0 +1,44 @@
import time
from fastapi import FastAPI, HTTPException, Request, Response
from prometheus_client import CONTENT_TYPE_LATEST, Counter, Histogram, generate_latest
app = FastAPI(title="DevOps Test Service")
REQUESTS = Counter(
"http_requests_total", "Total HTTP requests", ["method", "path", "status"]
)
LATENCY = Histogram(
"http_request_duration_seconds", "Request latency in seconds", ["path"]
)
@app.middleware("http")
async def track_metrics(request: Request, call_next):
start = time.perf_counter()
response = await call_next(request)
path = request.url.path
REQUESTS.labels(request.method, path, response.status_code).inc()
LATENCY.labels(path).observe(time.perf_counter() - start)
return response
@app.get("/")
def index():
return {"message": "Hello from the DevOps test service"}
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/error")
def error():
# Used to generate 5xx responses and trigger the high-error-rate alert
raise HTTPException(status_code=500, detail="simulated failure")
@app.get("/metrics")
def metrics():
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)

14
webapp/compose.yml Normal file
View File

@@ -0,0 +1,14 @@
services:
webapp:
build: .
image: webapp:latest
container_name: webapp
restart: unless-stopped
ports:
- "8000:8000"
networks:
- monitoring-net
networks:
monitoring-net:
external: true

2
webapp/pytest.ini Normal file
View File

@@ -0,0 +1,2 @@
[pytest]
pythonpath = .

View File

@@ -0,0 +1,3 @@
-r requirements.txt
pytest
httpx

3
webapp/requirements.txt Normal file
View File

@@ -0,0 +1,3 @@
fastapi
uvicorn[standard]
prometheus-client

27
webapp/tests/test_app.py Normal file
View File

@@ -0,0 +1,27 @@
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_index():
r = client.get("/")
assert r.status_code == 200
def test_health():
r = client.get("/health")
assert r.status_code == 200
assert r.json() == {"status": "ok"}
def test_error_endpoint_returns_500():
assert client.get("/error").status_code == 500
def test_metrics_exposed():
client.get("/health")
r = client.get("/metrics")
assert r.status_code == 200
assert "http_requests_total" in r.text