Skip to content

Commit

Permalink
feat: support http and image type scf (#65)
Browse files Browse the repository at this point in the history
* feat: support http and image type scf

* test: fix example dir

* chore: update example

* chore: fix test
  • Loading branch information
yugasun authored Jun 15, 2021
1 parent bbe94b9 commit 61864c6
Show file tree
Hide file tree
Showing 30 changed files with 703 additions and 94 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ build/
env.js
package-lock.json
test
yarn.lock
yarn.lock

!.env.example
6 changes: 5 additions & 1 deletion __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Scf', () => {
}
]

const codeSrc = path.join(__dirname, '../example/src')
const codeSrc = path.join(__dirname, '../example/event-scf/src')
const inputs = {
name: `scf-integration-tests-${generateId()}`,
src: {
Expand Down Expand Up @@ -92,6 +92,7 @@ describe('Scf', () => {
subDomain: expect.stringContaining('.gz.apigw.tencentcs.com'),
protocols: 'http&https',
environment: 'test',
url: expect.stringContaining('http'),
apiList: [
{
created: expect.any(Boolean),
Expand All @@ -102,6 +103,7 @@ describe('Scf', () => {
authType: 'NONE',
businessType: 'NORMAL',
internalDomain: expect.any(String),
url: expect.stringContaining('http'),
isBase64Encoded: false
}
],
Expand Down Expand Up @@ -150,6 +152,7 @@ describe('Scf', () => {
protocols: 'http&https',
environment: 'release',
environment: 'test',
url: expect.stringContaining('http'),
apiList: [
{
created: expect.any(Boolean),
Expand All @@ -160,6 +163,7 @@ describe('Scf', () => {
authType: 'NONE',
businessType: 'NORMAL',
internalDomain: expect.any(String),
url: expect.stringContaining('http'),
isBase64Encoded: false
}
],
Expand Down
94 changes: 52 additions & 42 deletions docs/configure.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions example/serverless.yml → example/event-scf/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
component: scf
name: scfdemo
name: event

inputs:
src:
src: ./src
exclude:
- .env
# 此处注释部分将自动使用默认值
# type: event
# region: ap-guangzhou
# runtime: Nodejs10.15
# handler: index.main_handler
# runtime: Nodejs12.16
name: event-function
handler: index.main_handler
events:
- apigw:
parameters:
Expand All @@ -19,4 +20,4 @@ inputs:
environment: release
endpoints:
- path: /
method: GET
method: ANY
4 changes: 2 additions & 2 deletions example/src/index.js → example/event-scf/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports.main_handler = async (event, context) => {
console.log(event)
return {
msg: 'Hello Serverless'
msg: 'Hello Serverless',
event
}
}
27 changes: 27 additions & 0 deletions example/http-scf/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
component: scf
name: http

inputs:
src:
src: ./src
exclude:
- .env
# 指定 SCF 类型为 Web 类型
type: web
name: web-function
region: ap-chengdu
handler: scf_bootstrap
runtime: Nodejs12.16
events:
- apigw:
parameters:
protocols:
- http
- https
environment: release
endpoints:
- path: /
method: ANY
function:
# 指定 API 类型为 Web 类型
type: web
60 changes: 60 additions & 0 deletions example/http-scf/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const express = require('express')
const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.get(`/`, (req, res) => {
res.send({
message: 'Hello Serverless'
})
})

app.get(`/headers`, (req, res) => {
res.send({
headers: req.headers
})
})

app.post('/body', (req, res) => {
res.send({
body: req.body
})
})

app.get('/user', (req, res) => {
res.send([
{
title: 'Tencent Serverless',
link: 'https://console.cloud.tencent.com/scf'
}
])
})

app.get('/user/:id', (req, res) => {
const id = req.params.id
res.send({
id: id,
title: 'Tencent Serverless',
link: 'https://console.cloud.tencent.com/scf'
})
})

app.get('/404', (req, res) => {
res.status(404).send('Not found')
})

app.get('/500', (req, res) => {
res.status(500).send('Server Error')
})

// Error handler
app.use(function(err, req, res, next) {
console.error(err)
res.status(500).send('Internal Serverless Error')
})

// Web 类型云函数,只能监听地址 0.0.0.0 和 端口 9000
app.listen(9000, '0.0.0.0', () => {
console.log(`Server start on http://0.0.0.0:9000`)
})
File renamed without changes.
3 changes: 3 additions & 0 deletions example/http-scf/src/scf_bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

/var/lang/node12/bin/node index.js
14 changes: 14 additions & 0 deletions example/image-event-scf/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 注意:请根据账号信息修改以下配置

# 实例名称,企业版需要
registry_name="serverless"

# 命名空间
namespace="sls-scf"
# 镜像名称
image_name="nodejs_test"
# 镜像版本
tag_name="latest"

# 镜像登录密码
password="xxx"
9 changes: 9 additions & 0 deletions example/image-event-scf/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:12.16

WORKDIR /usr/src/app
COPY ./src .
RUN npm install

EXPOSE 9000

CMD [ "node", "./index.js" ]
42 changes: 42 additions & 0 deletions example/image-event-scf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 腾讯云自定义镜像实例

在部署前,需要先构建镜像,并推送到云端镜像仓库。

首先,复制 `account.example.conf``account.conf`,并修改其中配置为开发者账号:

```bash
# 主账号 UIN
UIN=123455555

# 实例名称,企业版需要
registry_name="serverless"

# 命名空间
namespace="sls-scf"
# 镜像名称
image_name="nodejs_test_event"
# 镜像版本
tag_name="latest"

# 镜像登录密码
password="xxx"
```

然后,构建镜像:

```bash
# 构建个人版
$ ./build.sh
```

或者构建企业版:

```bash
$ ./build.sh enterprise
```

然后执行 Serverless 部署:

```
$ sls deploy
```
62 changes: 62 additions & 0 deletions example/image-event-scf/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

set -e

# 注入配置参数
source ./.env

# 构建的类型,默认构建个人版,可以指定为 enterprise 来构建企业版
# 比如: ./build.sh enterprise
type=$1

# 镜像构件函数
build() {
server_url=$1
image_url=$1/$2/$3
# 1. 登录,需要输入登录密钥
docker login "$server_url" --username=$UIN --password "$password"
# 2. 构建
echo "正在构件镜像..."
docker build -t "$image_url:$tag_name" .
# 3. 推送
echo "正在同步镜像..."
docker push "$image_url:$tag_name"
}

#####################
# 构建个人版镜像
#####################
buildPersonal() {
# 镜像服务域名
server_url="ccr.ccs.tencentyun.com"

# 开始构件
build $server_url $namespace $image_name
}
#####################
# end
#####################

#####################
# 构建企业版镜像
#####################
buildEnterprise() {
# 镜像服务域名
server_url="$registry_name.tencentcloudcr.com"

# 开始构件
build $server_url $namespace $image_name
}
#####################
# end
#####################


if [[ "$type" != "" ]] && [[ "$type" == "enterprise" ]]
then
echo "正在构建企业版镜像..."
buildEnterprise
else
echo "正在构建个人版镜像..."
buildPersonal
fi
21 changes: 21 additions & 0 deletions example/image-event-scf/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
component: scf
name: imageeventdemo

inputs:
name: event-function-image
region: ap-chengdu
image:
registryName: ${env:registry_name}
namespace: ${env:namespace}
repositoryName: ${env:image_name}
tagName: ${env:tag_name}
events:
- apigw:
parameters:
protocols:
- http
- https
environment: release
endpoints:
- path: /
method: ANY
62 changes: 62 additions & 0 deletions example/image-event-scf/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const express = require('express')
const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.get(`/`, (req, res) => {
res.send({
message: 'Hello Serverless'
})
})

app.get(`/headers`, (req, res) => {
res.send({
headers: req.headers
})
})

// 获取 event
app.post('/event-invoke', (req, res) => {
res.send({
headers: req.headers,
body: req.body
})
})

app.get('/user', (req, res) => {
res.send([
{
title: 'Tencent Serverless',
link: 'https://console.cloud.tencent.com/scf'
}
])
})

app.get('/user/:id', (req, res) => {
const id = req.params.id
res.send({
id: id,
title: 'Tencent Serverless',
link: 'https://console.cloud.tencent.com/scf'
})
})

app.get('/404', (req, res) => {
res.status(404).send('Not found')
})

app.get('/500', (req, res) => {
res.status(500).send('Server Error')
})

// Error handler
app.use(function(err, req, res, next) {
console.error(err)
res.status(500).send('Internal Serverless Error')
})

// Web 类型云函数,只能监听地址 0.0.0.0 和 端口 9000
app.listen(9000, '0.0.0.0', () => {
console.log(`Server start on http://0.0.0.0:9000`)
})
Loading

0 comments on commit 61864c6

Please sign in to comment.