Skip to content

Commit

Permalink
Update FastAPI deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mel Cadano committed Aug 1, 2024
1 parent 691e017 commit 06653a9
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 35 deletions.
8 changes: 8 additions & 0 deletions .funcignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.git*
.vscode
__azurite_db*__.json
__blobstorage__
__queuestorage__
local.settings.json
test
venv
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ Thumbs.db
*.avi
*.flv
*.mov
*.wmv
*.wmv
.python_packages
__pycache__
venv

VS Code
.vscode
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Serverless FastAPI on Azure
# Serverless FastAPI on Azure

Little project to tests Serverless FastAPI deployment on Azure.
Little project to tests Serverless FastAPI deployment on Azure.

## Local run
## Setup the project

```bash
# Clone project
Expand All @@ -17,8 +17,20 @@ $ virtualenv venv

# Install dependencies
$ pip install -r requirements.txt
$ pip install -r requirements-dev.txt
```

# Fire up local app server
## Running locally using Uvicorn

```bash
$ uvicorn app.main:app --reload --log-level debug
```
```

## Running locally using Azure Functions Core Tools

```bash
# Install tools via npm
$ npm install -g azure-functions-core-tools@3

# Start local server
$ func start
```
24 changes: 24 additions & 0 deletions app/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"scriptFile": "main.py",
"bindings": [
{
"authLevel": "admin",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post",
"put",
"patch",
"delete"
],
"route": "{*route}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
34 changes: 8 additions & 26 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
import azure.functions as func
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

from app.routers import users

class Item(BaseModel):
id: int
name: str
app = FastAPI()
app.include_router(users.router, prefix='/users', tags=['Users'])


@app.get("/")
def root():
return {"message": "API health at 100%."}


@app.get("/items")
def get_items():
return {"message": f"{len([])} Items retrieved."}


@app.get("/items/{itemId}")
def get_item(item_id: int):
return {"message": f"[id={item_id}] Item retrieved."}


@app.post("/items")
def add_item(item: Item):
return {"message": f"[id={item.id}] New item '{item.name}' added."}
async def health():
return {"message": "The API is 100% ready."}


@app.delete("/items/{itemId}")
def delete_item(item_id: int):
return {"message": f"[id={item_id}] Item deleted."}
async def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
return await func.AsgiMiddleware(app).handle_async(req, context)
Empty file added app/routers/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions app/routers/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import logging

from fastapi import APIRouter

router = APIRouter()


@router.get("/{user_id}")
async def get_user(user_id: int):
logging.info("Retrieving user \'%s\'...", user_id)
return {
"user_id": user_id,
"username": 'vic',
"firstname": 'Victor',
"lastname": 'Magtanggol',
}


@router.put("/{user_id}")
async def update_user(user_id: int):
logging.info("Updating user \'%s\'...", user_id)
return {
"user_id": user_id,
"username": 'vic',
"firstname": 'Victor',
"lastname": 'Magtanggol',
}


@router.delete("/{user_id}")
async def delete_user(user_id: int):
logging.info("Deleting user \'%s\'...", user_id)
return {"message": "OK"}
20 changes: 20 additions & 0 deletions host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
},
"extensions": {
"http": {
"routePrefix": ""
}
}
}
8 changes: 8 additions & 0 deletions local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
}
}
1 change: 0 additions & 1 deletion requirements-dev.txt

This file was deleted.

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
azure-functions
fastapi==0.111.0
fastapi==0.111.0
uvicorn==0.30.1
Empty file added tests/__init__.py
Empty file.

0 comments on commit 06653a9

Please sign in to comment.