GithubHelp home page GithubHelp logo

ptzagk / azure-functions-golang-worker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from radu-matei/azure-functions-golang-worker

0.0 1.0 0.0 3.82 MB

Golang worker for Azure Functions

License: MIT License

Makefile 1.99% Go 91.60% Dockerfile 5.89% Shell 0.52%

azure-functions-golang-worker's Introduction

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:

  • docker build -t azure-functions-go-sample .
  • docker run -p 81:80 -it azure-functions-go-sample

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:

  • entryPoint - this is the name of the function used as entrypioint
  • inBlob - in blob binding - when executed, the runtime will search for a serialized key-value pair, with the key inblobname and will give as input data to your function the contents of the blob specified by inblobname - we easily pass this as a query string in the HTTP request
  • outBlob - we want to write something to this blob (and create it if it doesn't exist) - the name of the blob to create is passed the same as for inblobname, through a query string

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:

  • we can use any vendored dependencies we might have available at compile time (everything is packaged as a Golang plugin)
  • the name of the function is Run - can be changed, just remember to do the same in function.json
  • the function signature - func Run(ctx *azfunc.Context, req *http.Request, inBlob *azfunc.Blob, outBlob *azfunc.Blob) BlobData - based on the function.json, req, inBlob, outBlob and ctx are automatically populated by the worker

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!

  • you can have a return type from the function that, in the case of the HTTPTrigger is packaged back as the response body - the discussion regarding idiomatic return types is still open

  • outBlob is an output binding - after the function is executed, the contents of the outBlob object is marshaled and sent back to the function runtime

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.

azure-functions-golang-worker's People

Contributors

radu-matei avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.