Skip to content

Commit

Permalink
health route and fix prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed May 31, 2024
1 parent 3437b25 commit e8f24ae
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ __azurite_db*__.json
# Serverless directories
.serverless
node_modules
aws_lambda_fastapi_venv
.aws_lambda_fastapi_venv
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pip3 install -r requirements.txt

# run project with uvicorn
uvicorn "fastapi_project.main:app" --reload --port=8000

# or, run bash with shell
./run_fastapi_project.sh
```

# deploy on production
Expand All @@ -33,11 +36,12 @@ uvicorn "fastapi_project.main:app" --reload --port=8000
set secrets `SERVERLESS_ACCESS_KEY` , `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`

## serverless
* set your data

* set your own data

```bash
# serverless.yml
org: code4mk
org: ****
app: demo-app-api-fastapi
service: demo-app-api-fastapi
```
Expand All @@ -49,4 +53,9 @@ service: demo-app-api-fastapi
```


# project route

```bash
http://localhost:8000/api/v1/users
http://localhost:8000/health
```
11 changes: 11 additions & 0 deletions fastapi_project/api/health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from fastapi import APIRouter, status
from fastapi.responses import JSONResponse

# Create a api router
router = APIRouter()

# Health check route
@router.get("/")
async def health_check():
data = {"status": "ok"}
return JSONResponse(content=data, status_code=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion fastapi_project/api/root_index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, status, Request
from fastapi.responses import JSONResponse

# Create a FastAPI app
# Create a api router
router = APIRouter()

# root index
Expand Down
9 changes: 5 additions & 4 deletions fastapi_project/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fastapi import FastAPI
from dotenv import load_dotenv
from fastapi.middleware.cors import CORSMiddleware
from fastapi_project.api import root_index
from fastapi_project.api import health, root_index

# Load .env file
load_dotenv()
Expand All @@ -14,15 +14,16 @@
def create_application():
application = FastAPI()

# Include the root index router
# Include the root index and health router
application.include_router(root_index.router)

application.include_router(health.router, prefix="/health")


if load_sql_project == True:
print("SQL_PROJECT is enabled")
# Include additional routers if LOAD_SQL_PROJECT is enabled
from fastapi_project.api.v1 import user
application.include_router(user.router)
application.include_router(user.router, prefix="/api/v1")

# Add CORS middleware
# In production, replace the "*" with the actual frontend URL
Expand Down

0 comments on commit e8f24ae

Please sign in to comment.