-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserverless.js
92 lines (80 loc) · 4.14 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
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, utils } = require('@serverless/core');
module.exports = class TencentFastify extends Component {
async default(inputs = {}) {
inputs.name =
ensureString(inputs.functionName, { isOptional: true }) ||
this.state.functionName ||
`FastifyComponent_${random({ length: 6 })}`;
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd();
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' });
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString });
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString });
const apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} });
if (!(await utils.fileExists(path.resolve(inputs.codeUri, 'app.js')))) {
throw new Error(`app.js not found in ${inputs.codeUri}`);
}
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store');
const filePath = path.resolve(__dirname, 'lambda.js');
const tencentFastifyPath = path.resolve(__dirname, 'tencent-cloud-fastify.js');
inputs.include.push(filePath);
inputs.include.push(tencentFastifyPath);
inputs.handler = 'lambda.handler';
inputs.runtime = 'Nodejs8.9';
const tencentCloudFunction = await this.load('@serverless/tencent-scf');
const tencentApiGateway = await this.load('@serverless/tencent-apigateway');
if (inputs.functionConf) {
inputs.timeout = inputs.functionConf.timeout || 3;
inputs.memorySize = inputs.functionConf.memorySize || 128;
if (inputs.functionConf.environment) inputs.environment = inputs.functionConf.environment;
if (inputs.functionConf.vpcConfig) inputs.vpcConfig = inputs.functionConf.vpcConfig;
}
inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-fastify';
const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs);
const apigwParam = {
serviceName: inputs.serviceName,
description: 'Serverless Framework tencent-fastify Component',
serviceId: inputs.serviceId,
region: inputs.region,
protocol: apigatewayConf.protocol || 'http',
environment: apigatewayConf.environment || 'release',
endpoints: [
{
path: '/',
method: 'ANY',
function: {
isIntegratedResponse: true,
functionName: tencentCloudFunctionOutputs.Name,
},
},
],
};
if (apigatewayConf.usagePlan) apigwParam.endpoints[0].usagePlan = apigatewayConf.usagePlan;
if (apigatewayConf.auth) apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth;
this.state.functionName = inputs.name;
await this.save();
apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-fastify';
const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam);
return {
region: tencentApiGatewayOutputs.region,
functionName: inputs.name,
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
url: `${tencentApiGatewayOutputs.protocols[0]}://${tencentApiGatewayOutputs.subDomain}/${tencentApiGatewayOutputs.environment}/`,
};
}
async remove(inputs = {}) {
const removeInput = {
fromClientRemark: inputs.fromClientRemark || 'tencent-fastify'
};
const tencentApiGateway = await this.load('@serverless/tencent-apigateway');
const tencentCloudFunction = await this.load('@serverless/tencent-scf');
await tencentApiGateway.remove(removeInput);
await tencentCloudFunction.remove(removeInput);
return {};
}
};