Skip to content

Commit 39c89fa

Browse files
committed
feat: Added health check endpoint
1 parent 1672754 commit 39c89fa

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

app/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
class Settings(BaseSettings):
7+
DOCS_URL: str | None = None
78
GATEWAY_ALLOWED_ORIGINS: str = ""
89

910

app/main.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from contextlib import asynccontextmanager
22

3-
from fastapi import FastAPI, Request
3+
from fastapi import FastAPI, Request, status
44
from httpx import AsyncClient
55
from starlette.middleware.cors import CORSMiddleware
6+
from starlette.responses import JSONResponse
67

78
from app.config import settings
89
from app.middleware import log_request_middleware
9-
from app.utils import build_merged_openapi, proxy_request
10+
from app.utils import build_merged_openapi, proxy_request, json_encoder
11+
from app.limiter import limiter
1012

1113

1214
@asynccontextmanager
@@ -30,8 +32,7 @@ async def lifespan(app: FastAPI):
3032
}
3133
yield
3234

33-
34-
app = FastAPI(title="Secure Chain Gateway", docs_url=None, lifespan=lifespan)
35+
app = FastAPI(title="Secure Chain Gateway", docs_url=settings.DOCS_URL, lifespan=lifespan)
3536
app.middleware("http")(log_request_middleware)
3637
app.add_middleware(
3738
CORSMiddleware,
@@ -42,13 +43,34 @@ async def lifespan(app: FastAPI):
4243
)
4344

4445

46+
@app.get(
47+
"/health",
48+
summary="Health Check",
49+
description="Check the status of the API.",
50+
response_description="API status.",
51+
tags=["Secure Chain Gateway Health"]
52+
)
53+
@limiter.limit("25/minute")
54+
async def health_check(request: Request):
55+
return JSONResponse(
56+
status_code=status.HTTP_200_OK, content=json_encoder(
57+
{
58+
"code": "healthy",
59+
}
60+
)
61+
)
62+
63+
4564
@app.api_route("/auth/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
65+
@limiter.limit("25/minute")
4666
async def proxy_auth(path: str, request: Request):
4767
url = f"http://securechain-auth:8000/{path}"
4868
return await proxy_request(url, request)
4969

5070

5171
@app.api_route("/depex/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
72+
@limiter.limit("25/minute")
5273
async def proxy_depex(path: str, request: Request):
5374
url = f"http://securechain-depex:8000/{path}"
5475
return await proxy_request(url, request)
76+

app/utils/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from app.utils.openapi_merge import build_merged_openapi, proxy_request
1+
from .openapi_merge import build_merged_openapi, proxy_request
2+
from .json_encoder import json_encoder
23

3-
__all__ = ["build_merged_openapi", "proxy_request"]
4+
__all__ = ["build_merged_openapi", "proxy_request", "json_encoder"]

app/utils/json_encoder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from datetime import datetime
2+
from json import JSONEncoder, loads
3+
from typing import Any
4+
5+
6+
class JSONencoder(JSONEncoder):
7+
def default(self, o: Any) -> Any:
8+
if isinstance(o, datetime):
9+
return str(o)
10+
return JSONEncoder.default(self, o)
11+
12+
13+
def json_encoder(raw_response: dict[str, Any]) -> Any:
14+
return loads(JSONencoder().encode(raw_response))

0 commit comments

Comments
 (0)