Define FunctionGraph function handler in Go¶
The Go function handler is the method in your function code that processes events. When your function is invoked, FunctionGraph runs the handler method. Your function runs until the handler returns a response, exits, or times out.
Setting up the Go handler project¶
A typical Go FunctionGraph project is typically structured as follows:
/project-root
├─ go.mod
├─ go.sum
├─ main.go
└─ Makefile
The main logic for the function resides in Go file main.go. When deploying to FunctionGraph make sure to specify the correct handler:
The name of the handler is the name of the Go executable file.
Example code for Go FunctionGraph function¶
A FunctionGraph function written in Go is authored as a Go executable. You can initialize a Go FunctionGraph function project the same way you would initialize any other Go project using the following go mod init command:
go mod init example
Here, example is the module name. You can replace this with anything.
This command initializes your project in the current folder and generates the go.mod file that lists your project’s dependencies.
Use the go get command to add any external dependencies to your project.
For example, for all FunctionGraph functions in Go, you must include the github.com/opentelekomcloud-community/otc-functiongraph-go-runtime package, which implements the FunctionGraph programming model for Go.
Include this package with the following go get command:
go get github.com/opentelekomcloud-community/otc-functiongraph-go-runtime
Your function code should live in a Go file. In the following example, we name this file main.go. In this file, you implement your core function logic in a handleRequest method, as well as a main() function that starts the runtime with this handler.
Example Go FunctionGraph function code¶
The following example shows a simple FunctionGraph function written in Go.
package main
import (
"fmt"
"github.com/opentelekomcloud-community/otc-functiongraph-go-runtime/go-runtime/go-api/context"
"github.com/opentelekomcloud-community/otc-functiongraph-go-runtime/go-runtime/pkg/runtime"
)
// handleRequest is your function handler method
func handleRequest(event []byte, ctx context.RuntimeContext) (string, error) {
logger := ctx.GetLogger()
logger.Logf("Function invoked with event: %v", event)
return fmt.Sprintf("Hello, %v!", string(event)), nil
}
// main function starts the runtime with your handler
func main() {
runtime.Register(handleRequest)
}
Syntax for creating a handler function in Go:¶
func Handler ( payload [] byte , ctx context.RuntimeContext)
Entry function name (Handler):
Entry function name.
Execution event body (payload)
The execution event parameters entered by the user in the function execution interface, in the format of a JSON object.
Context (ctx):
The function execution context provided by Runtime.
Return values of the handler function¶
return object, error
If the error parameter returned by a function is not nil, the function execution fails.
If the error parameter returned by a function is nil, FunctionGraph supports only the following types of values:
Supported return value types¶ object Type
Description
nil
The HTTP response body is empty.
[]byte
The content in this byte array is the body of an HTTP response.
string
The content in this string is the body of an HTTP response.
Other
FunctionGraph returns a value for JSON encoding, and uses the encoded object as the body of an HTTP response.
The Content-Type header of the HTTP response is set to application/json.
HANDLER_NAME=example
build:
GOARCH=amd64 GOOS=linux CGO_ENABLED=0 go build -o target/$(HANDLER_NAME) main.go
zip:
zip -j deploy.zip target/$(HANDLER_NAME)
clean:
rm -f deploy.zip
rm -rf target
all: build zip
The Makefile automates the build process for your Go FunctionGraph function.
You can run the make build command in the project root directory to compile your function code and generate the executable file named example.
The make zip command creates a deployment package named deploy.zip that contains the executable file.
You can then upload this deployment package to FunctionGraph.
Accessing and using the FunctionGraph context object¶
The Context interface allows functions to obtain the function execution context, such as information about the invocation, function, execution environment, and so on.
The context is of type ctx context.RuntimeContext
and is the second argument of the handler function.
To produce logs in OpenTelekomCloud Log Tank Servics (LTS) you can use
ctx.GetLogger() to get a RuntimeLogger object for logging.
ctx.GetLogger().Logf("Received payload: %s", string(payload))
Besides of logging, you can also use the context object for function monitoring. For more information about the context object, see Using the FunctionGraph context object to retrieve function information.
Accessing environment variables¶
Environment variables defined in OpenTelekomCloud ->
Configuration -> Environment Variables can be accessed using:
// accessing an environment variable named "ENV_VAR1"
ctx.getUserData("ENV_VAR1");