# Functions Framework for Go
[![GoDoc](https://godoc.org/github.com/GoogleCloudPlatform/functions-framework-go?status.svg)](http://godoc.org/github.com/GoogleCloudPlatform/functions-framework-go) [![Go version](https://img.shields.io/badge/go-v1.11+-blue)](https://golang.org/dl/#stable)
[![Go unit CI][ff_go_unit_img]][ff_go_unit_link] [![Go lint CI][ff_go_lint_img]][ff_go_lint_link] [![Go conformace CI][ff_go_conformance_img]][ff_go_conformance_link]
An open source FaaS (Function as a Service) framework for writing portable
Go functions, brought to you by the Google Cloud Functions team.
The Functions Framework lets you write lightweight functions that run in many
different environments, including:
* [Google Cloud Functions](https://cloud.google.com/functions/)
* Your local development machine
* [Knative](https://github.com/knative/)-based environments
* [Google App Engine](https://cloud.google.com/appengine/docs/go/)
* [Google Cloud Run](https://cloud.google.com/run/docs/quickstarts/build-and-deploy)
The framework allows you to go from:
```golang
func HelloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, World!")
}
```
To:
```sh
curl http://my-url
# Output: Hello, World!
```
All without needing to worry about writing an HTTP server or request
handling logic.
## Features
* Build your Function in the same container environment used by Cloud Functions using [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks).
* Invoke a function in response to a request
* Automatically unmarshal events conforming to the
[CloudEvents](https://cloudevents.io/) spec
* Portable between serverless platforms
## Quickstart: Hello, World on your local machine
1. Install Go 1.11+.
1. Create a Go module:
```sh
go mod init example.com/hello
```
> Note: You can use a different module name rather than `example.com/hello`.
1. Create a `function.go` file with the following contents:
```golang
package function
import (
"fmt"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
functions.HTTP("HelloWorld", helloWorld)
}
// helloWorld writes "Hello, World!" to the HTTP response.
func helloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
}
```
> Note that you can use any file name or package name (convention is to make
package name same as directory name).
1. To run locally, you'll need to create a main package to start your server
(see instructions below for container builds to skip this step and match your
local development environment to production):
```sh
mkdir cmd
```
1. Create a `cmd/main.go` file with the following contents:
```golang
package main
import (
"log"
"os"
// Blank-import the function package so the init() runs
_ "example.com/hello"
"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
)
func main() {
// Use PORT environment variable, or default to 8080.
port := "8080"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
if err := funcframework.Start(port); err != nil {
log.Fatalf("funcframework.Start: %v\n", err)
}
}
```
1. Run `go mod tidy` to update dependency requirements.
1. Start the local development server:
```sh
export FUNCTION_TARGET=HelloWorld
go run cmd/main.go
# Output: Serving function: HelloWorld
```
Upon starting, the framework will listen to HTTP requests at `/` and invoke your registered function
specified by the `FUNCTION_TARGET` environment variable (i.e. `FUNCTION_TARGET=HelloWorld`).
1. Send requests to this function using `curl` from another terminal window:
```sh
curl localhost:8080
# Output: Hello, World!
```
## Go further: build a deployable container
1. Install [Docker](https://store.docker.com/search?type=edition&offering=community) and the [`pack` tool](https://buildpacks.io/docs/install-pack/).
1. Build a container from your function using the Functions [buildpacks](https://github.com/GoogleCloudPlatform/buildpacks):
```sh
pack build \
--builder gcr.io/buildpacks/builder:v1 \
--env GOOGLE_FUNCTION_SIGNATURE_TYPE=http \
--env GOOGLE_FUNCTION_TARGET=HelloWorld \
my-first-function
```
1. Start the built container:
```sh
docker run --rm -p 8080:8080 my-first-function
# Output: Serving function...
```
1. Send requests to this function using `curl` from another terminal window:
```sh
curl localhost:8080
# Output: Hello, World!
```
## Run your function on serverless platforms
### Google Cloud Functions
Deploy from your local machine using the `gcloud` command-line tool.
[Check out the Cloud Functions quickstart](https://cloud.google.com/functions/docs/quickstart).
### Container environments based on Knative
The Functions Framework is designed to be compatible with Knative environments.
Just build and deploy your container to a Knative environment. Note that your app needs to listen
`PORT` environment variable per [Knative runtime contract](https://github.com/knative/serving/blob/master/docs/runtime-contract.md#inbound-network-connectivity).
## Functions Framework Features
The Go Functions Framework conforms to the [Functions Framework Contract](https://github.com/GoogleCloudPlatform/functions-framework), As such, it
supports HTTP functions, background event functions, and CloudEvent functions
(as of v1.1.0). The primary build mechanism is the [GCP buildpacks stack](https://github.com/GoogleCloudPlatform/buildpacks), which takes a function of
one of the accepted types, converts it to a full HTTP serving app, and creates a
launchable container to run the server.
### HTTP Functions
The Framework provides support for handling native Go HTTP-style functions:
```golang
package function
import (
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
functions.HTTP("HelloWorld", helloWorld)
}
func helloWorld(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
```
### CloudEvent Functions
The Functions Framework provides support for unmarshalling an incoming
[CloudEvent](https://cloudevents.io/) payload into a `cloudevents.Event` object.
These will be passed as arguments to your function when it receives a request.
```golang
package function
import (
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
functions.CloudEvent("CloudEventFunc", cloudEventFunc)
}
func cloudEventFunc(ctx context.Context, e cloudevents.Event) error {
// Do something with event.Context and event.Data (via event.DataAs(foo)).
return nil
}
```
These functions are registered with the handler via `funcframework.RegisterCloudEventFunctionContext`.
To learn more about CloudEvents, see the [Go SDK for CloudEvents](https://github.com/cloudevents/sdk-go).
### Background Event Functions
[Background events](https://cloud.google.com/functions/docs/writing/background)
are also supported. This type of function takes two parameters: a Go context and
a user-defined data struct.
```golang
func BackgroundEventFunction(ctx context.Context, data userDefinedEventStruct) error {
// Do something with ctx and data.
}
```
This type of event requires you to define a struct with the
appropriate data fields (e.g. those for a PubSub message or GCS event) and pass
that struct as the data parameter. See the [samples](https://cloud.google.com/functions/docs/writing/background) for details.
The context parameter is a Go `context.Context`, and contains additional event
metadata under a functions-specific key. This data is accesible via the `cloud.google.com/go/functions/metadata` package:
```golang
m := metadata.FromContext(ctx)
```
These functions can be registered in `main.go` for local testing with the handler via `funcframework.RegisterEventFunctionContext`.
[ff_go_unit_img]: https://github.com/GoogleCloudPlatform/functions-framework-go/wor
没有合适的资源?快使用搜索试试~ 我知道了~
FaaS(函数即服务)框架,用于编写可移植的Go函数___下载.zip
共35个文件
go:16个
yml:8个
md:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 170 浏览量
2023-04-18
00:33:06
上传
评论
收藏 73KB ZIP 举报
温馨提示
FaaS(函数即服务)框架,用于编写可移植的Go函数___下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
FaaS(函数即服务)框架,用于编写可移植的Go函数___下载.zip (35个子文件)
functions-framework-go-master
go.mod 195B
.github
blunderbuss.yml 130B
release-please.yml 76B
renovate.json 288B
workflows
unit.yml 867B
conformance.yml 4KB
scorecard.yml 2KB
codeql.yml 2KB
buildpack-integration-test.yml 2KB
lint.yml 902B
go.sum 56KB
LICENSE 11KB
CONTRIBUTING.md 2KB
funcframework
framework_test.go 30KB
events.go 19KB
events_test.go 29KB
framework.go 13KB
CHANGELOG.md 5KB
internal
fftypes
types.go 533B
registry
registry_test.go 4KB
registry.go 4KB
events
pubsub
pubsub_test.go 7KB
pubsub.go 3KB
.gitignore 69B
functions
functions.go 1KB
testdata
conformance
cmd
declarative
main.go 1KB
legacyevent
main.go 1KB
cloudevent
main.go 1KB
http
main.go 1KB
prerun.sh 1KB
run_conformance_tests.sh 2KB
function
function.go 2KB
nondeclarative
function.go 2KB
.gitignore 240B
README.md 8KB
共 35 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功