Skip to content

Commit

Permalink
Merge pull request #68 from devsapp/fix-go
Browse files Browse the repository at this point in the history
fix custom golang event example
  • Loading branch information
rsonghuster authored Jan 19, 2023
2 parents 86d66d2 + 635fc37 commit 567bf43
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion custom-function/golang/fc-custom-golang-event/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 27 additions & 1 deletion custom-function/golang/fc-custom-golang-event/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
</deploy>

<appdetail id="flushContent">

# 应用详情

本应用仅作为学习和参考使用,您可以基于本项目进行二次开发和完善,实现自己的业务逻辑

## 如何本地调试
直接根据您的平台完成编译, 然后将目标二进制运行起来, 其实本质是启动了一个 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)

</appdetail>

Expand Down
9 changes: 0 additions & 9 deletions custom-function/golang/fc-custom-golang-event/src/Makefile

This file was deleted.

This file was deleted.

11 changes: 11 additions & 0 deletions custom-function/golang/fc-custom-golang-event/src/code/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 7 additions & 4 deletions custom-function/golang/fc-custom-golang-event/src/s.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 # 执行系统命令/钩子的路径
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions custom-function/golang/fc-custom-golang-event/src/s_en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions update.list
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
event-function/fc-event-golang1.x
http-function/fc-http-golang1.x
custom-function/golang/fc-custom-golang-event

0 comments on commit 567bf43

Please sign in to comment.