Skip to content

Commit d18d9b7

Browse files
authored
feat: support apigw base64 encode (#46)
* feat: support apigw base64 encode * docs: update upload.md * test: fix
1 parent 9814c9a commit d18d9b7

File tree

12 files changed

+94
-150
lines changed

12 files changed

+94
-150
lines changed

README.en.md

Lines changed: 0 additions & 122 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
# 腾讯云 Koa 组件
44

5-
简体中文 | [English](https://github.com/serverless-components/tencent-koa/tree/master/README.en.md)
6-
75
## 简介
86

97
使用腾讯云 Koa 组件,可快速的在腾讯云创建,配置和管理一个 [Koa 框架](https://koajs.com/) 服务。
@@ -152,9 +150,9 @@ module.exports = app
152150

153151
这样应用部署到云函数后,在函数服务逻辑执行前,会先执行 `slsInitialize()` 函数,来初始化数据库连接。
154152

155-
### 还支持哪些组件?
153+
## 文件上传
156154

157-
可以在 [Serverless Components](https://github.com/serverless/components) repo 中查询更多组件的信息。
155+
[文件上传教程](https://github.com/serverless-components/tencent-koa/tree/master/docs/upload.md)
158156

159157
## License
160158

__tests__/index.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const credentials = {
2626
// get serverless construct sdk
2727
const sdk = getServerlessSdk(instanceYaml.org)
2828

29-
it('should successfully deploy koa app', async () => {
29+
it('deploy koa app by template', async () => {
3030
const instance = await sdk.deploy(instanceYaml, credentials)
3131
expect(instance).toBeDefined()
3232
expect(instance.instanceName).toEqual(instanceYaml.name)
@@ -39,7 +39,7 @@ it('should successfully deploy koa app', async () => {
3939
expect(instance.outputs.apigw.environment).toEqual(instanceYaml.inputs.apigatewayConf.environment)
4040
})
4141

42-
it('should successfully update source code', async () => {
42+
it('deploy with src code', async () => {
4343
const srcPath = path.join(__dirname, '..', 'example')
4444
execSync('npm install', { cwd: srcPath })
4545
instanceYaml.inputs.src = srcPath
@@ -51,7 +51,7 @@ it('should successfully update source code', async () => {
5151
expect(instance.outputs.templateUrl).not.toBeDefined()
5252
})
5353

54-
it('should successfully remove koa app', async () => {
54+
it('remove koa app', async () => {
5555
await sdk.remove(instanceYaml, credentials)
5656
result = await sdk.getInstance(instanceYaml.org, instanceYaml.stage, instanceYaml.app, instanceYaml.name)
5757

docs/output.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
>
55
> 例如,如果该组件名称是 `test_name`, ·且只部署于一个地域,则可以通过 `${output:${stage}:${app}:test_name.apigw.url}` 在别的组件中获取该组件的 API 网关的 `url`
66
7-
| 名称 | 类型 | 描述 |
8-
| :---------- | :-------------: | :----------------------------------------------------- | ---------------- |
9-
| templateUrl | string | 未提供代码时的模板代码 url |
10-
| region | string | 地域信息(只有一个地域时才提供) |
11-
| scf | [`FunctionOutput | Record<string,FunctionOutput>`](#云函数输出-`FunctionOutput`) | 云函数输出信息 |
12-
| apigw | [`ApigwOutput | Record<string:ApigwOutput>`](#API-网关输出-`ApigwOutput`) | API 网关输出信息 |
7+
| 名称 | 类型 | 描述 |
8+
| :---------- | :------------------------------------------------------------------------------: | :------------------------------- |
9+
| templateUrl | string | 未提供代码时的模板代码 url |
10+
| region | string | 地域信息(只有一个地域时才提供) |
11+
| scf | [`FunctionOutput | Record<string,FunctionOutput>`](#云函数输出-`FunctionOutput`) | 云函数输出信息 |
12+
| apigw | [`ApigwOutput | Record<string:ApigwOutput>`](#API-网关输出-`ApigwOutput`) | API 网关输出信息 |
1313

1414
## 云函数输出 `FunctionOutput`
1515

@@ -25,30 +25,27 @@
2525
## API 网关输出 `ApigwOutput`
2626

2727
| 名称 | 类型 | 描述 |
28-
| :------------ | :------------------------------------------------------------------: | :------------------------- | ------- | -------- |
28+
| :------------ | :------------------------------------------------------------------: | :------------------------- |
2929
| serviceId | string | API 网关 ID |
3030
| subDomain | string | API 网关子域名 |
31-
| enviroment | `"release" | "prepub" | "test"` | API 网关 |
31+
| enviroment | `"release" | "prepub" | "test"` | API 网关 |
3232
| url | string | API 网关对外的完整 URL |
3333
| traffic | number (0~1) | 将多少流量导向该云函数 |
3434
| customDomains | [CustomDomain[]](#API-网关自定义域名输出-`ApigwOutput.CustomDomain`) | API 网关自定义域名输出列表 |
3535

3636
## API 网关自定义域名输出 `ApigwOutput.CustomDomain`
3737

3838
| 名称 | 类型 | 描述 |
39-
| :--------------- | :----------------------------------------------------------------: | :------------------------- | ---------- |
39+
| :--------------- | :----------------------------------------------------------------: | :------------------------- |
4040
| domain | string | 自定义域名 |
4141
| certificateId | string | 域名证书 ID |
4242
| isDefaultMapping | boolean | 该自定义域名是否为默认域名 |
4343
| pathMappingSet | [PathMapping[]](#-API-网关域名映射规则-`CustomDomain.PathMapping`) | 该域名的路径映射规则列表 |
44-
| protocols | `"http" | "https"` | 启用的协议 |
45-
46-
44+
| protocols | `"http" | "https"` | 启用的协议 |
4745

4846
## API 网关域名映射规则 `CustomDomain.PathMapping`
4947

5048
| 名称 | 类型 | 描述 |
5149
| :--------- | :----: | :--------------- |
5250
| path | string | 路径 |
5351
| enviroment | string | 路径映射到的环境 |
54-

docs/upload.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## 文件上传说明
2+
3+
项目中如果涉及到文件上传,需要依赖 API 网关提供的 [Base64 编码能力](https://cloud.tencent.com/document/product/628/51799),使用时只需要 `serverless.yml` 中配置 `isBase64Encoded``true`,如下:
4+
5+
```yaml
6+
app: appDemo
7+
stage: dev
8+
component: koa
9+
name: koaDemo
10+
11+
inputs:
12+
# 省略...
13+
apigatewayConf:
14+
isBase64Encoded: true
15+
# 省略...
16+
# 省略...
17+
```
18+
19+
当前 API 网关支持上传最大文件大小为 `2M`,如果文件过大,请修改为前端直传对象存储方案。
20+
21+
## Base64 示例
22+
23+
此 Github 项目的 `example` 目录下存在模板文件:
24+
25+
- [sls.upload.js](../example/sls.upload.js)
26+
27+
开发者可根据个人项目需要参考修改,使用时需要复制文件名为 `sls.js`
28+
29+
文件中实现了文件上传接口 `POST /upload`,如果要支持文件上传,需要安装 `@koajs/multer``multer` 包。
30+
31+
同时需要在 `serverless.yml``apigatewayConf` 中配置 `isBase64Encoded``true`

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"author": "yugasun",
1010
"license": "MIT",
1111
"dependencies": {
12-
"koa": "^2.11.0",
13-
"koa-router": "^8.0.8",
12+
"@koa/router": "^10.0.0",
13+
"koa": "^2.13.1",
1414
"koa-sendfile": "^2.0.1"
1515
}
1616
}

example/serverless.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
org: orgDemo
21
app: appDemo
32
stage: dev
43
component: koa

example/sls.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
const Koa = require('koa')
2-
const KoaRouter = require('koa-router')
2+
const KoaRouter = require('@koa/router')
33
const sendFile = require('koa-sendfile')
44
const path = require('path')
55

66
const app = new Koa()
77
const router = new KoaRouter()
8+
const isServerless = process.env.SERVERLESS
9+
const PORT = 3000
810

911
// Routes
10-
router.get(`/*`, async (ctx) => {
12+
router.get(`/`, async (ctx) => {
1113
await sendFile(ctx, path.join(__dirname, 'index.html'))
1214
})
1315

1416
app.use(router.allowedMethods()).use(router.routes())
1517

1618
// don't forget to export!
17-
module.exports = app
19+
if (isServerless) {
20+
module.exports = app
21+
} else {
22+
app.listen(PORT, () => {
23+
console.log(`Server start on http://localhost:${PORT}`)
24+
})
25+
}

example/sls.upload.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Koa = require('koa')
2+
const KoaRouter = require('@koa/router')
3+
const multer = require('@koa/multer')
4+
const sendFile = require('koa-sendfile')
5+
const path = require('path')
6+
7+
const isServerless = process.env.SERVERLESS
8+
const app = new Koa()
9+
const router = new KoaRouter()
10+
const upload = multer({ dest: isServerless ? '/tmp/upload' : './upload' })
11+
12+
router.get(`/`, async (ctx) => {
13+
await sendFile(ctx, path.join(__dirname, 'index.html'))
14+
})
15+
router.post('/upload', upload.single('file'), (ctx) => {
16+
ctx.body = {
17+
success: true,
18+
data: ctx.file
19+
}
20+
})
21+
22+
app.use(router.routes()).use(router.allowedMethods())
23+
24+
if (isServerless) {
25+
module.exports = app
26+
} else {
27+
app.listen(3000, () => {
28+
console.log(`Server start on http://localhost:3000`)
29+
})
30+
}

serverless.component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: koa
2-
version: 0.3.2
2+
version: 0.4.0
33
author: 'Tencent Cloud, Inc.'
44
org: 'Tencent Cloud, Inc.'
55
description: Deploy a serverless Koa.js application onto Tencent SCF and API Gateway.

0 commit comments

Comments
 (0)