Skip to content

Commit

Permalink
Merge pull request #2 from serverless-tencent/mutil-region
Browse files Browse the repository at this point in the history
增加多地域部署能力
  • Loading branch information
anycodes authored Mar 17, 2020
2 parents 55175db + f8b43dd commit 6db584d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 109 deletions.
1 change: 1 addition & 0 deletions component/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from werkzeug.http import HTTP_STATUS_CODES
from werkzeug._compat import BytesIO, string_types, to_bytes, wsgi_encoding_dance
import bottle
import app

TEXT_MIME_TYPES = [
"application/json",
Expand Down
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@serverless/tencent-bottle",
"description": "Tencent Cloud Python Bottle Serverless Component",
"version": "0.0.1",
"version": "1.0.0",
"main": "serverless.js",
"publishConfig": {
"access": "public"
Expand All @@ -24,12 +24,6 @@
},
"author": "Dfounderliu",
"license": "Apache-2.0",
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"lint-staged": {
"**/*.{js,ts,tsx}": [
"eslint --fix --ext .js,.ts,.tsx .",
Expand All @@ -47,8 +41,7 @@
},
"dependencies": {
"@serverless/core": "^1.1.1",
"@serverless/tencent-apigateway": "^2.1.4",
"@serverless/tencent-scf": "^3.0.0",
"@serverless/tencent-framework": "^0.0.1",
"ext": "^1.4.0",
"type": "^2.0.0"
},
Expand Down
124 changes: 24 additions & 100 deletions serverless.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
const ensureIterable = require('type/iterable/ensure')
const ensurePlainObject = require('type/plain-object/ensure')
const ensureString = require('type/string/ensure')
const random = require('ext/string/random')
const path = require('path')
const { Component } = require('@serverless/core')

Expand All @@ -12,113 +9,40 @@ const DEFAULTS = {
}

class TencentBottle extends Component {
getDefaultProtocol(protocols) {
if (protocols.map((i) => i.toLowerCase()).includes('https')) {
return 'https'
}
return 'http'
}

async prepareInputs(inputs = {}) {
inputs.name =
ensureString(inputs.functionName, { isOptional: true }) ||
this.state.functionName ||
`BottleComponent_${random({ length: 6 })}`
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd()
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' })
inputs.namespace = ensureString(inputs.namespace, { default: 'default' })
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString })
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString })
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })

inputs.include = [path.join(__dirname, 'component')]
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')

inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })

if (inputs.functionConf) {
inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3
inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128
if (inputs.functionConf.environment) {
inputs.environment = inputs.functionConf.environment
}
if (inputs.functionConf.vpcConfig) {
inputs.vpcConfig = inputs.functionConf.vpcConfig
}
}

return inputs
}

async default(inputs = {}) {
inputs = await this.prepareInputs(inputs)

const tencentCloudFunction = await this.load('@serverless/tencent-scf')
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')
inputs.include = []

inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-pyramid'
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs)
const apigwParam = {
serviceName: inputs.serviceName,
description: 'Serverless Framework Tencent-Bottle Component',
serviceId: inputs.serviceId,
region: inputs.region,
protocols: inputs.apigatewayConf.protocols || ['http'],
environment:
inputs.apigatewayConf && inputs.apigatewayConf.environment
? inputs.apigatewayConf.environment
: 'release',
endpoints: [
{
path: '/',
method: 'ANY',
function: {
isIntegratedResponse: true,
functionName: tencentCloudFunctionOutputs.Name,
functionNamespace: inputs.namespace
}
}
],
customDomain: inputs.apigatewayConf.customDomain
}
const cachePath = path.join(__dirname, 'component')
inputs.include = ensureIterable(inputs.include, { default: [] })
inputs.include.push(cachePath)

if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan
}
if (inputs.apigatewayConf && inputs.apigatewayConf.auth) {
apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth
}
inputs.handelr = DEFAULTS.handler
inputs.runtime = DEFAULTS.runtime

apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-bottle'
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam)
const outputs = {
region: inputs.region,
functionName: inputs.name,
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
tencentApiGatewayOutputs.subDomain
}/${tencentApiGatewayOutputs.environment}/`
}
const Framework = await this.load('@serverless/tencent-framework')

this.state = outputs
const framworkOutpus = await Framework({
...inputs,
...{
framework: 'bottle'
}
})

this.state = framworkOutpus
await this.save()

return outputs
return framworkOutpus
}

async remove(inputs = {}) {
const removeInput = {
fromClientRemark: inputs.fromClientRemark || 'tencent-bottle'
}
const tencentCloudFunction = await this.load('@serverless/tencent-scf')
const tencentApiGateway = await this.load('@serverless/tencent-apigateway')

await tencentCloudFunction.remove(removeInput)
await tencentApiGateway.remove(removeInput)

const Framework = await this.load('@serverless/tencent-framework')
await Framework.remove({
...inputs,
...{
framework: 'bottle'
}
})
this.state = {}
await this.save()
return {}
}
}
Expand Down

0 comments on commit 6db584d

Please sign in to comment.