Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Azure deployment example #680

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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