radu-matei / azure-functions-golang-worker

Golang worker for Azure Functions
MIT License
41 stars 6 forks source link
azure-functions golang grpc serverless

Azure Functions Golang Worker

circle

This project aims to add Golang support for Azure Functions.

How to run the sample

In order to register Golang as a worker for the Azure Functions Runtime you need to implement an IWorkerProvider as described here. I already did this in a fork of the Azure Functions Runtime and you can find all modifications here and pushed a Docker image on Docker Hub based on the Dockerfile here

To build the the worker and sample you need to:

Then, if you go to localhost:81/api/HttpTriggerGo, your Run method from the sample should be executed.

If you have an Azure storage account and want to run the blob binding sample, then uncomment the following lines from the Dockerfile:

#WORKDIR /go/src/github.com/radu-matei/azure-functions-golang-worker/sample/HttpTriggerBlobBindingGo
#RUN go build -buildmode=plugin -o bin/HttpTriggerBlobBindingGo.so main.go

as well as:

#WORKDIR /go/src/github.com/radu-matei/azure-functions-golang-worker/sample/HttpTriggerBlobBindingInOutGo
#RUN go build -buildmode=plugin -o bin/HttpTriggerBlobBindingInOutGo.so main.go

They are commented as when started, the runtime tries to connect to the storage account - if the storage account key is not present, it will fail

Then, you need to pass the storage account key when starting the container:

docker run -p 81:80 -e AzureWebJobsStorage=DefaultEndpointsProtocol="your-storage-account-key" azure-functions-go-sample

Let's see how a blob binding function looks like - first, function.json:

{
  "entryPoint": "Run",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req"
    },
    {
      "name": "inBlob",
      "type": "blob",
      "direction": "in",
      "path": "demo/{inblobname}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outBlob",
      "type": "blob",
      "direction": "out",
      "path": "demo/{outblobname}",
      "connection": "AzureWebJobsStorage"
    }
  ],
  "disabled": false
}

Things to notice:

Now let's see the Golang function:

package main

import (
    log "github.com/sirupsen/logrus"

    "github.com/radu-matei/azure-functions-golang-worker/azfunc"
)

// Run is the entrypoint to our Go Azure Function - if you want to change it, see function.json
func Run(ctx *azfunc.Context, req *http.Request, inBlob *azfunc.Blob, outBlob *azfunc.Blob) BlobData {
    log.SetLevel(log.DebugLevel)

    log.Debugf("function id: %s, invocation id: %s", ctx.FunctionID, ctx.InvocationID)

    d := BlobData{
        Name: req.URL.Query().Get("name"),
        Data: inBlob.Data,
    }

    outBlob.Data = "Leeeet's hope this doesn't miserably fail..."

    return d
}

// BlobData mocks any struct (or pointer to struct) you might want to return
type BlobData struct {
    Name string
    Data string
}

Things to notice:

The content of the parameters is populated based on the name of the parameter! You can change the order, but the name has to be consistent with the name of the binding defined in function.json!

Calling that function:

Accessing http://localhost:81/api/HttpTriggerBlobBindingInOutGo?inblobname=your-input-blob&outblobname=your-output-blob&name=gopher, the function will receive as inBlob the contents of your-input-blob and will write some string in your-output-blob, returning a response body back to the HTTP response.

Disclaimer

This is not an official Azure Project - it is an unofficial project to support native Golang in Azure Functions by implementing the Worker for v2 - more details here

It is not officially supported by Microsoft and it is not guaranteed to be supported or even work.