1
+ 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' )
1
5
const path = require ( 'path' )
2
6
const { Component, utils } = require ( '@serverless/core' )
3
7
@@ -8,32 +12,40 @@ const DEFAULTS = {
8
12
}
9
13
10
14
class TencentFlask extends Component {
15
+ getDefaultProtocol ( protocols ) {
16
+ if ( protocols . map ( ( i ) => i . toLowerCase ( ) ) . includes ( 'https' ) ) {
17
+ return 'https'
18
+ }
19
+ return 'http'
20
+ }
21
+
11
22
/**
12
23
* prepare create function inputs
13
24
* @param {object } inputs inputs
14
25
*/
15
26
async prepareInputs ( inputs = { } ) {
16
- const len = 6
17
- const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
18
- const maxPos = chars . length
19
- let result = ''
20
- for ( let i = 0 ; i < len ; i ++ ) {
21
- result += chars . charAt ( Math . floor ( Math . random ( ) * maxPos ) )
22
- }
27
+ inputs . name =
28
+ ensureString ( inputs . functionName , { isOptional : true } ) ||
29
+ this . state . functionName ||
30
+ `FlaskComponent_${ random ( { length : 6 } ) } `
31
+ inputs . codeUri = ensureString ( inputs . code , { isOptional : true } ) || process . cwd ( )
32
+ inputs . region = ensureString ( inputs . region , { default : 'ap-guangzhou' } )
33
+ inputs . include = ensureIterable ( inputs . include , { default : [ ] , ensureItem : ensureString } )
34
+ inputs . exclude = ensureIterable ( inputs . exclude , { default : [ ] , ensureItem : ensureString } )
35
+ inputs . apigatewayConf = ensurePlainObject ( inputs . apigatewayConf , { default : { } } )
23
36
24
37
const shimsDir = path . join ( __dirname , 'shims' )
25
38
inputs . include = [
26
39
path . join ( shimsDir , 'severless_wsgi.py' ) ,
27
40
path . join ( shimsDir , 'api_service.py' )
28
41
]
29
- inputs . exclude = inputs . exclude || [ '.git/**' , '.gitignore' , '.serverless' , '.DS_Store' ]
30
- inputs . handler = inputs . handler || DEFAULTS . handler
31
- inputs . runtime = inputs . runtime || DEFAULTS . runtime
32
- inputs . name = inputs . functionName || 'FlaskComponent_' + result
33
- inputs . codeUri = inputs . codeUri || process . cwd ( )
42
+ inputs . exclude . push ( '.git/**' , '.gitignore' , '.serverless' , '.DS_Store' )
34
43
35
- const appFile = path . join ( path . resolve ( inputs . codeUri ) , 'app.py' )
44
+ inputs . handler = ensureString ( inputs . handler , { default : DEFAULTS . handler } )
45
+ inputs . runtime = ensureString ( inputs . runtime , { default : DEFAULTS . runtime } )
46
+ inputs . apigatewayConf = ensurePlainObject ( inputs . apigatewayConf , { default : { } } )
36
47
48
+ const appFile = path . join ( path . resolve ( inputs . codeUri ) , 'app.py' )
37
49
if ( ! ( await utils . fileExists ( appFile ) ) ) {
38
50
throw new Error ( `app.py not found in ${ inputs . codeUri } ` )
39
51
}
@@ -48,9 +60,8 @@ class TencentFlask extends Component {
48
60
inputs . vpcConfig = inputs . functionConf . vpcConfig
49
61
}
50
62
}
51
- if ( ! inputs . requirements ) {
52
- inputs . requirements = { }
53
- }
63
+
64
+ inputs . requirements = ensurePlainObject ( inputs . apigatewayConf , { default : { } } )
54
65
inputs . requirements . include = inputs . include
55
66
inputs . requirements . runtime = inputs . runtime . toLowerCase ( )
56
67
inputs . requirements . codeUri = inputs . codeUri
@@ -78,10 +89,7 @@ class TencentFlask extends Component {
78
89
description : 'Serverless Framework tencent-flask Component' ,
79
90
serviceId : inputs . serviceId ,
80
91
region : inputs . region ,
81
- protocol :
82
- inputs . apigatewayConf && inputs . apigatewayConf . protocol
83
- ? inputs . apigatewayConf . protocol
84
- : 'http' ,
92
+ protocols : inputs . apigatewayConf . protocols || [ 'http' ] ,
85
93
environment :
86
94
inputs . apigatewayConf && inputs . apigatewayConf . environment
87
95
? inputs . apigatewayConf . environment
@@ -110,7 +118,9 @@ class TencentFlask extends Component {
110
118
region : inputs . region ,
111
119
functionName : inputs . name ,
112
120
apiGatewayServiceId : tencentApiGatewayOutputs . serviceId ,
113
- url : `${ tencentApiGatewayOutputs . protocol } ://${ tencentApiGatewayOutputs . subDomain } /${ tencentApiGatewayOutputs . environment } /`
121
+ url : `${ this . getDefaultProtocol ( tencentApiGatewayOutputs . protocols ) } ://${
122
+ tencentApiGatewayOutputs . subDomain
123
+ } /${ tencentApiGatewayOutputs . environment } /`
114
124
}
115
125
116
126
await this . save ( )
0 commit comments