Skip to content

Commit f2d0cc6

Browse files
committed
health route and prefix add
1 parent f25f3f1 commit f2d0cc6

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ pip3 install -r requirements.txt
2222

2323
# run project with uvicorn
2424
uvicorn "fastapi_project.main:app" --reload --port=8000
25+
26+
# or, run bash with shell
27+
./run_fastapi_project.sh
2528
```
2629

2730
# deploy on production
@@ -35,4 +38,11 @@ uvicorn "fastapi_project.main:app" --reload --port=8000
3538

3639
set the environment variable
3740
* `DATABASE_URL`
38-
* `LOAD_SQL_PROJECT` value will be `yes`
41+
* `LOAD_SQL_PROJECT` value will be `yes`
42+
43+
## Project route
44+
45+
```bash
46+
http://localhost:8000/api/v1/users
47+
http://localhost:8000/health
48+
```

fastapi_project/api/health.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from fastapi import APIRouter, status
2+
from fastapi.responses import JSONResponse
3+
4+
# Create a api router
5+
router = APIRouter()
6+
7+
# Health check route
8+
@router.get("/")
9+
async def health_check():
10+
data = {"status": "ok"}
11+
return JSONResponse(content=data, status_code=status.HTTP_200_OK)

fastapi_project/main.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from fastapi import FastAPI
33
from dotenv import load_dotenv
44
from fastapi.middleware.cors import CORSMiddleware
5-
from fastapi_project.api import root_index
5+
from fastapi_project.api import health, root_index
66

77
# Load .env file
88
load_dotenv()
@@ -14,15 +14,16 @@
1414
def create_application():
1515
application = FastAPI()
1616

17-
# Include the root index router
17+
# Include the root index and health router
1818
application.include_router(root_index.router)
19+
application.include_router(health.router, prefix="/health")
1920

2021

2122
if load_sql_project == True:
2223
print("SQL_PROJECT is enabled")
2324
# Include additional routers if LOAD_SQL_PROJECT is enabled
2425
from fastapi_project.api.v1 import user
25-
application.include_router(user.router)
26+
application.include_router(user.router, prefix="/api/v1")
2627

2728
# Add CORS middleware
2829
# In production, replace the "*" with the actual frontend URL
File renamed without changes.

0 commit comments

Comments
 (0)