Azure / azure-functions-dapr-extension

Extension for interacting with Dapr APIs from an Azure Function
MIT License
100 stars 34 forks source link

Is it possible to run the Azure Functions Dapr Extension with a Custom Handler? #190

Open cmelo05 opened 8 months ago

cmelo05 commented 8 months ago

I am experimenting with an odd scenario. Basically I want to subscribe to a Dapr Topic from a Go function inside Azure Functions.

For the Go function, I was able to register a Custom Handler that can handle the HTTP Requests:

func orderHandler(w http.ResponseWriter, r *http.Request) {
    log.Println("Handler invoked from an event")

    var invokeRequest InvokeRequest

    d := json.NewDecoder(r.Body)
    if err := d.Decode(&invokeRequest); err != nil {
        log.Fatal("Error while decoding the Body")
        return
    }

    log.Println(invokeRequest)
}

And inside the main function, I have the code to register the endpoints

func main() {
    customHandlerPort, exists := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT")
    if !exists {
        customHandlerPort = "8080"
    }
    mux := http.NewServeMux()
    mux.HandleFunc("/orders", orderHandler)
    fmt.Println("Go server Listening on: ", customHandlerPort)
    log.Fatal(http.ListenAndServe(":"+customHandlerPort, mux))
}

I added the customHandler property inside the host.json file:

"customHandler": {
    "description": {
      "defaultExecutablePath": "handler.exe",
      "workingDirectory": "",
      "arguments": []
    }
  }

And created Function called DaprEventTrigger with the following function.json:

{
  "bindings": [
    {
      "name": "eventTrigger",
      "type": "daprTopicTrigger",
      "pubsubname": "orderpubsub",
      "topic": "orders",
      "direction": "in"
    }
  ]
}

To run this I am running the following cmd:

dapr run --app-id functionA --resources-path ./components/ --app-port 3001 -- func host start

Inside the components folder I only have one file called pubsub.yaml, which contains the Redis Component.

Right after the initialization I get this error:

time="2024-03-06T10:13:24.3482556-03:00" level=error msg="app returned http status code 500 from subscription endpoint" app_id=functionA instance= scope=dapr.runtime.processor.pubsub type=log ver=1.12.5

I suppose there's something going on with the Subscription Endpoint, which is falling with 500 HTTP Error in Dapr initialization and also when I try to call localhost:3001/dapr/subscribe

Is there a way to run a Custom Handler with Dapr Topic Subscription in Azure Functions?

I have also tried:

raorugan commented 7 months ago

Interesting!! is using Go lang a hard requirement or can one of the supported languages from the list be explored?

szymon3 commented 2 months ago

@cmelo05 did you find a solution to your issue?