-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi_example.py
More file actions
executable file
·73 lines (63 loc) · 1.99 KB
/
fastapi_example.py
File metadata and controls
executable file
·73 lines (63 loc) · 1.99 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
#!/usr/bin/env python3
"""
FastAPI integration example for allgreen health checks.
Install dependencies:
pip install allgreen[fastapi]
Run:
python examples/fastapi_example.py
# Visit http://localhost:8000/healthcheck
# Or try http://localhost:8000/docs for OpenAPI docs
"""
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from allgreen.integrations.fastapi_integration import (
create_router,
healthcheck_endpoint,
)
# Create FastAPI app
app = FastAPI(
title="FastAPI + Allgreen Example",
description="Example showing allgreen health checks with FastAPI",
version="1.0.0"
)
# Basic route
@app.get("/", response_class=HTMLResponse)
async def index():
return '''
<h1>FastAPI + Allgreen Example</h1>
<p><a href="/healthcheck">View Health Checks</a></p>
<p><a href="/healthcheck.json">JSON API</a></p>
<p><a href="/docs">OpenAPI Docs</a></p>
'''
# Method 1: Mount the health check router
health_router = create_router(
app_name="FastAPI Example App",
config_path="examples/allgreen_config.py",
environment="development"
)
app.include_router(health_router)
# Method 2: Individual endpoint (alternative approach)
@app.get("/health")
async def health_check(request: Request):
"""Alternative health check endpoint using direct function call."""
return await healthcheck_endpoint(
request,
app_name="FastAPI Direct Endpoint",
config_path="examples/allgreen_config.py",
environment="development"
)
if __name__ == '__main__':
print("🚀 FastAPI + Allgreen Example")
print("📋 Health checks: http://localhost:8000/healthcheck")
print("🔧 JSON API: http://localhost:8000/healthcheck.json")
print("📚 OpenAPI docs: http://localhost:8000/docs")
print("💡 Using config: examples/allgreen_config.py")
print()
# Run with uvicorn
uvicorn.run(
"fastapi_example:app",
host="0.0.0.0",
port=8000,
reload=True
)