-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from devsapp/add-custom-go-http
add custom go http example
- Loading branch information
Showing
14 changed files
with
523 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 0 additions & 110 deletions
110
custom-function/golang/fc-custom-golang-event/readme.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/readme.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
custom-function/golang/fc-custom-golang-http/cloudshell.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
custom-function/golang/fc-custom-golang-http/hook/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 之间 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/readme.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
custom-function/golang/fc-custom-golang-http/src/code/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.