This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: process * fix: tencent-support * fix: support timer * fix: test
- Loading branch information
Showing
25 changed files
with
414 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/faas-cli-plugin-invoke/test/fixtures/http-scf/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"dependencies": { | ||
"@midwayjs/faas": "*" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/faas-cli-plugin-invoke/test/fixtures/http-scf/serverless.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
service: serverless-midway-test | ||
|
||
provider: | ||
name: tencent | ||
runtime: nodejs8 | ||
|
||
functions: | ||
http: | ||
handler: http.handler | ||
events: | ||
- apigw: | ||
method: get |
20 changes: 20 additions & 0 deletions
20
packages/faas-cli-plugin-invoke/test/fixtures/http-scf/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { func, inject, provide } from '@midwayjs/faas'; | ||
|
||
@provide() | ||
@func('http.handler') | ||
export class HelloHttpService { | ||
|
||
@inject() | ||
ctx; // context | ||
|
||
async handler() { | ||
|
||
this.ctx.body = { | ||
headers: this.ctx.headers, | ||
method: this.ctx.method, | ||
path: this.ctx.path, | ||
body: this.ctx.request.body, | ||
params: this.ctx.params | ||
}; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/faas-cli-plugin-invoke/test/fixtures/http-scf/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compileOnSave": true, | ||
"compilerOptions": { | ||
"target": "ES2018", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"inlineSourceMap": true, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"stripInternal": true, | ||
"pretty": true, | ||
"declaration": true, | ||
"outDir": "dist", | ||
"sourceRoot": "" | ||
}, | ||
"exclude": [ | ||
"dist", | ||
"node_modules", | ||
"test" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "@midwayjs/fcli-plugin-scf", | ||
"version": "0.2.74", | ||
"main": "dist/index", | ||
"typings": "dist/index.d.ts", | ||
"dependencies": { | ||
"@midwayjs/fcli-command-core": "^0.2.74", | ||
"@midwayjs/serverless-scf-starter": "^0.2.74", | ||
"@midwayjs/serverless-spec-builder": "^0.2.74", | ||
"serverless-tencent-scf": "^0.1.36" | ||
}, | ||
"devDependencies": { | ||
"@midwayjs/fcli-plugin-package": "^0.2.74", | ||
"fs-extra": "^8.1.0", | ||
"midway-bin": "^2.0.0" | ||
}, | ||
"engines": { | ||
"node": ">= 10" | ||
}, | ||
"files": [ | ||
"src", | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "npm run lint && midway-bin build -c", | ||
"lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts", | ||
"test": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace --timeout=0", | ||
"debug": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace --inspect-brk=9229", | ||
"cov": "NODE_ENV=unittest midway-bin cov --ts", | ||
"clean": "midway-bin clean", | ||
"autod": "midway-bin autod" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { BasePlugin, ICoreInstance, CommandHookCore } from '@midwayjs/fcli-command-core'; | ||
import { join } from 'path'; | ||
import * as Tencent from 'serverless-tencent-scf'; | ||
import { writeWrapper } from '@midwayjs/serverless-spec-builder'; | ||
import { generateFunctionsSpec, generateFunctionsSpecFile } from '@midwayjs/serverless-spec-builder/scf'; | ||
|
||
export class TencentSCFPlugin extends BasePlugin { | ||
core: ICoreInstance; | ||
options: any; | ||
provider = 'tencent'; | ||
servicePath = this.core.config.servicePath; | ||
midwayBuildPath = join(this.servicePath, '.serverless'); | ||
|
||
hooks = { | ||
'package:generateSpec': async () => { | ||
this.core.cli.log('Generate spec file...'); | ||
await generateFunctionsSpecFile( | ||
this.getSpecJson({ | ||
provider: { | ||
stage: 'test', | ||
}, | ||
}), | ||
join(this.midwayBuildPath, 'serverless.yml') | ||
); | ||
}, | ||
'package:generateEntry': async () => { | ||
this.core.cli.log('Generate entry file...'); | ||
this.setGlobalDependencies('@midwayjs/serverless-scf-starter'); | ||
writeWrapper({ | ||
baseDir: this.servicePath, | ||
service: this.core.service, | ||
distDir: this.midwayBuildPath, | ||
starter: '@midwayjs/serverless-scf-starter' | ||
}); | ||
}, | ||
'deploy:deploy': async () => { | ||
// 执行 package 打包 | ||
if (!this.core.service.package) { | ||
this.core.service.package = {}; | ||
} | ||
if (!this.core.service.package.artifact) { | ||
this.core.service.package.artifact = 'artifact.zip'; | ||
} | ||
await this.core.invoke(['package'], true, this.options); | ||
const tencentDeploy = await this.getTencentServerless(this.core.service.package.artifact); | ||
await tencentDeploy.invoke(); | ||
}, | ||
}; | ||
|
||
getSpecJson(coverOptions?: any) { | ||
const service = this.core.service; | ||
if (coverOptions) { | ||
Object.keys(coverOptions).forEach((key: string) => { | ||
if (!service[key]) { | ||
service[key] = {}; | ||
} | ||
Object.assign(service[key], coverOptions[key]); | ||
}); | ||
} | ||
return { | ||
custom: service.custom, | ||
service: service.service, | ||
provider: service.provider, | ||
functions: this.getNotIgnoreFunc(), | ||
resources: service.resources, | ||
package: service.package, | ||
}; | ||
} | ||
|
||
// 获取没有忽略的方法(for 高密度部署) | ||
getNotIgnoreFunc() { | ||
const func = {}; | ||
for (const funcName in this.core.service.functions) { | ||
const funcConf = this.core.service.functions[funcName]; | ||
if (funcConf._ignore) { | ||
continue; | ||
} | ||
func[funcName] = funcConf; | ||
} | ||
return func; | ||
} | ||
|
||
// 设置全局依赖,在package的时候会读取 | ||
setGlobalDependencies(name: string, version?: string) { | ||
if (!this.core.service.globalDependencies) { | ||
this.core.service.globalDependencies = {}; | ||
} | ||
this.core.service.globalDependencies[name] = version || '*'; | ||
} | ||
|
||
async getTencentServerless(artifact) { | ||
Object.assign( | ||
this.core.service, | ||
{ | ||
package: {}, | ||
runtimeExtensions: {}, | ||
}, | ||
await generateFunctionsSpec( | ||
this.getSpecJson({ | ||
provider: { | ||
stage: 'test', | ||
}, | ||
}) | ||
) | ||
); | ||
const midwayServerless: any = new CommandHookCore({ | ||
config: { | ||
servicePath: this.servicePath, | ||
}, | ||
commands: ['deploy'], | ||
service: { | ||
...this.core.service, | ||
plugins: [], | ||
getAllFunctions: () => { | ||
return Object.keys(this.core.service.functions); | ||
}, | ||
getFunction: functionName => { | ||
return this.core.service.functions[functionName]; | ||
}, | ||
package: { | ||
artifact | ||
} | ||
}, | ||
provider: this.provider, | ||
options: { | ||
package: `.serverless/${this.core.service.package.artifact}`, | ||
} | ||
}); | ||
midwayServerless.cliCommands = ['deploy']; | ||
midwayServerless.addPlugin(class DeployPlugin extends BasePlugin { | ||
commands = { | ||
deploy: { lifecycleEvents: ['deploy'] } | ||
}; | ||
}); | ||
midwayServerless.addPlugin(Tencent); | ||
await midwayServerless.ready(); | ||
return midwayServerless; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
service: midway-test | ||
|
||
provider: | ||
name: tencent | ||
|
||
functions: | ||
index: | ||
handler: index.handler | ||
events: | ||
- http: | ||
method: get | ||
path: /http | ||
index2: | ||
handler: index.handler2 | ||
events: | ||
- apigw: | ||
method: get | ||
path: /apigw | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"dependencies": { | ||
"@midwayjs/runtime-engine": "*", | ||
"@midwayjs/faas": "*" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/faas-cli-plugin-scf/test/fixtures/base/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { FaaSContext, func, inject, provide } from '@midwayjs/faas'; | ||
|
||
@provide() | ||
@func('index.handler') | ||
export class HelloService { | ||
|
||
@inject() | ||
ctx: FaaSContext; // context | ||
|
||
async handler(event, obj = {}) { | ||
return 'hello world'; | ||
} | ||
} |
Oops, something went wrong.