Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (96 loc) · 3.16 KB
/
Copy pathapp.py
File metadata and controls
110 lines (96 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import os
import time
import sentry_sdk
from pathlib import Path
from fastapi import FastAPI
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from starlette_exporter import PrometheusMiddleware, handle_metrics
from middleware import (
AuthenticationMiddleware,
ErrorHandlerMiddleware,
LoggingMiddleware,
RateLimitMiddleware,
)
from db.redis_config import redis_client
from routes.auth import router as auth_router
from routes.citizen import router as citizen_router
from routes.vendor import router as vendor_router
from routes.government import router as government_router
from routes.chat import router as chat_router
# Initialize Sentry (Used in prod environment)
# sentry_sdk.init(
# dsn=os.environ.get("SENTRY_DSN"),
# environment=os.environ.get("SENTRY_ENV"),
# integrations=[
# FastApiIntegration(),
# RedisIntegration(),
# ],
# )
# Initialize FastAPI app
app = FastAPI(
title="Payzee API",
description="A digital payment system",
version="1.0.0",
)
# Setup middleware (* for dev environment)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True,
)
app.add_middleware(AuthenticationMiddleware)
app.add_middleware(ErrorHandlerMiddleware)
app.add_middleware(LoggingMiddleware)
app.add_middleware(RateLimitMiddleware)
# Add Prometheus middleware for metrics (Used in prod environment)
app.add_middleware(
PrometheusMiddleware,
app_name="payzee",
group_paths=True,
filter_unhandled_paths=False,
prefix="payzee",
)
app.add_route("/metrics", handle_metrics)
# Include routers
app.include_router(auth_router, prefix="/api/v1/auth", tags=["authentication"])
app.include_router(citizen_router, prefix="/api/v1/citizens", tags=["citizen"])
app.include_router(vendor_router, prefix="/api/v1/vendors", tags=["vendor"])
app.include_router(government_router, prefix="/api/v1/governments", tags=["government"])
app.include_router(chat_router, prefix="/api/v1/chat", tags=["chatbot"])
@app.get("/", response_class=HTMLResponse)
def root() -> HTMLResponse:
html_file = Path("templates/index.html").read_text(encoding="utf-8")
return html_file
@app.get("/health")
def health_check() -> JSONResponse:
api_start_time = time.time()
api_status = {
"name": "FastAPI",
"title": "API Check",
"code": 200,
"message": "All OK",
"latency": 0,
}
api_latency = time.time() - api_start_time
api_status["latency"] = round(api_latency, 4)
db_start_time = time.time()
db_status = {
"name": "Redis Database",
"title": "Database Check",
"code": 200,
"message": "All OK",
"latency": 0,
}
try:
redis_client.ping()
except Exception as e:
db_status["code"] = 503
db_status["message"] = str(e)
db_latency = time.time() - db_start_time
db_status["latency"] = round(db_latency, 4)
return JSONResponse(content={"health": [api_status, db_status]})