From 635fc3775776a5589f34788627f9c91a057d9aec Mon Sep 17 00:00:00 2001 From: rsonghuster Date: Thu, 19 Jan 2023 12:41:15 +0800 Subject: [PATCH] fix custom golang event example --- .../fc-custom-golang-event/publish.yaml | 2 +- .../golang/fc-custom-golang-event/readme.md | 28 ++++++++++++++++++- .../fc-custom-golang-event/src/Makefile | 9 ------ .../src/build-image/Dockerfile | 6 ---- .../fc-custom-golang-event/src/code/go.mod | 11 ++++++++ .../fc-custom-golang-event/src/code/main.go | 3 +- .../golang/fc-custom-golang-event/src/s.yaml | 11 +++++--- .../fc-custom-golang-event/src/s_en.yaml | 9 ++++-- update.list | 3 +- 9 files changed, 55 insertions(+), 27 deletions(-) delete mode 100644 custom-function/golang/fc-custom-golang-event/src/Makefile delete mode 100644 custom-function/golang/fc-custom-golang-event/src/build-image/Dockerfile create mode 100644 custom-function/golang/fc-custom-golang-event/src/code/go.mod diff --git a/custom-function/golang/fc-custom-golang-event/publish.yaml b/custom-function/golang/fc-custom-golang-event/publish.yaml index 4d974d3..49d99c0 100644 --- a/custom-function/golang/fc-custom-golang-event/publish.yaml +++ b/custom-function/golang/fc-custom-golang-event/publish.yaml @@ -2,7 +2,7 @@ Type: Application Name: fc-custom-golang-event Provider: - 阿里云 -Version: 1.1.22 +Version: 1.1.23 Description: 快速部署一个基于custom runtime 的 Golang Event 类型的 Hello World 到阿里云函数计算 HomePage: https://github.com/devsapp/start-fc Tags: diff --git a/custom-function/golang/fc-custom-golang-event/readme.md b/custom-function/golang/fc-custom-golang-event/readme.md index 1458054..a2d7949 100644 --- a/custom-function/golang/fc-custom-golang-event/readme.md +++ b/custom-function/golang/fc-custom-golang-event/readme.md @@ -53,17 +53,43 @@ - 初始化项目:`s init fc-custom-golang-event -d fc-custom-golang-event` - 进入项目,并进行项目部署:`cd fc-custom-golang-event && s deploy -y` +> 注意: s deploy 之前的 actions 中 pre-deploy 中完成了编译, 如果编译过程中 go mod 下载很慢,可以考虑使用国内 go proxy 代理 [https://goproxy.cn/](https://goproxy.cn/) + # 应用详情 +本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑 + +## 如何本地调试 +直接根据您的平台完成编译, 然后将目标二进制运行起来, 其实本质是启动了一个 http server,然后对这个 http server 发动 http 请求即可 +**build** -本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑 +```bash +$ cd code + +# linux +$ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go + +# mac +$ GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go + +# windows +$ GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go +``` + +**debug** +``` bash +# 打开一个终端, 运行 target/main +# 然后打开另外一个终端,curl 发 http 请求 +$ curl 127.0.0.1:9000/invoke -d "my event" -H "x-fc-request-id:rid123456" +``` +![](https://img.alicdn.com/imgextra/i4/O1CN019fgqet1haF7QDSTT3_!!6000000004293-2-tps-2338-358.png) diff --git a/custom-function/golang/fc-custom-golang-event/src/Makefile b/custom-function/golang/fc-custom-golang-event/src/Makefile deleted file mode 100644 index 9685ebb..0000000 --- a/custom-function/golang/fc-custom-golang-event/src/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -build-img: - docker build -t fc-go-runtime -f build-image/Dockerfile build-image - -build: build-img - docker run --rm -it -v $$(pwd):/tmp fc-go-runtime bash -c "GOARCH=amd64 go build -o /tmp/code//bootstrap /tmp/code/main.go" - chmod +x code/bootstrap - -deploy: build - s deploy -y \ No newline at end of file diff --git a/custom-function/golang/fc-custom-golang-event/src/build-image/Dockerfile b/custom-function/golang/fc-custom-golang-event/src/build-image/Dockerfile deleted file mode 100644 index 4da3bb4..0000000 --- a/custom-function/golang/fc-custom-golang-event/src/build-image/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM golang:1.12.16-stretch -RUN mkdir -p $GOPATH/src/golang.org/x/ -RUN cd $GOPATH/src/golang.org/x/ && git clone https://github.com/golang/net.git -RUN cd $GOPATH/src/golang.org/x/ && git clone https://github.com/golang/sys.git -RUN go get github.com/awesome-fc/golang-runtime -RUN go get github.com/gorilla/websocket \ No newline at end of file diff --git a/custom-function/golang/fc-custom-golang-event/src/code/go.mod b/custom-function/golang/fc-custom-golang-event/src/code/go.mod new file mode 100644 index 0000000..be57253 --- /dev/null +++ b/custom-function/golang/fc-custom-golang-event/src/code/go.mod @@ -0,0 +1,11 @@ +module example.com/m + +go 1.18 + +require github.com/awesome-fc/golang-runtime v0.0.0-20230119040721-3f65ab4b97d3 + +require ( + github.com/sirupsen/logrus v1.9.0 // indirect + github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 // indirect + golang.org/x/sys v0.4.0 // indirect +) diff --git a/custom-function/golang/fc-custom-golang-event/src/code/main.go b/custom-function/golang/fc-custom-golang-event/src/code/main.go index bc86285..70ca43d 100644 --- a/custom-function/golang/fc-custom-golang-event/src/code/main.go +++ b/custom-function/golang/fc-custom-golang-event/src/code/main.go @@ -12,12 +12,13 @@ func initialize(ctx *gr.FCContext) error { } func handler(ctx *gr.FCContext, event []byte) ([]byte, error) { - fcLogger := gr.GetLogger().WithField("requestId", ctx.RequestID) + fcLogger := ctx.GetLogger() _, err := json.Marshal(ctx) if err != nil { fcLogger.Error("error:", err) } fcLogger.Infof("hello golang!") + fcLogger.Infof("hello golang2!") return event, nil } diff --git a/custom-function/golang/fc-custom-golang-event/src/s.yaml b/custom-function/golang/fc-custom-golang-event/src/s.yaml index 4077eac..d0fb235 100644 --- a/custom-function/golang/fc-custom-golang-event/src/s.yaml +++ b/custom-function/golang/fc-custom-golang-event/src/s.yaml @@ -34,9 +34,9 @@ services: # 如果不带有 helloworld ,而是直接执行 s build,工具则会对当前Yaml下,所有和 helloworld 平级的业务模块(如有其他平级的模块,例如下面注释的next-function),按照一定顺序进行 build 操作 component: fc # 组件名称,Serverless Devs 工具本身类似于一种游戏机,不具备具体的业务能力,组件类似于游戏卡,用户通过向游戏机中插入不同的游戏卡实现不同的功能,即通过使用不同的组件实现不同的具体业务能力 actions: # 自定义执行逻辑,关于actions 的使用,可以参考:https://www.serverless-devs.com/serverless-devs/yaml#行为描述 - pre-deploy: # 在deploy之前运行 - - run: make build - path: ./ + pre-deploy: # 在deploy之前运行 + - run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go + path: ./code # - component: fc build --use-docker --dockerfile ./code/Dockerfile # 要运行的组件,格式为【component: 组件名 命令 参数】(可以通过s cli registry search --type Component 获取组件列表) # - run: docker build xxx # 要执行的系统命令,类似于一种钩子的形式 # path: ./src # 执行系统命令/钩子的路径 @@ -54,7 +54,10 @@ services: timeout: 30 memorySize: 512 runtime: custom - codeUri: ./code + codeUri: ./code/target + customRuntimeConfig: + command: + - '/code/main' # next-function: # 第二个函数的案例,仅供参考 # # 如果在当前项目下执行 s deploy,会同时部署模块: # # helloworld:服务hello-world-service,函数cpp-event-function diff --git a/custom-function/golang/fc-custom-golang-event/src/s_en.yaml b/custom-function/golang/fc-custom-golang-event/src/s_en.yaml index d81a6b8..19d2f28 100644 --- a/custom-function/golang/fc-custom-golang-event/src/s_en.yaml +++ b/custom-function/golang/fc-custom-golang-event/src/s_en.yaml @@ -35,8 +35,8 @@ services: component: fc # The name of the component. The Serverless Devs tool itself is similar to a game console and does not have specific business capabilities. The component is similar to a game card. Users can achieve different functions by inserting different game cards into the game console, that is, by using Different components implement different specific business capabilities actions: # Customize execution logic. For the use of actions, please refer to: https://www.serverless-devs.com/serverless-devs/yaml#Behavior description pre-deploy: # run before deploy - - run: make build - path: ./ + - run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go + path: ./code # - component: fc build --use-docker --dockerfile ./code/Dockerfile # The component to run, the format is [component: component name command parameter] (you can get the component list through s cli registry search --type Component) # - run: docker build xxx # System command to execute, similar to a hook # path: ./src # The path to execute system commands/hooks @@ -54,7 +54,10 @@ services: timeout: 30 memorySize: 512 runtime: custom - codeUri: ./code + codeUri: ./code/target + customRuntimeConfig: + command: + - '/code/main' # next-function: # The case of the second function, just for reference # # If you execute s deploy under the current project, the modules will be deployed at the same time: # # helloworld: service hello-world-service, function cpp-event-function diff --git a/update.list b/update.list index 1814aa3..301222e 100644 --- a/update.list +++ b/update.list @@ -1,2 +1 @@ -event-function/fc-event-golang1.x -http-function/fc-http-golang1.x +custom-function/golang/fc-custom-golang-event