aa
This commit is contained in:
160
src/app/main.py
160
src/app/main.py
@@ -1,153 +1,79 @@
|
||||
# ==========================
|
||||
# IMPORTS DES DÉPENDANCES
|
||||
# ==========================
|
||||
import traceback
|
||||
import os
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI, Request, HTTPException, Response
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.responses import HTMLResponse
|
||||
from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
|
||||
import time
|
||||
import os
|
||||
import logging
|
||||
from typing import Dict
|
||||
import markdown
|
||||
|
||||
# Import du routeur pour /users
|
||||
from routes import router as routes_app
|
||||
|
||||
# ==========================
|
||||
# CONFIGURATION DE L'APPLICATION
|
||||
# ==========================
|
||||
# Configuration du logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Création de l'application FastAPI
|
||||
app = FastAPI(
|
||||
title="DevOps Windows API",
|
||||
description="Application de démonstration DevOps sous Windows",
|
||||
version="1.0.0"
|
||||
)
|
||||
app = FastAPI(title="DevOps Windows API", description="Application de démonstration DevOps sous Windows", version="1.0.0")
|
||||
|
||||
# Inclusion du routeur (doit être après la création de 'app')
|
||||
app.include_router(routes_app, prefix="/users", tags=["users"])
|
||||
# 🔹 Utilise un chemin absolu pour le dossier templates
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
templates = Jinja2Templates(directory=str(BASE_DIR / "templates"))
|
||||
|
||||
# ==========================
|
||||
# MÉTRIQUES PROMETHEUS
|
||||
# ==========================
|
||||
REQUEST_COUNT = Counter(
|
||||
'http_requests_total',
|
||||
'Total des requêtes HTTP',
|
||||
['method', 'endpoint', 'status']
|
||||
)
|
||||
app.include_router(routes_app, prefix="/users", tags=["users"])
|
||||
|
||||
REQUEST_LATENCY = Histogram(
|
||||
'http_request_duration_seconds',
|
||||
'Temps de réponse HTTP',
|
||||
['method', 'endpoint']
|
||||
)
|
||||
REQUEST_COUNT = Counter('http_requests_total', 'Total des requêtes HTTP', ['method', 'endpoint', 'status'])
|
||||
REQUEST_LATENCY = Histogram('http_request_duration_seconds', 'Temps de réponse HTTP', ['method', 'endpoint'])
|
||||
|
||||
# ==========================
|
||||
# MIDDLEWARE DE MONITORING
|
||||
# ==========================
|
||||
@app.middleware("http")
|
||||
async def monitor_requests(request: Request, call_next):
|
||||
"""Middleware pour suivre les requêtes et mesurer la latence."""
|
||||
start_time = time.time()
|
||||
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status_code = str(response.status_code)
|
||||
except Exception as e:
|
||||
status_code = "500"
|
||||
response = Response(
|
||||
content=f"Erreur serveur : {str(e)}",
|
||||
status_code=500
|
||||
)
|
||||
|
||||
response = Response(content=f"Erreur serveur : {str(e)}", status_code=500)
|
||||
process_time = time.time() - start_time
|
||||
|
||||
# Enregistrement des métriques
|
||||
REQUEST_COUNT.labels(
|
||||
method=request.method,
|
||||
endpoint=request.url.path,
|
||||
status=status_code
|
||||
).inc()
|
||||
|
||||
REQUEST_LATENCY.labels(
|
||||
method=request.method,
|
||||
endpoint=request.url.path
|
||||
).observe(process_time)
|
||||
|
||||
# Ajout du temps de traitement dans les headers
|
||||
REQUEST_COUNT.labels(method=request.method, endpoint=request.url.path, status=status_code).inc()
|
||||
REQUEST_LATENCY.labels(method=request.method, endpoint=request.url.path).observe(process_time)
|
||||
response.headers["X-Process-Time"] = f"{process_time:.3f}s"
|
||||
|
||||
return response
|
||||
|
||||
# ==========================
|
||||
# ENDPOINTS PRINCIPAUX
|
||||
# ==========================
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def home(request: Request):
|
||||
md_file_path = "/app/TpDevOpsProject.md"
|
||||
try:
|
||||
with open(md_file_path, "r", encoding="utf-8") as f:
|
||||
md_content = f.read()
|
||||
html_content = markdown.markdown(md_content)
|
||||
except Exception as e:
|
||||
html_content = f"<h3>Erreur de lecture du fichier :</h3><pre>{traceback.format_exc()}</pre>"
|
||||
|
||||
@app.get("/")
|
||||
async def home():
|
||||
"""Endpoint racine - Statut de l'application."""
|
||||
return {
|
||||
"message": "🚀 DevOps Stack Windows - Fonctionnel !",
|
||||
"environment": os.getenv("ENV", "development"),
|
||||
"status": "running",
|
||||
"hostname": os.getenv("HOSTNAME", "windows-devops"),
|
||||
"version": "1.0.0"
|
||||
}
|
||||
header_banner = "🚀 <strong>$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ DevOps Stack Windows - Fonctionnel ! $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$</strong>"
|
||||
|
||||
try:
|
||||
return templates.TemplateResponse("index.html", {
|
||||
"request": request,
|
||||
"header": header_banner,
|
||||
"content": html_content
|
||||
})
|
||||
except Exception as e:
|
||||
return HTMLResponse(f"<h3>Erreur de template :</h3><pre>{traceback.format_exc()}</pre>", status_code=500)
|
||||
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
"""Health check pour Kubernetes."""
|
||||
return {
|
||||
"status": "healthy",
|
||||
"timestamp": time.time(),
|
||||
"service": "devops-app"
|
||||
}
|
||||
async def health(): return {"status": "healthy", "timestamp": time.time(), "service": "devops-app"}
|
||||
|
||||
@app.get("/metrics")
|
||||
async def metrics():
|
||||
"""Endpoint pour Prometheus."""
|
||||
try:
|
||||
data = generate_latest()
|
||||
return Response(
|
||||
content=data,
|
||||
media_type=CONTENT_TYPE_LATEST,
|
||||
headers={"Cache-Control": "no-cache"}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Erreur lors de la génération des métriques : {e}")
|
||||
raise HTTPException(status_code=500, detail="Échec de la génération des métriques")
|
||||
try: return Response(content=generate_latest(), media_type=CONTENT_TYPE_LATEST)
|
||||
except Exception as e: raise HTTPException(status_code=500, detail="Échec des métriques")
|
||||
|
||||
@app.get("/info")
|
||||
async def info():
|
||||
"""Informations système."""
|
||||
return {
|
||||
"python_version": "3.11",
|
||||
"platform": "windows",
|
||||
"service": "FastAPI DevOps",
|
||||
"features": ["docker", "kubernetes", "monitoring", "ci-cd"]
|
||||
}
|
||||
async def info(): return {
|
||||
"python_version": "3.11", "platform": "windows", "service": "FastAPI DevOps",
|
||||
"features": ["docker", "kubernetes", "monitoring", "ci-cd"]
|
||||
}
|
||||
|
||||
@app.get("/env")
|
||||
async def show_env():
|
||||
"""Affiche les variables d'environnement (sécurisées)."""
|
||||
safe_env = {
|
||||
"ENV": os.getenv("ENV", "not-set"),
|
||||
"HOSTNAME": os.getenv("HOSTNAME", "not-set"),
|
||||
"PYTHON_VERSION": os.getenv("PYTHON_VERSION", "not-set")
|
||||
}
|
||||
return safe_env
|
||||
|
||||
# ==========================
|
||||
# LANCEMENT EN DÉVELOPPEMENT
|
||||
# ==========================
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
logger.info("Démarrage du serveur FastAPI...")
|
||||
uvicorn.run(
|
||||
app,
|
||||
host="0.0.0.0",
|
||||
port=8000,
|
||||
log_level="info",
|
||||
reload=True # Auto-reload en dev
|
||||
)
|
||||
async def show_env(): return {"ENV": os.getenv("ENV", "not-set"), "HOSTNAME": os.getenv("HOSTNAME", "not-set")}
|
||||
Reference in New Issue
Block a user