-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
105 lines (81 loc) · 2.67 KB
/
Copy pathroutes.py
File metadata and controls
105 lines (81 loc) · 2.67 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
from fastapi import APIRouter, FastAPI, HTTPException, Request
from pydantic import BaseModel
from starlette.responses import HTMLResponse, JSONResponse
from sample import __version__
from sample.utils.constants import API_PREFIX, GREETING
from sample.utils.helper import normalize_name
app = FastAPI(
title="Sample API",
version=__version__,
)
api_router = APIRouter()
greet_router = APIRouter(prefix=API_PREFIX, tags=["V1"])
class GreetRequest(BaseModel):
name: str
class GreetResponse(BaseModel):
message: str
@api_router.get("/health")
def health_check():
return {"status": "ok"}
@greet_router.post("/greet", response_model=GreetResponse)
def greet_user(payload: GreetRequest):
clean_name = normalize_name(payload.name)
if not clean_name:
raise HTTPException(status_code=400, detail="Invalid name provided")
return {"message": f"{GREETING}, {clean_name} 👋"}
@api_router.get("/version", tags=["Version"])
def version():
return {"version": api_router.version}
@api_router.get("/", response_class=HTMLResponse, tags=["Home"])
async def read_root(request: Request):
return """
<html>
<head>
<title>🎉 Sample Api 🎉</title>
<style>
body {
background: linear-gradient(to right, #268387, #e3898a);
text-align: center;
padding-top: 10%;
color: #fff;
}
h1 {
font-size: 3em;
margin-bottom: 0.2em;
}
p {
font-size: 1.2em;
}
a {
color: #fff;
background: #4CAF50;
padding: 10px 20px;
text-decoration: none;
border-radius: 10px;
font-weight: bold;
transition: background 0.3s ease;
}
a:hover {
background: #45a049;
}
</style>
</head>
<body>
<h1>🎈 This is sample API Tool 🎈</h1>
<p>Explore the <a href="/docs">API Documentation</a></p>
</body>
</html>
"""
@api_router.get("/help", tags=["Help"])
def get_help():
return JSONResponse(
status_code=200,
content={
"message": "Welcome to the Sample BoilerPlate API! Visit /docs for API documentation."
},
)
app.include_router(api_router)
app.include_router(greet_router)
def start(port: int = 5000):
import uvicorn
uvicorn.run("sample.api.routes:app", host="127.0.0.1", port=port, reload=True)