Skip to content

Commit 85c2ddb

Browse files
authored
Merge pull request #2 from serverless-tencent/mutil-region
增加多地域部署能力
2 parents a58b258 + b21b60e commit 85c2ddb

File tree

2 files changed

+31
-113
lines changed

2 files changed

+31
-113
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-pyramid",
33
"description": "Tencent Cloud Python Pyramid Serverless Component",
4-
"version": "0.0.1",
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: 29 additions & 104 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,125 +36,53 @@ class TencentPyramid extends Component {
3936
}
4037
}
4138

42-
async prepareInputs(inputs = {}) {
43-
inputs.name =
44-
ensureString(inputs.functionName, { isOptional: true }) ||
45-
this.state.functionName ||
46-
`PyramidComponent_${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.pyramidProjectName) {
41+
throw new Error(`'pyramidProjectName' is required in serverless.yaml`)
42+
}
43+
const cachePath = path.join(inputs.code, '.cache')
44+
inputs.include = ensureIterable(inputs.include, { default: [] })
45+
inputs.include.push(cachePath)
5346

5447
const src = path.join(__dirname, 'component')
55-
const dst = path.join(inputs.codeUri, '.cache')
56-
await this.copyDir(src, dst)
48+
await this.copyDir(src, cachePath)
5749
const indexPyFile = await fs.readFileSync(
58-
path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'),
50+
path.join(path.resolve(inputs.code), '.cache', 'index.py'),
5951
'utf8'
6052
)
6153
const replacedFile = indexPyFile.replace(
6254
eval('/{{pyramid_project}}/g'),
6355
inputs.pyramidProjectName
6456
)
65-
await fs.writeFileSync(
66-
path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'),
67-
replacedFile
68-
)
57+
await fs.writeFileSync(path.join(path.resolve(inputs.code), '.cache', 'index.py'), replacedFile)
6958

70-
inputs.include = [path.join(inputs.codeUri, '.cache')]
71-
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')
59+
inputs.handelr = DEFAULTS.handler
60+
inputs.runtime = DEFAULTS.runtime
7261

73-
inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
74-
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
75-
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
62+
const Framework = await this.load('@serverless/tencent-framework')
7663

77-
if (inputs.functionConf) {
78-
inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3
79-
inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128
80-
if (inputs.functionConf.environment) {
81-
inputs.environment = inputs.functionConf.environment
82-
}
83-
if (inputs.functionConf.vpcConfig) {
84-
inputs.vpcConfig = inputs.functionConf.vpcConfig
64+
const framworkOutpus = await Framework({
65+
...inputs,
66+
...{
67+
framework: 'pyramid'
8568
}
86-
}
87-
88-
return inputs
89-
}
90-
91-
async default(inputs = {}) {
92-
if (!inputs.pyramidProjectName) {
93-
throw new Error(`'pyramidProjectName' is required in serverless.yaml`)
94-
}
95-
inputs = await this.prepareInputs(inputs)
96-
97-
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
98-
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
99-
100-
inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-pyramid'
101-
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs)
102-
const apigwParam = {
103-
serviceName: inputs.serviceName,
104-
description: 'Serverless Framework Tencent-Pyramid Component',
105-
serviceId: inputs.serviceId,
106-
region: inputs.region,
107-
protocols: inputs.apigatewayConf.protocols || ['http'],
108-
environment:
109-
inputs.apigatewayConf && inputs.apigatewayConf.environment
110-
? inputs.apigatewayConf.environment
111-
: 'release',
112-
endpoints: [
113-
{
114-
path: '/',
115-
method: 'ANY',
116-
function: {
117-
isIntegratedResponse: true,
118-
functionName: tencentCloudFunctionOutputs.Name,
119-
functionNamespace: inputs.namespace
120-
}
121-
}
122-
],
123-
customDomain: inputs.apigatewayConf.customDomain
124-
}
125-
126-
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
127-
apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan
128-
}
129-
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
130-
apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth
131-
}
132-
133-
apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-pyramid'
134-
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam)
135-
const outputs = {
136-
region: inputs.region,
137-
functionName: inputs.name,
138-
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
139-
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
140-
tencentApiGatewayOutputs.subDomain
141-
}/${tencentApiGatewayOutputs.environment}/`
142-
}
143-
144-
this.state = outputs
69+
})
14570

71+
this.state = framworkOutpus
14672
await this.save()
147-
148-
return outputs
73+
return framworkOutpus
14974
}
15075

15176
async remove(inputs = {}) {
152-
const removeInput = {
153-
fromClientRemark: inputs.fromClientRemark || 'tencent-pyramid'
154-
}
155-
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
156-
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
157-
158-
await tencentCloudFunction.remove(removeInput)
159-
await tencentApiGateway.remove(removeInput)
160-
77+
const Framework = await this.load('@serverless/tencent-framework')
78+
await Framework.remove({
79+
...inputs,
80+
...{
81+
framework: 'pyramid'
82+
}
83+
})
84+
this.state = {}
85+
await this.save()
16186
return {}
16287
}
16388
}

0 commit comments

Comments
 (0)