Skip to content

Commit 49d5f8e

Browse files
gasscvincentsarago
andauthored
Update Azure deployment example (#680)
* update imports per #537; reenable middleware * updated docs * updated extensionBundle * lint --------- Co-authored-by: vincentsarago <[email protected]>
1 parent 6b43ea3 commit 49d5f8e

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

deployment/azure/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
### Function
22

3-
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).
3+
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).
44

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

77
Minimal TiTiler Azure function code:
88
```python
99
import azure.functions as func
10-
from titiler.application.routers import cog, mosaic, stac, tms
10+
from titiler.application.main import cog, mosaic, stac, tms
1111
from fastapi import FastAPI
1212

1313

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

2222

23-
def main(
23+
async def main(
2424
req: func.HttpRequest, context: func.Context,
2525
) -> func.HttpResponse:
26-
return func.AsgiMiddleware(app).handle(req, context)
26+
return await func.AsgiMiddleware(app).handle_async(req, context)
2727
```
2828

29-
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).
30-
3129
#### Requirements
3230
- Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
3331
- Azure Function Tool: https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local
@@ -42,8 +40,8 @@ $ cd titiler/deployment/azure
4240

4341
$ az login
4442
$ az group create --name AzureFunctionsTiTiler-rg --location eastus
45-
$ az storage account create --name TiTilerStorage --sku Standard_LRS
46-
$ az functionapp create --consumption-plan-location eastus --runtime python --runtime-version 3.8 --functions-version 3 --name titiler --os-type linux
43+
$ az storage account create --name titilerstorage --sku Standard_LRS -g AzureFunctionsTiTiler-rg
44+
$ 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
4745
$ func azure functionapp publish titiler
4846
```
4947

deployment/azure/app/__init__.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
from starlette_cramjam.middleware import CompressionMiddleware
99

1010
from titiler.application import __version__ as titiler_version
11-
from titiler.application.custom import templates
12-
from titiler.application.routers import cog, mosaic, stac, tms
11+
from titiler.application.main import cog, mosaic, stac, templates, tms
1312
from titiler.application.settings import ApiSettings
1413
from titiler.core.errors import DEFAULT_STATUS_CODES, add_exception_handlers
15-
16-
# from titiler.core.middleware import (
17-
# CacheControlMiddleware,
18-
# LoggerMiddleware,
19-
# LowerCaseQueryStringMiddleware,
20-
# TotalTimeMiddleware,
21-
# )
14+
from titiler.core.middleware import (
15+
CacheControlMiddleware,
16+
LoggerMiddleware,
17+
LowerCaseQueryStringMiddleware,
18+
TotalTimeMiddleware,
19+
)
2220
from titiler.mosaic.errors import MOSAIC_STATUS_CODES
2321

2422
api_settings = ApiSettings()
@@ -68,19 +66,18 @@
6866
},
6967
)
7068

71-
# see https://github.com/encode/starlette/issues/1320
72-
# app.add_middleware(
73-
# CacheControlMiddleware,
74-
# cachecontrol=api_settings.cachecontrol,
75-
# exclude_path={r"/healthz"},
76-
# )
69+
app.add_middleware(
70+
CacheControlMiddleware,
71+
cachecontrol=api_settings.cachecontrol,
72+
exclude_path={r"/healthz"},
73+
)
7774

78-
# if api_settings.debug:
79-
# app.add_middleware(LoggerMiddleware, headers=True, querystrings=True)
80-
# app.add_middleware(TotalTimeMiddleware)
75+
if api_settings.debug:
76+
app.add_middleware(LoggerMiddleware, headers=True, querystrings=True)
77+
app.add_middleware(TotalTimeMiddleware)
8178

82-
# if api_settings.lower_case_query_parameters:
83-
# app.add_middleware(LowerCaseQueryStringMiddleware)
79+
if api_settings.lower_case_query_parameters:
80+
app.add_middleware(LowerCaseQueryStringMiddleware)
8481

8582

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

10198

102-
def main(
99+
async def main(
103100
req: func.HttpRequest,
104101
context: func.Context,
105102
) -> func.HttpResponse:
106103
"""Run App in AsgiMiddleware."""
107-
return func.AsgiMiddleware(app).handle(req, context)
104+
return await func.AsgiMiddleware(app).handle_async(req, context)

deployment/azure/host.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"extensionBundle": {
1212
"id": "Microsoft.Azure.Functions.ExtensionBundle",
13-
"version": "[2.*, 3.0.0)"
13+
"version": "[3.*, 4.0.0)"
1414
},
1515
"extensions": {
1616
"http": {

0 commit comments

Comments
 (0)