This is a guide to help you build and deploy OpenFaaS functions with Golang server
- Golang server. CLI installed.
- Docker running on host.
- OpenFaaS CLI.
- OpenFaaS Gateway (Link to setup-guide) . Running & accessible OpenFaaS gateway.
Create a new repository on github. I used the Desktop client and later switched to terminal.
Change your working directory to your github repository folder. Initialize a Golang server project within the repository with following command :
gos --make
Set FaaS gateway of project. Open the gos.gxml
within your repository folder and update the second line of the file. Add the attribute gateway
to specify the location of your OpenFaaS gateway. The example below is set to the default setting of OpenFaaS :
<gos gateway="http://localhost:8080">
Update your gos.gxml
deploy tag content from webapp
to faas
.
Add a new <end>
tag within <endpoints>
and voila you have a serverless function with full access to http.Request
to retrieve body data. <end>
tags with type attribute set to f
or left blank will not be processed.
Templates are also processed into OpenFaas functions. More information about adding new templates with Golang server.
Build and deploy functions with command
gos --run
Your <end>
's path attribute will be stripped of /
(forward slashes) and the letter following it will be converted to uppercase. This will become the name of your function on OpenFaaS. The Stdin of this function will be converted into a request body.
Your <template>
's name attribute will be the name of your OpenFaaS function. The Stdin of this function is a JSON string which will be converted to the tag's specified struct
attribute.
With end tags write your response as usual with *http.ResponseWriter
's Write
func.
Return JSON of your interfaces with this guide.
I used this Go server repository for this guide.
Template functions compile their linked .tmpl
(with internal package html/template
) file then write the output to Stdout.
The session
variable availble by default to <end>
tags, only spans the scope of the request.