Skip to content

Commit

Permalink
add custom go http example
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed Jan 19, 2023
1 parent 44dbc9e commit f4ac84e
Show file tree
Hide file tree
Showing 14 changed files with 523 additions and 112 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.24
Version: 1.1.25
Description: 快速部署一个基于custom runtime 的 Golang Event 类型的 Hello World 到阿里云函数计算
HomePage: https://github.com/devsapp/start-fc
Tags:
Expand Down
110 changes: 0 additions & 110 deletions custom-function/golang/fc-custom-golang-event/readme.md

This file was deleted.

1 change: 1 addition & 0 deletions custom-function/golang/fc-custom-golang-event/readme.md
28 changes: 27 additions & 1 deletion custom-function/golang/fc-custom-golang-event/src/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
68 changes: 68 additions & 0 deletions custom-function/golang/fc-custom-golang-http/cloudshell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 快速体验 Golang Http 函数案例

欢迎您使用Serverless Devs开发者工具进行项目开发,本实验是基于Serverless Devs部署 Golang Http 案例到阿里云函数计算。

整个实验过程包括:
- [快速体验 Golang Http 函数案例](#快速体验-golang-http-函数案例)
- [下载工具](#下载工具)
- [配置密钥](#配置密钥)
- [初始化项目](#初始化项目)
- [部署项目](#部署项目)
- [更多信息](#更多信息)

> - [:octocat: 源代码](https://github.com/devsapp/start-fc/tree/main/custom-function/golang/fc-custom-golang-http/src)
## 下载工具

> 由于本系统已经默认集成了Serverless Devs,所以该步骤在本次试验中可以跳过
通过`npm`安装Serverless Devs开发者工具:

```
npm install -g @serverless-devs/s
```

除了上述的安装方法之外,您还可以参考[Serverless Devs Cli 安装文档](https://www.serverless-devs.com/serverless-devs/install) 查看更多安装方法。

## 配置密钥

> 由于本系统已经配置了密钥信息,所以该步骤在本次试验中可以跳过
配置阿里云账号的 AccessKeyID, AccessKeySecret 以及密钥别名。

配置方法可以通过`s config add`指令,选择`Alibaba Cloud`并根据提示进行配置。

除了上述的配置方法之外,您还可以参考[Serverless Devs 配置阿里云密钥信息](https://www.serverless-devs.com/fc/config) 查看更多密钥配置方法。

## 初始化项目

进行项目初始化:

```
s init fc-custom-golang-http -d fc-custom-golang-http
```

在初始化的过程中,可能涉及到部分服务的开通、参数的获取以及参数的定义,请根据命令行的提醒进行具体的操作。

## 部署项目

- 进入项目:`cd fc-custom-golang-http`
- 进行项目的部署:`s deploy`

稍等片刻,即可完成项目的部署。

## 更多信息

- 组件仓库地址:https://github.com/devsapp/fc
- 组件帮助文档:https://www.serverless-devs.com/fc/readme
- Yaml参考文档:https://www.serverless-devs.com/fc/yaml/readme
- 关于:
- Serverless Devs和FC组件的关系、如何声明/部署多个函数、超过50M的代码包如何部署
- 关于.fcignore使用方法、工具中.s目录是做什么、函数进行build操作之后如何处理build的产物
等问题,可以参考文档:https://www.serverless-devs.com/fc/tips
- 关于如何做CICD等问题,可以参考:https://www.serverless-devs.com/serverless-devs/cicd
- 关于如何进行环境划分等问题,可以参考:https://www.serverless-devs.com/serverless-devs/extend

更多函数计算案例,可参考:https://github.com/devsapp/awesome/

> 有问题快来钉钉群问一下吧:33947367
23 changes: 23 additions & 0 deletions custom-function/golang/fc-custom-golang-http/hook/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
async function preInit(inputObj) {
console.log(`
Serverless Devs Application Case
Cloud services required:
- FC : https://fc.console.aliyun.com/
Tips:
- FC Component: https://www.serverless-devs.com/fc/readme`)
}

async function postInit(inputObj) {
console.log(`
* Before using, please check whether the actions command in Yaml file is available
* Carefully reading the notes in s.yaml is helpful for the use of the tool
* If need help in the use process, please apply to join the Dingtalk Group: 33947367
`)
}

module.exports = {
postInit,
preInit
}
60 changes: 60 additions & 0 deletions custom-function/golang/fc-custom-golang-http/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Type: Application
Name: fc-custom-golang-http
Provider:
- 阿里云
Version: 0.0.1
Description: 快速部署一个基于custom runtime 的 Golang Http 类型的 Hello World 到阿里云函数计算
HomePage: https://github.com/devsapp/start-fc
Tags:
- 函数计算
- 你好世界
- 新手入门
Category: 新手入门
Service:
函数计算:
Authorities:
- AliyunFCFullAccess
Runtime: Custom
Parameters:
type: object
additionalProperties: false # 不允许增加其他属性
required: # 必填项
- region
- serviceName
- functionName
properties:
region:
title: 地域
type: string
default: cn-hangzhou
description: 创建应用所在的地区
enum:
- cn-beijing
- cn-hangzhou
- cn-shanghai
- cn-qingdao
- cn-zhangjiakou
- cn-huhehaote
- cn-shenzhen
- cn-chengdu
- cn-hongkong
- ap-southeast-1
- ap-southeast-2
- ap-southeast-3
- ap-southeast-5
- ap-northeast-1
- eu-central-1
- eu-west-1
- us-west-1
- us-east-1
- ap-south-1
serviceName:
title: 服务名
type: string
default: hello-world-service
description: 服务名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-128 之间
functionName:
title: 函数名
type: string
default: fc-custom-golang-http
description: 函数名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-64 之间
1 change: 1 addition & 0 deletions custom-function/golang/fc-custom-golang-http/readme.md
3 changes: 3 additions & 0 deletions custom-function/golang/fc-custom-golang-http/src/code/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module example.com/m

go 1.18
49 changes: 49 additions & 0 deletions custom-function/golang/fc-custom-golang-http/src/code/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
)

const (
fcRequestID = "x-fc-request-id"
fcLogTailStartPrefix = "FC Invoke Start RequestId: %s" // Start of log tail mark
fcLogTailEndPrefix = "FC Invoke End RequestId: %s" // End of log tail mark
)

func aHandler(w http.ResponseWriter, req *http.Request) {
requestID := req.Header.Get(fcRequestID)
fmt.Println(fmt.Sprintf(fcLogTailStartPrefix, requestID))

defer func() {
fmt.Println(fmt.Sprintf(fcLogTailEndPrefix, requestID))
}()

// your logic
w.Write([]byte(fmt.Sprintf("Hello, golang http invoke!")))
}

func bHandler(w http.ResponseWriter, req *http.Request) {
requestID := req.Header.Get(fcRequestID)
fmt.Println(fmt.Sprintf(fcLogTailStartPrefix, requestID))

defer func() {
fmt.Println(fmt.Sprintf(fcLogTailEndPrefix, requestID))
}()

// your logic
b, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err)
}
info := fmt.Sprintf("method = %+v;\nheaders = %+v;\nbody = %+v", req.Method, req.Header, string(b))
w.Write([]byte(fmt.Sprintf("Hello, golang http invoke! detail:\n %s", info)))
}

func main() {
fmt.Println("FunctionCompute custom go runtime inited.")
http.HandleFunc("/a", aHandler)
http.HandleFunc("/b", bHandler)
http.ListenAndServe(":9000", nil)
}
Loading

0 comments on commit f4ac84e

Please sign in to comment.