README ¶
Custom OpenFaaS Templates
This repository contains templates for OpenFaaS which I use for my OpenFaaS functions.
$ faas-cli template pull https://github.com/retgits/of-templates
$ faas-cli new --list
Languages available as templates:
- gomods-http
- go-arti-http
gomods-http
The template gomods-http is equivalent to golang-http
with the exception being this template uses Go modules and the Go 1.12.5
go-arti-http
The template go-arti-http is equivalent to gomods-http
with the exception being this template uses JFrog Artifactory to resolve Go modules. If you need an Artifactory instance yo try it out, you can use JFrog's testdrive and get a four-day triel of both Artifactory and Xray.
Status of the templates
The template is customized to suit my needs, though if you have any thoughts or suggestions, I'd love to hear them.
Just like the golang-http
template, this template makes use of the OpenFaaS incubator project of-watchdog.
Trying the templates
$ faas-cli template pull https://github.com/retgits/of-templates
$ faas-cli new --lang <template> <fn-name>
The above commands will create a folder with the name of your function and a set of files:
.
├── go.mod
├── go.sum
└── handler.go
You can change the name of your package by updating the name in the go.mod
file. The build template will automatically pick up the correct name when building an OpenFaaS function. It is important that you keep the package function
declaration at the top of handler.go
.
When using the go-arti-http
template, you'll need to set a few build-args
for it to work.
# The command
faas-cli build -f stacks.yml -b APP_VERSION=<version of your app> -b APP_NAME=<name of your app> -b ARTIFACTORY_URL=<artifactory host> -b ARTIFACTORY_USER=<artifactory user> -b ARTIFACTORY_PASSWD=<artifactory password>
The build automatically creates a buildInfo
in Artifactory and publishes the dependencies in case they do not yet exist.
Example usage
The below samples are from the OpenFaaS repository.
Example writing a successful message:
package function
import (
"fmt"
"net/http"
"github.com/openfaas-incubator/go-function-sdk"
)
// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
var err error
message := fmt.Sprintf("Hello world, input was: %s", string(req.Body))
return handler.Response{
Body: []byte(message),
}, err
}
Example writing a custom status code
package function
import (
"fmt"
"net/http"
"github.com/openfaas-incubator/go-function-sdk"
)
// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
var err error
return handler.Response{
Body: []byte("Your workload was accepted"),
StatusCode: http.StatusAccepted,
}, err
}
Example writing an error / failure.
package function
import (
"fmt"
"net/http"
"github.com/openfaas-incubator/go-function-sdk"
)
// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
var err error
return handler.Response{
Body: []byte("the input was invalid")
}, fmt.Errorf("invalid input")
}
The error will be logged to stderr
and the body
will be written to the client along with a HTTP 500 status code.
Example reading a header.
package function
import (
"log"
"github.com/openfaas-incubator/go-function-sdk"
)
// Handle a function invocation
func Handle(req handler.Request) (handler.Response, error) {
var err error
log.Println(req.Header) // Check function logs for the request headers
return handler.Response{
Body: []byte("This is the response"),
Header: map[string][]string{
"X-Served-By": []string{"My Awesome Function"},
},
}, err
}