Skip to content

Commit 2134f9b

Browse files
author
定戎
committed
add golang, nodejs14, and python3.9 custom container demo that support websocket
1 parent 4080730 commit 2134f9b

File tree

30 files changed

+1547
-4
lines changed

30 files changed

+1547
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
async function preInit(inputObj) {
2+
console.log(`\n ____ _ _ ___ _ _ _ _____ ____
3+
/ _ \\/ \\ / \\\\ \\/// \\ /\\/ \\ /| / // _\\
4+
| / \\|| | | | \\ / | | ||| |\\ || | __\\| /
5+
| |-||| |_/\\| | / / | \\_/|| | \\|| | | | \\__
6+
\\_/ \\|\\____/\\_//_/ \\____/\\_/ \\| \\_/ \\____/`)
7+
}
8+
9+
async function postInit(inputObj) {
10+
console.log(`\n Welcome to the Aliyun FC start application
11+
This application requires to open these services:
12+
FC : https://fc.console.aliyun.com/
13+
ACR: https://cr.console.aliyun.com/
14+
15+
* 额外说明:s.yaml中声明了actions:
16+
部署前执行:s build --use-docker --dockerfile ./code/Dockerfile
17+
如果不需要每次都构建项目,或者部署前不需要构建,或者已经手动构建了,可以注释掉这部分内容
18+
> PS:部署的时候还需要修改s.yaml中image字段为自己的acr配置的地址
19+
* 项目初始化完成,您可以直接进入项目目录下,并使用 s deploy 进行项目部署
20+
\n`)
21+
}
22+
23+
module.exports = {
24+
postInit,
25+
preInit
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Type: Application
2+
Name: start-fc-custom-container-websocket-golang
3+
Provider:
4+
- 阿里云
5+
Version: 0.0.1
6+
Description: 快速部署一个基于custom container runtime 的 Golang Wensocket 类型的 Hello World 到阿里云函数计算
7+
HomePage: https://github.com/devsapp/start-fc
8+
Tags:
9+
- 函数计算
10+
- 你好世界
11+
- 新手入门
12+
Category: 新手入门
13+
Service:
14+
函数计算:
15+
Authorities:
16+
- 创建函数
17+
- 删除函数
18+
- 创建服务
19+
- 删除服务
20+
Runtime: custom container
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 阿里云函数计算 Custom Container Golang WebSocket 函数案例
2+
3+
只需几步就可以快速在阿里云函数计算服务上体验 Golang:
4+
5+
- 初始化项目:`s init start-fc-custom-container-websocket-golang -d start-cc-golang`
6+
- 进入项目:`cd start-cc-golang`
7+
- 修改 s.yaml 中 Image 为自己的 ACR 镜像地址
8+
- 部署项目:`s deploy -y`
9+
- 调用函数:`# 在浏览器或者 Postman 中进行调用`
10+
11+
即可实现`Custom Container Golang`案例的初始化、部署整个流程。
12+
13+
> 本应用仓库地址:https://github.com/devsapp/start-fc
14+
15+
------------------------------------
16+
> # More
17+
> 欢迎您使用阿里云函数计算 FC 组件进行项目开发
18+
> 组件仓库地址/帮助文档:https://github.com/devsapp/fc
19+
> Yaml参考文档:https://github.com/devsapp/fc/blob/main/docs/zh/yaml.md
20+
> 快速入门:
21+
> - 快速创建应用:https://github.com/devsapp/fc/blob/main/docs/zh/quick_start_application.md
22+
> - 快速使用命令:https://github.com/devsapp/fc/blob/main/docs/zh/quick_start_function.md
23+
------------------------------------
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Alibaba Cloud Function Computing Custom Container Golang WebSocket function
2+
3+
You can quickly experience one-click deployment of a Golang application on Alibaba Cloud Function Computing Service in just a few steps:
4+
5+
- Initialize the project: `s init start-fc-custom-container-websocket-golang -d start-cc-golang`
6+
- Enter the project: `cd start-cc-golang`
7+
- Modify Image in s.yaml to be your own ACR mirror address
8+
- Deployment project: `s deploy -y`
9+
- Invoke function: `# invoke by browser or postman`
10+
11+
> This application warehouse address: https://github.com/devsapp/start-fc
12+
13+
------------------------------------
14+
> # More
15+
> Welcome to use Alibaba Cloud Function Compute FC component for project development
16+
>
17+
> Component warehouse address/help document: https://github.com/devsapp/fc
18+
>
19+
> Yaml reference documentation: https://github.com/devsapp/fc/blob/main/docs/zh/yaml.md
20+
>
21+
> Quick start:
22+
> - Quickly create an application: https://github.com/devsapp/fc/blob/main/docs/zh/quick_start_application.md
23+
> - Quick use of commands: https://github.com/devsapp/fc/blob/main/docs/zh/quick_start_function.md
24+
------------------------------------
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 阿里云函数计算 Custom Container Golang WebSocket 函数案例
2+
3+
只需几步就可以快速在阿里云函数计算服务上体验 Golang:
4+
5+
- 初始化项目:`s init start-fc-custom-container-websocket-golang -d start-cc-websocket-golang`
6+
- 进入项目:`cd start-cc-golang`
7+
- 修改 s.yaml 中 Image 为自己的 ACR 镜像地址
8+
- 部署项目:`s deploy -y`
9+
- 调用函数:`# 在浏览器或者 Postman 中进行调用`
10+
11+
即可实现`Custom Container golang`案例的初始化、部署整个流程。
12+
13+
> 本应用仓库地址:https://github.com/devsapp/start-fc
14+
15+
------------------------------------
16+
> # More
17+
> 欢迎您使用阿里云函数计算 FC 组件进行项目开发
18+
> 组件仓库地址/帮助文档:https://github.com/devsapp/fc
19+
> Yaml参考文档:https://github.com/devsapp/fc/blob/main/docs/zh/yaml.md
20+
> 快速入门:
21+
> - 快速创建应用:https://github.com/devsapp/fc/blob/main/docs/zh/quick_start_application.md
22+
> - 快速使用命令:https://github.com/devsapp/fc/blob/main/docs/zh/quick_start_function.md
23+
------------------------------------
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.12.16-stretch
2+
RUN mkdir -p $GOPATH/src/golang.org/x/
3+
RUN cd $GOPATH/src/golang.org/x/ && git clone https://github.com/golang/net.git
4+
RUN cd $GOPATH/src/golang.org/x/ && git clone https://github.com/golang/sys.git
5+
RUN go get github.com/awesome-fc/golang-runtime
6+
RUN go get github.com/gorilla/websocket
7+
8+
WORKDIR /tmp
9+
COPY . .
10+
11+
RUN go build -o bootstrap main.go
12+
RUN mkdir -p /usr/src/app
13+
WORKDIR /usr/src/app
14+
RUN cp /tmp/bootstrap ./bootstrap
15+
ENTRYPOINT ["/usr/src/app/bootstrap"]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gorilla/websocket"
7+
)
8+
9+
var upgrader = websocket.Upgrader{}
10+
11+
func echo(w http.ResponseWriter, r *http.Request) {
12+
c, err := upgrader.Upgrade(w, r, nil)
13+
if err != nil {
14+
return
15+
}
16+
defer c.Close()
17+
for {
18+
mt, message, err := c.ReadMessage()
19+
if err != nil {
20+
break
21+
}
22+
err = c.WriteMessage(mt, message)
23+
if err != nil {
24+
break
25+
}
26+
}
27+
}
28+
29+
func main() {
30+
http.HandleFunc("/", echo)
31+
http.ListenAndServe(":9000", nil)
32+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# ------------------------------------
2+
# 欢迎您使用阿里云函数计算 FC 组件进行项目开发
3+
# 组件仓库地址/帮助文档:https://github.com/devsapp/fc
4+
# Yaml参考文档:https://github.com/devsapp/fc/blob/jiangyu-docs/docs/zh/yaml.md
5+
# 关于:
6+
# - Serverless Devs和FC组件的关系、如何声明/部署多个函数、超过50M的代码包如何部署
7+
# - 关于.fcignore使用方法、工具中.s目录是做什么、函数进行build操作之后如何处理build的产物
8+
# 等问题,可以参考文档:https://github.com/devsapp/fc/blob/jiangyu-docs/docs/zh/tips.md
9+
# 关于如何做CICD等问题,可以参考:https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/cicd.md
10+
# 有问题快来钉钉群问一下吧:33947367
11+
# ------------------------------------
12+
edition: 1.0.0
13+
name: hello-world-app
14+
access: "{{ access }}"
15+
16+
vars: # 全局变量
17+
region: cn-shanghai
18+
service:
19+
name: hello-world-service
20+
description: 'hello world by serverless devs'
21+
22+
services:
23+
helloworld: # 业务名称/模块名称
24+
component: fc
25+
actions: # 自定义执行逻辑
26+
pre-deploy: # 在deploy之前运行
27+
- run: s build --use-docker --dockerfile ./code/Dockerfile # 要运行的命令行
28+
path: ./ # 命令行运行的路径
29+
# post-deploy: # 在deploy之后运行
30+
# - run: s versions # 要运行的命令行
31+
# path: ./ # 命令行运行的路径
32+
props:
33+
region: ${vars.region}
34+
service: ${vars.service}
35+
function:
36+
name: go-websocket-function
37+
description: 'hello world by serverless devs'
38+
timeout: 60
39+
caPort: 9000
40+
memorySize: 256
41+
runtime: custom-container
42+
customContainerConfig:
43+
#instanceID: cri-v1akeviqpik09jgs 企业实例需要提供
44+
image: registry.cn-shanghai.aliyuncs.com/fc-demo2/custom_container:v3 # 需要换成自己的registry地址
45+
codeUri: ./code
46+
triggers:
47+
- name: http_t
48+
type: http
49+
config:
50+
authType: anonymous
51+
methods:
52+
- GET
53+
# next-function: # 第二个函数的案例,仅供参考
54+
# # 如果在当前项目下执行 s deploy,会同时部署模块:
55+
# # helloworld:服务hello-world-service,函数cpp-event-function
56+
# # next-function:服务hello-world-service,函数next-function-example
57+
# # 如果想单独部署当前服务与函数,可以执行 s + 模块名/业务名 + deploy,例如:s next-function deploy
58+
# # 如果想单独部署当前函数,可以执行 s + 模块名/业务名 + deploy function,例如:s next-function deploy function
59+
# # 更多命令可参考:https://github.com/devsapp/fc/tree/jiangyu-docs/docs#文档相关
60+
# component: fc
61+
# props:
62+
# region: ${vars.region}
63+
# service: ${vars.service} # 应用整体的服务配置
64+
# function: # 定义一个新的函数
65+
# name: next-function-example
66+
# description: 'hello world by serverless devs'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- 兼容新版本
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
async function preInit(inputObj) {
2+
console.log(`\n ____ _ _ ___ _ _ _ _____ ____
3+
/ _ \\/ \\ / \\\\ \\/// \\ /\\/ \\ /| / // _\\
4+
| / \\|| | | | \\ / | | ||| |\\ || | __\\| /
5+
| |-||| |_/\\| | / / | \\_/|| | \\|| | | | \\__
6+
\\_/ \\|\\____/\\_//_/ \\____/\\_/ \\| \\_/ \\____/`)
7+
}
8+
9+
async function postInit(inputObj) {
10+
console.log(`\n Welcome to the Aliyun FC start application
11+
This application requires to open these services:
12+
FC : https://fc.console.aliyun.com/
13+
ACR: https://cr.console.aliyun.com/
14+
15+
* 额外说明:s.yaml中声明了actions:
16+
部署前执行:s build --use-docker --dockerfile ./code/Dockerfile
17+
如果不需要每次都构建项目,或者部署前不需要构建,或者已经手动构建了,可以注释掉这部分内容
18+
> PS:部署的时候还需要修改s.yaml中image字段为自己的acr配置的地址
19+
* 项目初始化完成,您可以直接进入项目目录下,并使用 s deploy 进行项目部署
20+
\n`)
21+
}
22+
23+
module.exports = {
24+
postInit,
25+
preInit
26+
}

0 commit comments

Comments
 (0)