-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support http and image type scf (#65)
* feat: support http and image type scf * test: fix example dir * chore: update example * chore: fix test
- Loading branch information
Showing
30 changed files
with
703 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ build/ | |
env.js | ||
package-lock.json | ||
test | ||
yarn.lock | ||
yarn.lock | ||
|
||
!.env.example |
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
Large diffs are not rendered by default.
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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
exports.main_handler = async (event, context) => { | ||
console.log(event) | ||
return { | ||
msg: 'Hello Serverless' | ||
msg: 'Hello Serverless', | ||
event | ||
} | ||
} |
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,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 |
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 @@ | ||
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.
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 @@ | ||
#!/usr/bin/env bash | ||
|
||
/var/lang/node12/bin/node 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,14 @@ | ||
# 注意:请根据账号信息修改以下配置 | ||
|
||
# 实例名称,企业版需要 | ||
registry_name="serverless" | ||
|
||
# 命名空间 | ||
namespace="sls-scf" | ||
# 镜像名称 | ||
image_name="nodejs_test" | ||
# 镜像版本 | ||
tag_name="latest" | ||
|
||
# 镜像登录密码 | ||
password="xxx" |
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,9 @@ | ||
FROM node:12.16 | ||
|
||
WORKDIR /usr/src/app | ||
COPY ./src . | ||
RUN npm install | ||
|
||
EXPOSE 9000 | ||
|
||
CMD [ "node", "./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,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 | ||
``` |
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,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 |
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,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 |
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,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`) | ||
}) |
Oops, something went wrong.