Skip to content

Commit 567bf43

Browse files
authored
Merge pull request #68 from devsapp/fix-go
fix custom golang event example
2 parents 86d66d2 + 635fc37 commit 567bf43

File tree

9 files changed

+55
-27
lines changed

9 files changed

+55
-27
lines changed

custom-function/golang/fc-custom-golang-event/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Application
22
Name: fc-custom-golang-event
33
Provider:
44
- 阿里云
5-
Version: 1.1.22
5+
Version: 1.1.23
66
Description: 快速部署一个基于custom runtime 的 Golang Event 类型的 Hello World 到阿里云函数计算
77
HomePage: https://github.com/devsapp/start-fc
88
Tags:

custom-function/golang/fc-custom-golang-event/readme.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,43 @@
5353
- 初始化项目:`s init fc-custom-golang-event -d fc-custom-golang-event`
5454
- 进入项目,并进行项目部署:`cd fc-custom-golang-event && s deploy -y`
5555

56+
> 注意: s deploy 之前的 actions 中 pre-deploy 中完成了编译, 如果编译过程中 go mod 下载很慢,可以考虑使用国内 go proxy 代理 [https://goproxy.cn/](https://goproxy.cn/)
57+
5658
</deploy>
5759

5860
<appdetail id="flushContent">
5961

6062
# 应用详情
6163

64+
本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑
65+
66+
## 如何本地调试
67+
直接根据您的平台完成编译, 然后将目标二进制运行起来, 其实本质是启动了一个 http server,然后对这个 http server 发动 http 请求即可
6268

69+
**build**
6370

64-
本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑
71+
```bash
72+
$ cd code
73+
74+
# linux
75+
$ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go
76+
77+
# mac
78+
$ GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go
79+
80+
# windows
81+
$ GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go
82+
```
83+
84+
**debug**
6585

86+
``` bash
87+
# 打开一个终端, 运行 target/main
88+
# 然后打开另外一个终端,curl 发 http 请求
89+
$ curl 127.0.0.1:9000/invoke -d "my event" -H "x-fc-request-id:rid123456"
90+
```
6691

92+
![](https://img.alicdn.com/imgextra/i4/O1CN019fgqet1haF7QDSTT3_!!6000000004293-2-tps-2338-358.png)
6793

6894
</appdetail>
6995

custom-function/golang/fc-custom-golang-event/src/Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

custom-function/golang/fc-custom-golang-event/src/build-image/Dockerfile

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module example.com/m
2+
3+
go 1.18
4+
5+
require github.com/awesome-fc/golang-runtime v0.0.0-20230119040721-3f65ab4b97d3
6+
7+
require (
8+
github.com/sirupsen/logrus v1.9.0 // indirect
9+
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 // indirect
10+
golang.org/x/sys v0.4.0 // indirect
11+
)

custom-function/golang/fc-custom-golang-event/src/code/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ func initialize(ctx *gr.FCContext) error {
1212
}
1313

1414
func handler(ctx *gr.FCContext, event []byte) ([]byte, error) {
15-
fcLogger := gr.GetLogger().WithField("requestId", ctx.RequestID)
15+
fcLogger := ctx.GetLogger()
1616
_, err := json.Marshal(ctx)
1717
if err != nil {
1818
fcLogger.Error("error:", err)
1919
}
2020
fcLogger.Infof("hello golang!")
21+
fcLogger.Infof("hello golang2!")
2122
return event, nil
2223
}
2324

custom-function/golang/fc-custom-golang-event/src/s.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ services:
3434
# 如果不带有 helloworld ,而是直接执行 s build,工具则会对当前Yaml下,所有和 helloworld 平级的业务模块(如有其他平级的模块,例如下面注释的next-function),按照一定顺序进行 build 操作
3535
component: fc # 组件名称,Serverless Devs 工具本身类似于一种游戏机,不具备具体的业务能力,组件类似于游戏卡,用户通过向游戏机中插入不同的游戏卡实现不同的功能,即通过使用不同的组件实现不同的具体业务能力
3636
actions: # 自定义执行逻辑,关于actions 的使用,可以参考:https://www.serverless-devs.com/serverless-devs/yaml#行为描述
37-
pre-deploy: # 在deploy之前运行
38-
- run: make build
39-
path: ./
37+
pre-deploy: # 在deploy之前运行
38+
- run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go
39+
path: ./code
4040
# - component: fc build --use-docker --dockerfile ./code/Dockerfile # 要运行的组件,格式为【component: 组件名 命令 参数】(可以通过s cli registry search --type Component 获取组件列表)
4141
# - run: docker build xxx # 要执行的系统命令,类似于一种钩子的形式
4242
# path: ./src # 执行系统命令/钩子的路径
@@ -54,7 +54,10 @@ services:
5454
timeout: 30
5555
memorySize: 512
5656
runtime: custom
57-
codeUri: ./code
57+
codeUri: ./code/target
58+
customRuntimeConfig:
59+
command:
60+
- '/code/main'
5861
# next-function: # 第二个函数的案例,仅供参考
5962
# # 如果在当前项目下执行 s deploy,会同时部署模块:
6063
# # helloworld:服务hello-world-service,函数cpp-event-function

custom-function/golang/fc-custom-golang-event/src/s_en.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ services:
3535
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
3636
actions: # Customize execution logic. For the use of actions, please refer to: https://www.serverless-devs.com/serverless-devs/yaml#Behavior description
3737
pre-deploy: # run before deploy
38-
- run: make build
39-
path: ./
38+
- run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o target/main main.go
39+
path: ./code
4040
# - 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)
4141
# - run: docker build xxx # System command to execute, similar to a hook
4242
# path: ./src # The path to execute system commands/hooks
@@ -54,7 +54,10 @@ services:
5454
timeout: 30
5555
memorySize: 512
5656
runtime: custom
57-
codeUri: ./code
57+
codeUri: ./code/target
58+
customRuntimeConfig:
59+
command:
60+
- '/code/main'
5861
# next-function: # The case of the second function, just for reference
5962
# # If you execute s deploy under the current project, the modules will be deployed at the same time:
6063
# # helloworld: service hello-world-service, function cpp-event-function

update.list

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
event-function/fc-event-golang1.x
2-
http-function/fc-http-golang1.x
1+
custom-function/golang/fc-custom-golang-event

0 commit comments

Comments
 (0)