Skip to content

Commit

Permalink
Update Azure deployment example (#680)
Browse files Browse the repository at this point in the history
* update imports per #537; reenable middleware

* updated docs

* updated extensionBundle

* lint

---------

Co-authored-by: vincentsarago <[email protected]>
  • Loading branch information
gassc and vincentsarago authored Mar 6, 2024
1 parent 6b43ea3 commit 49d5f8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
14 changes: 6 additions & 8 deletions deployment/azure/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
### Function

TiTiler is built on top of [FastAPI](https://github.com/tiangolo/fastapi), a modern, fast, Python web framework for building APIs. As for AWS Lambda we can make our FastAPI application work on Azure Function by wrapping it within the [Azure Function Python worker](https://github.com/Azure/azure-functions-python-worker).
TiTiler is built on top of [FastAPI](https://github.com/tiangolo/fastapi), a modern, fast, Python web framework for building APIs. We can make our FastAPI application work as an Azure Function by wrapping it within the [Azure Function Python worker](https://github.com/Azure/azure-functions-python-worker).

If you are not familiar with **Azure functions** we recommend checking https://docs.microsoft.com/en-us/azure/azure-functions/ first.

Minimal TiTiler Azure function code:
```python
import azure.functions as func
from titiler.application.routers import cog, mosaic, stac, tms
from titiler.application.main import cog, mosaic, stac, tms
from fastapi import FastAPI


Expand All @@ -20,14 +20,12 @@ app.include_router(mosaic.router, prefix="/mosaicjson", tags=["MosaicJSON"])
app.include_router(tms.router, tags=["TileMatrixSets"])


def main(
async def main(
req: func.HttpRequest, context: func.Context,
) -> func.HttpResponse:
return func.AsgiMiddleware(app).handle(req, context)
return await func.AsgiMiddleware(app).handle_async(req, context)
```

Note: there is a `bug` in `azure.functions.AsgiMiddleware` which prevent using `starlette.BaseHTTPMiddleware` middlewares (see: https://github.com/Azure/azure-functions-python-worker/issues/903).

#### Requirements
- Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
- Azure Function Tool: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local
Expand All @@ -42,8 +40,8 @@ $ cd titiler/deployment/azure

$ az login
$ az group create --name AzureFunctionsTiTiler-rg --location eastus
$ az storage account create --name TiTilerStorage --sku Standard_LRS
$ az functionapp create --consumption-plan-location eastus --runtime python --runtime-version 3.8 --functions-version 3 --name titiler --os-type linux
$ az storage account create --name titilerstorage --sku Standard_LRS -g AzureFunctionsTiTiler-rg
$ az functionapp create --consumption-plan-location eastus --runtime python --runtime-version 3.8 --functions-version 3 --name titiler --os-type linux -g AzureFunctionsTiTiler-rg -s titilerstorage
$ func azure functionapp publish titiler
```

Expand Down
41 changes: 19 additions & 22 deletions deployment/azure/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
from starlette_cramjam.middleware import CompressionMiddleware

from titiler.application import __version__ as titiler_version
from titiler.application.custom import templates
from titiler.application.routers import cog, mosaic, stac, tms
from titiler.application.main import cog, mosaic, stac, templates, tms
from titiler.application.settings import ApiSettings
from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers

# from titiler.core.middleware import (
# CacheControlMiddleware,
# LoggerMiddleware,
# LowerCaseQueryStringMiddleware,
# TotalTimeMiddleware,
# )
from titiler.core.middleware import (
CacheControlMiddleware,
LoggerMiddleware,
LowerCaseQueryStringMiddleware,
TotalTimeMiddleware,
)
from titiler.mosaic.errors import MOSAIC_STATUS_CODES

api_settings = ApiSettings()
Expand Down Expand Up @@ -68,19 +66,18 @@
},
)

# see https://github.com/encode/starlette/issues/1320
# app.add_middleware(
# CacheControlMiddleware,
# cachecontrol=api_settings.cachecontrol,
# exclude_path={r"/healthz"},
# )
app.add_middleware(
CacheControlMiddleware,
cachecontrol=api_settings.cachecontrol,
exclude_path={r"/healthz"},
)

# if api_settings.debug:
# app.add_middleware(LoggerMiddleware, headers=True, querystrings=True)
# app.add_middleware(TotalTimeMiddleware)
if api_settings.debug:
app.add_middleware(LoggerMiddleware, headers=True, querystrings=True)
app.add_middleware(TotalTimeMiddleware)

# if api_settings.lower_case_query_parameters:
# app.add_middleware(LowerCaseQueryStringMiddleware)
if api_settings.lower_case_query_parameters:
app.add_middleware(LowerCaseQueryStringMiddleware)


@app.get("/healthz", description="Health Check", tags=["Health Check"])
Expand All @@ -99,9 +96,9 @@ def landing(request: Request):
)


def main(
async def main(
req: func.HttpRequest,
context: func.Context,
) -> func.HttpResponse:
"""Run App in AsgiMiddleware."""
return func.AsgiMiddleware(app).handle(req, context)
return await func.AsgiMiddleware(app).handle_async(req, context)
2 changes: 1 addition & 1 deletion deployment/azure/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
"version": "[3.*, 4.0.0)"
},
"extensions": {
"http": {
Expand Down

0 comments on commit 49d5f8e

Please sign in to comment.