mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Refactoring and code cleanup
So that the code looks everywhere the familiar.
This commit is contained in:
parent
4d3fa667ca
commit
6bb55dbd63
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const BbPromise = require('bluebird');
|
||||
const _ = require('lodash');
|
||||
const forEach = require('lodash').forEach;
|
||||
|
||||
const compileRestApi = require('./lib/compileRestApi');
|
||||
const compileResources = require('./lib/compileResources');
|
||||
@ -32,7 +32,7 @@ class AwsCompileApigEvents {
|
||||
|
||||
let noEndpoints = true;
|
||||
|
||||
_.forEach(this.serverless.service.functions, functionObj => {
|
||||
forEach(this.serverless.service.functions, functionObj => {
|
||||
if (functionObj.events && functionObj.events.aws
|
||||
&& functionObj.events.aws.http_endpoints) {
|
||||
noEndpoints = false;
|
||||
|
||||
@ -6,15 +6,15 @@ const BbPromise = require('bluebird');
|
||||
module.exports = {
|
||||
compileDeployment() {
|
||||
const deploymentTemplate = `
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::Deployment",
|
||||
"DependsOn" : "${this.methodDep}",
|
||||
"Properties" : {
|
||||
"RestApiId" : { "Ref": "RestApiApigEvent" },
|
||||
"StageName" : "${this.options.stage}"
|
||||
}
|
||||
}
|
||||
`;
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::Deployment",
|
||||
"DependsOn" : "${this.methodDep}",
|
||||
"Properties" : {
|
||||
"RestApiId" : { "Ref": "RestApiApigEvent" },
|
||||
"StageName" : "${this.options.stage}"
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const newDeploymentObject = {
|
||||
DeploymentApigEvent: JSON.parse(deploymentTemplate),
|
||||
|
||||
@ -26,29 +26,29 @@ module.exports = {
|
||||
'/invocations';
|
||||
|
||||
const methodTemplate = `
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::Method",
|
||||
"Properties" : {
|
||||
"AuthorizationType" : "NONE",
|
||||
"HttpMethod" : "${method.toUpperCase()}",
|
||||
"MethodResponses" : [
|
||||
{
|
||||
"ResponseModels" : {},
|
||||
"ResponseParameters" : {},
|
||||
"StatusCode" : "200"
|
||||
}
|
||||
],
|
||||
"RequestParameters" : {},
|
||||
"Integration" : {
|
||||
"IntegrationHttpMethod" : "${method.toUpperCase()}",
|
||||
"Type" : "AWS",
|
||||
"Uri" : "${lambdaUri}"
|
||||
},
|
||||
"ResourceId" : { "Ref": "${resourceLogicalId}" },
|
||||
"RestApiId" : { "Ref": "RestApiApigEvent" }
|
||||
}
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::Method",
|
||||
"Properties" : {
|
||||
"AuthorizationType" : "NONE",
|
||||
"HttpMethod" : "${method.toUpperCase()}",
|
||||
"MethodResponses" : [
|
||||
{
|
||||
"ResponseModels" : {},
|
||||
"ResponseParameters" : {},
|
||||
"StatusCode" : "200"
|
||||
}
|
||||
],
|
||||
"RequestParameters" : {},
|
||||
"Integration" : {
|
||||
"IntegrationHttpMethod" : "${method.toUpperCase()}",
|
||||
"Type" : "AWS",
|
||||
"Uri" : "${lambdaUri}"
|
||||
},
|
||||
"ResourceId" : { "Ref": "${resourceLogicalId}" },
|
||||
"RestApiId" : { "Ref": "RestApiApigEvent" }
|
||||
}
|
||||
`;
|
||||
}
|
||||
`;
|
||||
|
||||
const methodObject = {
|
||||
[`${normalizedMethod}MethodApigEvent${extractedResourceId}`]: JSON.parse(methodTemplate),
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
const BbPromise = require('bluebird');
|
||||
const _ = require('lodash');
|
||||
const forEach = require('lodash').forEach;
|
||||
const merge = require('lodash').merge;
|
||||
|
||||
module.exports = {
|
||||
compilePermissions() {
|
||||
_.forEach(this.serverless.service.functions, (functionObj, functionName) => {
|
||||
_.forEach(functionObj.events.aws.http_endpoints, (path, method) => {
|
||||
forEach(this.serverless.service.functions, (functionObj, functionName) => {
|
||||
forEach(functionObj.events.aws.http_endpoints, (path, method) => {
|
||||
const normalizedMethod = method[0].toUpperCase() + method.substr(1);
|
||||
const permissionTemplate = `
|
||||
{
|
||||
@ -16,7 +17,8 @@ module.exports = {
|
||||
"Action": "lambda:InvokeFunction",
|
||||
"Principal": "apigateway.amazonaws.com"
|
||||
}
|
||||
}`;
|
||||
}
|
||||
`;
|
||||
|
||||
const permissionLogicalId = `ApigPermission${normalizedMethod}${this
|
||||
.resourcePaths.indexOf(path)}`;
|
||||
@ -24,7 +26,7 @@ module.exports = {
|
||||
[permissionLogicalId]: JSON.parse(permissionTemplate),
|
||||
};
|
||||
|
||||
_.merge(this.serverless.service.resources.aws.Resources, newPermissionObject);
|
||||
merge(this.serverless.service.resources.aws.Resources, newPermissionObject);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -9,8 +9,8 @@ module.exports = {
|
||||
this.resourcePaths = [];
|
||||
this.resourceLogicalIds = {};
|
||||
|
||||
forEach(this.serverless.service.functions, (functionObj) => {
|
||||
forEach(functionObj.events.aws.http_endpoints, (pathParam) => {
|
||||
forEach(this.serverless.service.functions, (functionObject) => {
|
||||
forEach(functionObject.events.aws.http_endpoints, (pathParam) => {
|
||||
let path = pathParam;
|
||||
while (path !== '') {
|
||||
if (this.resourcePaths.indexOf(path) === -1) {
|
||||
|
||||
@ -5,22 +5,21 @@ const BbPromise = require('bluebird');
|
||||
|
||||
module.exports = {
|
||||
compileRestApi() {
|
||||
this.serverless.service.getAllFunctions().forEach((functionName) => {
|
||||
const functionObject = this.serverless.service.getFunction(functionName);
|
||||
const restApiTemplate = `
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::RestApi",
|
||||
"Properties" : {
|
||||
"Name" : "${this.options.stage}-${this.serverless.service.service}"
|
||||
}
|
||||
this.serverless.service.getAllFunctions().forEach(() => {
|
||||
const restApiTemplate = `
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::RestApi",
|
||||
"Properties" : {
|
||||
"Name" : "${this.options.stage}-${this.serverless.service.service}"
|
||||
}
|
||||
`;
|
||||
}
|
||||
`;
|
||||
|
||||
const newRestApiObject = {
|
||||
RestApiApigEvent: JSON.parse(restApiTemplate),
|
||||
};
|
||||
const newRestApiObject = {
|
||||
RestApiApigEvent: JSON.parse(restApiTemplate),
|
||||
};
|
||||
|
||||
merge(this.serverless.service.resources.aws.Resources, newRestApiObject);
|
||||
merge(this.serverless.service.resources.aws.Resources, newRestApiObject);
|
||||
});
|
||||
|
||||
return BbPromise.resolve();
|
||||
|
||||
@ -5,30 +5,23 @@ const BbPromise = require('bluebird');
|
||||
|
||||
module.exports = {
|
||||
compileStage() {
|
||||
this.serverless.service.getAllFunctions().forEach((functionName) => {
|
||||
const functionObject = this.serverless.service.getFunction(functionName);
|
||||
|
||||
// checking all three levels in the obj tree
|
||||
// to avoid "can't read property of undefined" error
|
||||
if (functionObject.events && functionObject.events.aws
|
||||
&& functionObject.events.aws.http_endpoints) {
|
||||
const stageTemplate = `
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::Stage",
|
||||
"Properties" : {
|
||||
"DeploymentId": { "Ref": "DeploymentApigEvent" },
|
||||
"RestApiId" : { "Ref": "RestApiApigEvent" },
|
||||
"StageName" : "${this.options.stage}"
|
||||
}
|
||||
this.serverless.service.getAllFunctions().forEach(() => {
|
||||
const stageTemplate = `
|
||||
{
|
||||
"Type" : "AWS::ApiGateway::Stage",
|
||||
"Properties" : {
|
||||
"DeploymentId": { "Ref": "DeploymentApigEvent" },
|
||||
"RestApiId" : { "Ref": "RestApiApigEvent" },
|
||||
"StageName" : "${this.options.stage}"
|
||||
}
|
||||
`;
|
||||
}
|
||||
`;
|
||||
|
||||
const newStageObject = {
|
||||
[`StageApigEvent`]: JSON.parse(stageTemplate),
|
||||
};
|
||||
const newStageObject = {
|
||||
StageApigEvent: JSON.parse(stageTemplate),
|
||||
};
|
||||
|
||||
merge(this.serverless.service.resources.aws.Resources, newStageObject);
|
||||
}
|
||||
merge(this.serverless.service.resources.aws.Resources, newStageObject);
|
||||
});
|
||||
|
||||
return BbPromise.resolve();
|
||||
|
||||
@ -13,30 +13,30 @@ default_providers: &default_providers
|
||||
disabled: false
|
||||
|
||||
functions: # if this gets too big, you can always use JSON-REF
|
||||
hello:
|
||||
name_template: ${stage}-${service}-${name} # "name" references the function name, service the whole service name
|
||||
handler: handler.hello
|
||||
# only the following paths will be included in the resulting artefact uploaded to Lambda. Without specific include everything in the current folder will be included
|
||||
include:
|
||||
- lib
|
||||
- functions
|
||||
# The following paths will be excluded from the resulting artefact. If both include and exclude are defined we first apply the include, then the exclude so files are guaranteed to be excluded.
|
||||
exclude:
|
||||
- tmp
|
||||
- .git
|
||||
provider:
|
||||
<<: *default_providers
|
||||
events:
|
||||
aws:
|
||||
# s3:
|
||||
# - first-bucket
|
||||
http_endpoints:
|
||||
post: users/create
|
||||
# schedule: rate(10 minutes)
|
||||
azure:
|
||||
http_endpoint:
|
||||
direction: in
|
||||
name: req
|
||||
hello:
|
||||
name_template: ${stage}-${service}-${name} # "name" references the function name, service the whole service name
|
||||
handler: handler.hello
|
||||
# only the following paths will be included in the resulting artefact uploaded to Lambda. Without specific include everything in the current folder will be included
|
||||
include:
|
||||
- lib
|
||||
- functions
|
||||
# The following paths will be excluded from the resulting artefact. If both include and exclude are defined we first apply the include, then the exclude so files are guaranteed to be excluded.
|
||||
exclude:
|
||||
- tmp
|
||||
- .git
|
||||
provider:
|
||||
<<: *default_providers
|
||||
events:
|
||||
aws:
|
||||
# s3:
|
||||
# - first-bucket
|
||||
http_endpoints:
|
||||
post: users/create
|
||||
# schedule: rate(10 minutes)
|
||||
azure:
|
||||
http_endpoints:
|
||||
direction: in
|
||||
name: req
|
||||
|
||||
resources:
|
||||
aws_name_template: ${stage}-${service}-${name} # "name" references the resource name, service the whole service name
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user