Skip to content

Commit 5cde11e

Browse files
authored
Merge pull request #2 from serverless-tencent/mutil-region
增加多地域部署能力
2 parents c76547d + ade5d8c commit 5cde11e

File tree

2 files changed

+36
-110
lines changed

2 files changed

+36
-110
lines changed

package.json

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@serverless/tencent-werobot",
33
"description": "Tencent Cloud Python Werobot Serverless Component",
4-
"version": "0.0.3",
4+
"version": "1.0.0",
55
"main": "serverless.js",
66
"publishConfig": {
77
"access": "public"
@@ -24,12 +24,6 @@
2424
},
2525
"author": "Dfounderliu",
2626
"license": "Apache-2.0",
27-
"husky": {
28-
"hooks": {
29-
"pre-commit": "lint-staged",
30-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
31-
}
32-
},
3327
"lint-staged": {
3428
"**/*.{js,ts,tsx}": [
3529
"eslint --fix --ext .js,.ts,.tsx .",
@@ -47,8 +41,7 @@
4741
},
4842
"dependencies": {
4943
"@serverless/core": "^1.1.1",
50-
"@serverless/tencent-apigateway": "^2.1.4",
51-
"@serverless/tencent-scf": "^3.0.0",
44+
"@serverless/tencent-framework": "^0.0.1",
5245
"ext": "^1.4.0",
5346
"type": "^2.0.0"
5447
},

serverless.js

Lines changed: 34 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
const ensureIterable = require('type/iterable/ensure')
2-
const ensurePlainObject = require('type/plain-object/ensure')
3-
const ensureString = require('type/string/ensure')
4-
const random = require('ext/string/random')
52
const path = require('path')
63
const { Component } = require('@serverless/core')
74
const fs = require('fs')
@@ -39,121 +36,57 @@ class TencentWerobot extends Component {
3936
}
4037
}
4138

42-
async prepareInputs(inputs = {}) {
43-
inputs.name =
44-
ensureString(inputs.functionName, { isOptional: true }) ||
45-
this.state.functionName ||
46-
`WerobotComponent_${random({ length: 6 })}`
47-
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd()
48-
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' })
49-
inputs.namespace = ensureString(inputs.namespace, { default: 'default' })
50-
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString })
51-
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString })
52-
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
39+
async default(inputs = {}) {
40+
if (!inputs.werobotProjectName) {
41+
throw new Error(`'werobotProjectName' is required in serverless.yaml`)
42+
}
43+
if (!inputs.werobotAttrName) {
44+
throw new Error(`'werobotAttrName' is required in serverless.yaml`)
45+
}
46+
const cachePath = path.join(inputs.code, '.cache')
47+
inputs.include = ensureIterable(inputs.include, { default: [] })
48+
inputs.include.push(cachePath)
5349

5450
const src = path.join(__dirname, 'component')
55-
const dst = path.join(inputs.codeUri, '.cache')
56-
await this.copyDir(src, dst)
51+
await this.copyDir(src, cachePath)
5752
const indexPyFile = await fs.readFileSync(
58-
path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'),
53+
path.join(path.resolve(inputs.code), '.cache', 'index.py'),
5954
'utf8'
6055
)
56+
6157
const replacedFile = indexPyFile
6258
.replace(eval('/{{werobot_project}}/g'), inputs.werobotProjectName)
6359
.replace(eval('/{{attribute}}/g'), inputs.werobotAttrName)
64-
await fs.writeFileSync(
65-
path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'),
66-
replacedFile
67-
)
68-
69-
inputs.include = [path.join(inputs.codeUri, '.cache')]
70-
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')
71-
72-
inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
73-
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
74-
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
75-
76-
if (inputs.functionConf) {
77-
inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3
78-
inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128
79-
if (inputs.functionConf.environment) {
80-
inputs.environment = inputs.functionConf.environment
81-
}
82-
if (inputs.functionConf.vpcConfig) {
83-
inputs.vpcConfig = inputs.functionConf.vpcConfig
84-
}
85-
}
86-
87-
return inputs
88-
}
89-
90-
async default(inputs = {}) {
91-
inputs = await this.prepareInputs(inputs)
9260

93-
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
94-
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
61+
await fs.writeFileSync(path.join(path.resolve(inputs.code), '.cache', 'index.py'), replacedFile)
9562

96-
inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-werobot'
97-
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs)
98-
const apigwParam = {
99-
serviceName: inputs.serviceName,
100-
description: 'Serverless Framework Tencent-Werobot Component',
101-
serviceId: inputs.serviceId,
102-
region: inputs.region,
103-
protocols: inputs.apigatewayConf.protocols || ['http'],
104-
environment:
105-
inputs.apigatewayConf && inputs.apigatewayConf.environment
106-
? inputs.apigatewayConf.environment
107-
: 'release',
108-
endpoints: [
109-
{
110-
path: '/',
111-
method: 'ANY',
112-
function: {
113-
isIntegratedResponse: true,
114-
functionName: tencentCloudFunctionOutputs.Name,
115-
functionNamespace: inputs.namespace
116-
}
117-
}
118-
],
119-
customDomain: inputs.apigatewayConf.customDomain
120-
}
63+
inputs.handelr = DEFAULTS.handler
64+
inputs.runtime = DEFAULTS.runtime
12165

122-
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
123-
apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan
124-
}
125-
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
126-
apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth
127-
}
128-
129-
apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-werobot'
130-
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam)
131-
const outputs = {
132-
region: inputs.region,
133-
functionName: inputs.name,
134-
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
135-
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
136-
tencentApiGatewayOutputs.subDomain
137-
}/${tencentApiGatewayOutputs.environment}/`
138-
}
66+
const Framework = await this.load('@serverless/tencent-framework')
13967

140-
this.state = outputs
68+
const framworkOutpus = await Framework({
69+
...inputs,
70+
...{
71+
framework: 'werobot'
72+
}
73+
})
14174

75+
this.state = framworkOutpus
14276
await this.save()
143-
144-
return outputs
77+
return framworkOutpus
14578
}
14679

14780
async remove(inputs = {}) {
148-
const removeInput = {
149-
fromClientRemark: inputs.fromClientRemark || 'tencent-werobot'
150-
}
151-
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
152-
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
153-
154-
await tencentCloudFunction.remove(removeInput)
155-
await tencentApiGateway.remove(removeInput)
156-
81+
const Framework = await this.load('@serverless/tencent-framework')
82+
await Framework.remove({
83+
...inputs,
84+
...{
85+
framework: 'werobot'
86+
}
87+
})
88+
this.state = {}
89+
await this.save()
15790
return {}
15891
}
15992
}

0 commit comments

Comments
 (0)