Skip to content

Commit 38a93c4

Browse files
committed
feat: support custom entry file
1 parent 01bbf01 commit 38a93c4

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

serverless.component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: koa
2-
version: 0.1.0
2+
version: 0.2.0
33
author: 'Tencent Cloud, Inc.'
44
org: 'Tencent Cloud, Inc.'
55
description: Deploy a serverless Koa.js application onto Tencent SCF and API Gateway.

src/_shims/handler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ const path = require('path')
33
const { createServer, proxy } = require('tencent-serverless-http')
44

55
let server
6+
let app
67

78
exports.handler = async (event, context) => {
8-
const userSls = path.join(__dirname, '..', 'sls.js')
9-
let app
9+
const userSls = path.join(__dirname, '..', process.env.SLS_ENTRY_FILE)
1010
if (fs.existsSync(userSls)) {
11-
// load the user provided app
11+
// eslint-disable-next-line
12+
console.log(`Using user custom entry file ${process.env.SLS_ENTRY_FILE}`)
1213
app = require(userSls)
1314
} else {
14-
// load the built-in default app
1515
app = require('./sls.js')
1616
}
1717

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const CONFIGS = {
22
templateUrl: 'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/koa-demo.zip',
33
compName: 'koa',
44
compFullname: 'Koa.js',
5+
defaultEntryFile: 'sls.js',
56
handler: 'sl_handler.handler',
67
runtime: 'Nodejs10.15',
78
timeout: 3,

src/utils.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,21 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
239239
functionConf.needSetTraffic = inputs.traffic !== undefined && functionConf.lastVersion
240240

241241
if (tempFunctionConf.environment) {
242-
functionConf.environment = inputs.functionConf.environment
242+
functionConf.environment = tempFunctionConf.environment
243+
functionConf.environment.variables = functionConf.environment.variables || {}
244+
functionConf.environment.variables.SERVERLESS = '1'
245+
functionConf.environment.variables.SLS_ENTRY_FILE = inputs.entryFile || CONFIGS.defaultEntryFile
246+
} else {
247+
functionConf.environment = {
248+
variables: {
249+
SERVERLESS: '1',
250+
SLS_ENTRY_FILE: inputs.entryFile || CONFIGS.defaultEntryFile
251+
}
252+
}
243253
}
254+
244255
if (tempFunctionConf.vpcConfig) {
245-
functionConf.vpcConfig = inputs.functionConf.vpcConfig
256+
functionConf.vpcConfig = tempFunctionConf.vpcConfig
246257
}
247258

248259
// 对apigw inputs进行标准化
@@ -257,19 +268,22 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
257268
isDisabled: tempApigwConf.isDisabled === true,
258269
fromClientRemark: fromClientRemark,
259270
serviceName: inputs.serviceName || getDefaultServiceName(instance),
260-
description: getDefaultServiceDescription(instance),
271+
description: tempApigwConf.description || getDefaultServiceDescription(instance),
261272
protocols: tempApigwConf.protocols || ['http'],
262273
environment: tempApigwConf.environment ? tempApigwConf.environment : 'release',
263274
endpoints: [
264275
{
265-
path: '/',
276+
path: tempApigwConf.path || '/',
266277
enableCORS: tempApigwConf.enableCORS,
267278
serviceTimeout: tempApigwConf.serviceTimeout,
268279
method: 'ANY',
280+
apiName: tempApigwConf.apiName || 'index',
269281
function: {
270282
isIntegratedResponse: true,
271283
functionName: functionConf.name,
272-
functionNamespace: functionConf.namespace
284+
functionNamespace: functionConf.namespace,
285+
functionQualifier:
286+
(tempApigwConf.function && tempApigwConf.function.functionQualifier) || '$LATEST'
273287
}
274288
}
275289
],

0 commit comments

Comments
 (0)