Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

[BUG] routePrefix cannot be non-empty for FastAPI to work with ASGI Function on Python v2 #1310

Open lac-anakagawa opened 10 months ago

lac-anakagawa commented 10 months ago

Investigative information

Please provide the following:

Repro steps

Provide the steps required to reproduce the problem:
  1. Start a new Azure Function app with the following command: func init <> --worker-runtime python --model V2
  2. In function_app.py, create a new ASGI Function App with the following code:
    
    import azure.functions as func
    import logging

from fastapi import FastAPI

api = FastAPI()

@api.get("/") def root(): return {"message": "Hello World"}

@api.get("/healthcheck") def healthcheck(): return 1

app = func.AsgiFunctionApp( app=api, http_auth_level=func.AuthLevel.ANONYMOUS )

3. Add `fastapi` to `requirements.txt` (currently on 0.103.0)
4. Add the following to `host.json`:

"extensions": { "http": { "routePrefix": "api" } }

6. Run `func start` using core tools from cli

### Expected behavior
###### Provide a description of the expected behavior.

<!--
Example:

 - After I perform step B, the lights in the house should turn off.
-->

Should be able to access `http://localhost:7071/api` with no issue.

### Actual behavior
###### Provide a description of the actual behavior observed.
Following error is received:

❯ func start Found Python version 3.10.11 (py).

Azure Functions Core Tools Core Tools Version: 4.0.5274 Commit hash: N/A (64-bit) Function Runtime Version: 4.23.0.20886

[2023-08-28T23:02:24.316Z] Worker process started and initialized. [2023-08-28T23:02:24.385Z] A host error has occurred during startup operation 'a87fe4da-9c2c-4dd7-958f-c9b73021ea2c'. [2023-08-28T23:02:24.387Z] Microsoft.AspNetCore.Routing: An error occurred while creating the route with name 'http_app_func' and template 'api//{*route}'. Microsoft.AspNetCore.Routing: The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value. (Parameter 'routeTemplate'). Microsoft.AspNetCore.Routing: The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value. [2023-08-28T23:02:24.399Z] Failed to stop host instance '4b8b0863-dcb1-4a41-9b70-0beaad496108'. [2023-08-28T23:02:24.401Z] Microsoft.Azure.WebJobs.Host: The host has not yet started. Value cannot be null. (Parameter 'provider') [2023-08-28T23:02:24.409Z] Host startup operation has been canceled


<!--
Example:

- Step B actually causes my cat to meow for some reason.
-->

### Known workarounds
###### Provide a description of any known workarounds.

Only workaround is to make the `routePrefix` in `host.json` an empty string.

{ "version": "2.0", "logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } } }, "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[4.*, 5.0.0)" }, "extensions": { "http": { "routePrefix": "" } } }


<!--
Example:

- Turn off the circuit breaker for the lights.
-->

### Contents of the requirements.txt file:
###### Provide the requirements.txt file to help us find out module related issues.

<!--
Example:

azure-functions azure-storage-blob==12.1.0

-->

azure-functions fastapi==0.103.0


### Related information
###### Provide any related information

* Links to source
* Bindings used

<details>
<summary>Source</summary>

```python
# __init__.py

import azure.functions as func
import logging

from fastapi import FastAPI

api = FastAPI()

@api.get("/")
def root():
    return {"message": "Hello World"}

@api.get("/healthcheck")
def healthcheck():
    return 1

app = func.AsgiFunctionApp(
    app=api,
    http_auth_level=func.AuthLevel.ANONYMOUS
)
azure-functions
fastapi==0.103.0

johschmidt42 commented 10 months ago

Can confirm this behaviour. Only works with an empty string for routePrefix. However, if routePrefix is empty in the host.json, then the GitHub action static-web-apps-deploy@v1 will throw:

Error in processing api build artifacts: the host.json file cannot specify a http.routePrefix value other than 'api'.

Because all endpoints should be defined with api/.

In other words:

pietz commented 8 months ago

Not sure this helps anyone but if you deploy the backend as a separate function app, you can set the prefix to an empty string and make everything work.

I think the easiest way to fix this would be to remove the thrown error from the GitHub workflow. As far as I understand it should be a warning anyway. Simply rely on users setting all api paths under /api. All other routes will simply not work.

Edward-Zhou commented 8 months ago

@pietz Could you share how to remove the thrown error? After removing, will it work after static-web-apps-deploy@v1?

johschmidt42 commented 5 months ago

Can this issue get more attention please? Not being able to lift & shift your fastAPI (or any other?) application is such an bummer..

MaxOSchulte commented 3 months ago

Confirm, Error is still present.

Azure Functions + fastAPI + v2 Python model + Azure Static Web App

Wanted to deploy a demo Angular + FastAPI & GenAI.

seidnerj commented 2 months ago

Any update on this...?

seidnerj commented 2 months ago

For the time being I manually patched this by removing the excess "/" in route="/{*route}" in http_app_func's decorator under the AsgiFunctionApp class. It's a terrible solution. I hope this is fixed soon.

jparta commented 5 days ago

@bhagyshricompany Any updates? This ought to be fixed sooner than later.