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
├─ src
| └─ main.go
├─ go.mod
├─ go.sum
└─ 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 (here: main) 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:
# enable Go modules to get specific version of the runtime package
go env -w GO111MODULE=on
# install FunctionGraph Go runtime package for latest version
go get github.com/opentelekomcloud-community/otc-functiongraph-go-runtime
# or for a specified version
go get github.com/opentelekomcloud-community/otc-functiongraph-go-runtime@v0.0.1
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)
}
Handler name is: main
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) src/main.go
zip:
printf "# Handler name\n${HANDLER_NAME}" > target/handler_name.txt
cd target && zip -r ../deploy.zip .
clean:
rm -f deploy.zip
rm -rf target
all: build zip
.PHONY: build zip clean all
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.
rem #############################################################
rem # Build script for Golang Event Function
rem # for Linux target on Windows
rem #############################################################
rem set visibility of environment changes to local only
SETLOCAL
rem Set the following parameters to the corresponding value of Linux
set GOARCH=amd64
set GOOS=linux
set CGO_ENABLED=0
set GO111MODULE=on
rem set handler name
set HANDLER_NAME=example
rem create target_win folder if not exists
if not exist "target_win" md "target_win"
rem build the Go HTTP function in target_win folder
go build -o target_win/%HANDLER_NAME% src/main.go
rem package the target_win folder to a zip file
tar.exe -c -a -f deploy.zip -C target_win *
Run the build.cmd script in the project root directory to compile your function code and generate the executable file named example in the target_win directory.
The script also creates a deployment package named deploy.zip in the project root directory using the Windows tar.exe command.
You can then upload this deployment package to FunctionGraph.
For general deployment instructions see Building Functions in User Guide.
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");
Deploy the Event Function using Terraform¶
For details on how to deploy using Terraform, see Deploying an Event Function using Terraform.