azure-functions-python-worker icon indicating copy to clipboard operation
azure-functions-python-worker copied to clipboard

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

Open lac-anakagawa opened this issue 1 year ago • 14 comments

Investigative information

Please provide the following:
  • Timestamp: 2023-08-28T22:49:33+00:00
  • Function App name:
  • Function name(s) (as appropriate):
  • Core Tools version: 4.0.5274

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
)
  1. Add fastapi to requirements.txt (currently on 0.103.0)
  2. Add the following to host.json:
"extensions": {
    "http": {
      "routePrefix": "api"
    }
  }
  1. Run func start using core tools from cli

Expected behavior

Provide a description of the expected behavior.

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

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": ""
    }
  }
}

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions
fastapi==0.103.0

Related information

Provide any related information
  • Links to source
  • Bindings used
Source
# __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

lac-anakagawa avatar Aug 28 '23 23:08 lac-anakagawa