Skip to content

Commit 6db584d

Browse files
authored
Merge pull request #2 from serverless-tencent/mutil-region
增加多地域部署能力
2 parents 55175db + f8b43dd commit 6db584d

File tree

3 files changed

+27
-109
lines changed

3 files changed

+27
-109
lines changed

component/index.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from werkzeug.http import HTTP_STATUS_CODES
1010
from werkzeug._compat import BytesIO, string_types, to_bytes, wsgi_encoding_dance
1111
import bottle
12+
import app
1213

1314
TEXT_MIME_TYPES = [
1415
"application/json",

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-bottle",
33
"description": "Tencent Cloud Python Bottle 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: 24 additions & 100 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

@@ -12,113 +9,40 @@ const DEFAULTS = {
129
}
1310

1411
class TencentBottle extends Component {
15-
getDefaultProtocol(protocols) {
16-
if (protocols.map((i) => i.toLowerCase()).includes('https')) {
17-
return 'https'
18-
}
19-
return 'http'
20-
}
21-
22-
async prepareInputs(inputs = {}) {
23-
inputs.name =
24-
ensureString(inputs.functionName, { isOptional: true }) ||
25-
this.state.functionName ||
26-
`BottleComponent_${random({ length: 6 })}`
27-
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd()
28-
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' })
29-
inputs.namespace = ensureString(inputs.namespace, { default: 'default' })
30-
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString })
31-
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString })
32-
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
33-
34-
inputs.include = [path.join(__dirname, 'component')]
35-
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')
36-
37-
inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
38-
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
39-
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
40-
41-
if (inputs.functionConf) {
42-
inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3
43-
inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128
44-
if (inputs.functionConf.environment) {
45-
inputs.environment = inputs.functionConf.environment
46-
}
47-
if (inputs.functionConf.vpcConfig) {
48-
inputs.vpcConfig = inputs.functionConf.vpcConfig
49-
}
50-
}
51-
52-
return inputs
53-
}
54-
5512
async default(inputs = {}) {
56-
inputs = await this.prepareInputs(inputs)
57-
58-
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
59-
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
13+
inputs.include = []
6014

61-
inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-pyramid'
62-
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs)
63-
const apigwParam = {
64-
serviceName: inputs.serviceName,
65-
description: 'Serverless Framework Tencent-Bottle Component',
66-
serviceId: inputs.serviceId,
67-
region: inputs.region,
68-
protocols: inputs.apigatewayConf.protocols || ['http'],
69-
environment:
70-
inputs.apigatewayConf && inputs.apigatewayConf.environment
71-
? inputs.apigatewayConf.environment
72-
: 'release',
73-
endpoints: [
74-
{
75-
path: '/',
76-
method: 'ANY',
77-
function: {
78-
isIntegratedResponse: true,
79-
functionName: tencentCloudFunctionOutputs.Name,
80-
functionNamespace: inputs.namespace
81-
}
82-
}
83-
],
84-
customDomain: inputs.apigatewayConf.customDomain
85-
}
15+
const cachePath = path.join(__dirname, 'component')
16+
inputs.include = ensureIterable(inputs.include, { default: [] })
17+
inputs.include.push(cachePath)
8618

87-
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
88-
apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan
89-
}
90-
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
91-
apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth
92-
}
19+
inputs.handelr = DEFAULTS.handler
20+
inputs.runtime = DEFAULTS.runtime
9321

94-
apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-bottle'
95-
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam)
96-
const outputs = {
97-
region: inputs.region,
98-
functionName: inputs.name,
99-
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
100-
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
101-
tencentApiGatewayOutputs.subDomain
102-
}/${tencentApiGatewayOutputs.environment}/`
103-
}
22+
const Framework = await this.load('@serverless/tencent-framework')
10423

105-
this.state = outputs
24+
const framworkOutpus = await Framework({
25+
...inputs,
26+
...{
27+
framework: 'bottle'
28+
}
29+
})
10630

31+
this.state = framworkOutpus
10732
await this.save()
108-
109-
return outputs
33+
return framworkOutpus
11034
}
11135

11236
async remove(inputs = {}) {
113-
const removeInput = {
114-
fromClientRemark: inputs.fromClientRemark || 'tencent-bottle'
115-
}
116-
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
117-
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
118-
119-
await tencentCloudFunction.remove(removeInput)
120-
await tencentApiGateway.remove(removeInput)
121-
37+
const Framework = await this.load('@serverless/tencent-framework')
38+
await Framework.remove({
39+
...inputs,
40+
...{
41+
framework: 'bottle'
42+
}
43+
})
44+
this.state = {}
45+
await this.save()
12246
return {}
12347
}
12448
}

0 commit comments

Comments
 (0)