Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
fix: Refactor/invoke (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar authored Jun 16, 2020
1 parent e786f56 commit 37dd34f
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1589535981255
1592281077048
60 changes: 34 additions & 26 deletions packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ enum LOCK_TYPE {
COMPLETE,
}

function getPlatformPath(p) {
if (type() === 'Windows_NT') {
return p.replace(/\\/g, '\\\\');
}
return p;
}

export class FaaSInvokePlugin extends BasePlugin {
baseDir: string;
buildDir: string;
Expand Down Expand Up @@ -388,15 +381,7 @@ export class FaaSInvokePlugin extends BasePlugin {
const { name, fileName, userEntry } = this.checkUserEntry();
if (!userEntry) {
const isTsMode = this.checkIsTsMode();
let starterName;
const platform = this.getPlatform();
this.core.debug('Platform entry', platform);
if (platform === 'aliyun') {
starterName = require.resolve('@midwayjs/serverless-fc-starter');
} else if (platform === 'tencent') {
starterName = require.resolve('@midwayjs/serverless-scf-starter');
}

const starterName = this.getStarterName();
if (!starterName) {
return;
}
Expand All @@ -407,10 +392,11 @@ export class FaaSInvokePlugin extends BasePlugin {
layers: this.core.service.layers,
functions: this.core.service.functions,
},
faasModName: process.env.MidwayModuleName,
distDir: this.buildDir,
starter: getPlatformPath(starterName),
starter: this.getPlatformPath(starterName),
loadDirectory: isTsMode
? [getPlatformPath(resolve(this.defaultTmpFaaSOut, 'src'))]
? [this.getPlatformPath(resolve(this.defaultTmpFaaSOut, 'src'))]
: [],
});
if (isTsMode) {
Expand All @@ -432,12 +418,21 @@ export class FaaSInvokePlugin extends BasePlugin {
this.core.debug('EntryInfo', this.entryInfo);
}

getStarterName() {
const platform = this.getPlatform();
this.core.debug('Platform entry', platform);
if (platform === 'aliyun') {
return require.resolve('@midwayjs/serverless-fc-starter');
} else if (platform === 'tencent') {
return require.resolve('@midwayjs/serverless-scf-starter');
}
}

async getInvoke() {
let handler;
let initHandler;
let runtime;
let invoke;
const platform = this.getPlatform();
if (this.entryInfo) {
try {
const handlerMod = require(this.entryInfo.fileName);
Expand All @@ -462,13 +457,7 @@ export class FaaSInvokePlugin extends BasePlugin {
if (runtime) {
this.core.debug('Have Runtime');
invoke = async (...args) => {
let triggerMap;
if (platform === 'aliyun') {
triggerMap = FCTrigger;
} else if (platform === 'tencent') {
triggerMap = SCFTrigger;
}
const trigger = this.getTrigger(triggerMap, args);
const trigger = await this.getTriggerInfo(args);
await runtime.start();
this.core.debug('Invoke', trigger);
const result = await runtime.invoke(...trigger);
Expand All @@ -482,6 +471,17 @@ export class FaaSInvokePlugin extends BasePlugin {
this.invokeFun = invoke;
}

async getTriggerInfo(args) {
const platform = this.getPlatform();
let triggerMap;
if (platform === 'aliyun') {
triggerMap = FCTrigger;
} else if (platform === 'tencent') {
triggerMap = SCFTrigger;
}
return this.getTrigger(triggerMap, args);
}

async callInvoke() {
const resultType = this.options.resultType;
this.core.debug('ResultType', resultType);
Expand Down Expand Up @@ -549,6 +549,7 @@ export class FaaSInvokePlugin extends BasePlugin {
return 'tencent';
}
}
return provider;
}

getFunctionInfo() {
Expand Down Expand Up @@ -600,4 +601,11 @@ export class FaaSInvokePlugin extends BasePlugin {
// eslint-disable-next-line node/no-deprecated-api
return !!require.extensions['.ts'];
}

getPlatformPath(p) {
if (type() === 'Windows_NT') {
return p.replace(/\\/g, '\\\\');
}
return p;
}
}
6 changes: 6 additions & 0 deletions packages/faas-cli-plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,12 @@ export class PackagePlugin extends BasePlugin {
// }

this.core.cli.log('Aggregation Deploy');

// use picomatch to match url
if (!this.core.service.globalDependencies) {
this.core.service.globalDependencies = {};
}
this.core.service.globalDependencies['picomatch'] = '*';
const allAggregationPaths = [];
let allFuncNames = Object.keys(this.core.service.functions);
for (const aggregationName in this.core.service.aggregation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ functions:
- http:
method: get
path: /api/3
apiall:
handler: apiall.handler
events:
- http:
method: get
path: /api/*
render:
handler: render.handler
events:
Expand Down Expand Up @@ -55,7 +61,7 @@ functions:

custom:
customDomain:
domainName: 'aggregation.exanple.com'
domainName: 'fctest.iam.gy'

aggregation:
api:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,58 @@
import { FaaSContext, func, inject, provide } from '@midwayjs/faas';

@provide()
@func('index.handler')
export class HelloService {

@inject()
ctx: FaaSContext; // context

@func('index.handler')
async handler(event, obj = {}) {
return 'hello world';
}

@func('apiall.handler')
async apiall(event, obj = {}) {
return 'apiall:' + this.ctx.path;
}

@func('api1.handler')
async api1(event, obj = {}) {
return 'api1:' + this.ctx.path;;
}

@func('api2.handler')
async api2(event, obj = {}) {
return 'api2:' + this.ctx.path;;
}

@func('api3.handler')
async api3(event, obj = {}) {
return 'api3:' + this.ctx.path;;
}

@func('render.handler')
async render(event, obj = {}) {
return 'render:' + this.ctx.path;;
}

@func('render1.handler')
async render1(event, obj = {}) {
return 'render1:' + this.ctx.path;;
}

@func('render2.handler')
async render2(event, obj = {}) {
return 'render2:' + this.ctx.path;;
}

@func('normal1.handler')
async normal1(event, obj = {}) {
return 'normal1:' + this.ctx.path;;
}

@func('normal2.handler')
async normal2(event, obj = {}) {
return 'normal2:' + this.ctx.path;;
}
}
29 changes: 15 additions & 14 deletions packages/serverless-spec-builder/src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'path';
import { writeFileSync, existsSync } from 'fs';
import { render } from 'ejs';
import { getLayers } from './utils';
export const wrapperContent = `const { FaaSStarter } = require('@midwayjs/faas');
export const wrapperContent = `const { FaaSStarter } = require('<%=faasModName %>');
const { asyncWrapper, start } = require('<%=starter %>');
<% layerDeps.forEach(function(layer){ %>const <%=layer.name%> = require('<%=layer.path%>');
<% }); %>
Expand Down Expand Up @@ -34,24 +34,23 @@ exports.<%=handlerData.name%> = asyncWrapper(async (...args) => {
<% if (handlerData.handler) { %>
return runtime.asyncEvent(starter.handleInvokeWrapper('<%=handlerData.handler%>'))(...args);
<% } else { %>
const picomatch = require('picomatch');
const allHandlers = <%-JSON.stringify(handlerData.handlers)%>;
return runtime.asyncEvent(async (ctx) => {
let handler = null;
let ctxPath = ctx && ctx.path || '';
if (ctxPath) {
handler = allHandlers.find(handler => {
return ctxPath.indexOf(handler.path) === 0;
return picomatch.isMatch(ctxPath, handler.router)
});
}
if (!handler) {
handler = allHandlers[allHandlers.length - 1];
}
if (handler) {
return starter.handleInvokeWrapper(handler.handler)(ctx);
}
return 'unhandler path: ' + ctxPath + '; handlerInfo: ' + JSON.stringify(allHandlers);
ctx.status = 404;
ctx.set('Content-Type', 'text/html');
return '<h1>404 Page Not Found</h1><hr />Request path: ' + ctxPath + '<hr /><div style="font-size: 12px;color: #999999;">Powered by <a href="https://github.com/midwayjs/midway-faas">Midway</a></div>';
})(...args);
<% } %>
});
Expand All @@ -65,13 +64,15 @@ export function writeWrapper(options: {
starter: string;
cover?: boolean;
loadDirectory?: string[];
faasModName?: string;
}) {
const {
service,
distDir,
starter,
baseDir,
cover,
faasModName,
loadDirectory = [],
} = options;
const files = {};
Expand Down Expand Up @@ -114,6 +115,7 @@ export function writeWrapper(options: {
const layers = getLayers(service.layers, ...files[file].originLayers);
const content = render(wrapperContent, {
starter,
faasModName: faasModName || '@midwayjs/faas',
loadDirectory,
handlers: files[file].handlers,
...layers,
Expand All @@ -128,18 +130,17 @@ export function formetAggregationHandlers(handlers) {
}
return handlers
.map(handler => {
const path = handler.path.replace(/\**$/, '');
return {
handler: handler.handler,
path,
level: path.split('/').length - 1,
router: handler.path.replace(/\*/g, '**'), // picomatch use **
pureRouter: handler.path.replace(/\**$/, ''),
level: handler.path.split('/').length - 1,
};
})
.sort((handlerA, handlerB) => {
const levelDiff = handlerB.level - handlerA.level;
if (levelDiff === 0) {
return handlerB.path.length - handlerA.path.length;
if (handlerA.pureRouter === handlerB.pureRouter) {
return handlerA.router.length - handlerB.router.length;
}
return levelDiff;
return handlerB.level - handlerA.level;
});
}
11 changes: 4 additions & 7 deletions packages/serverless-spec-builder/test/fixtures/wrapper/aggre.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const initializeMethod = async (initializeContext = {}) => {
runtime = await start({
layers: []
});
starter = new FaaSStarter({ baseDir: __dirname, initializeContext });
starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime });

await starter.start();
inited = true;
Expand All @@ -26,20 +26,17 @@ exports.handler = asyncWrapper(async (...args) => {
await initializeMethod();
}

const allHandlers = [{"handler":"index.handler","path":"/api/test","level":2},{"handler":"render.handler","path":"/","level":1}];
const picomatch = require('picomatch');
const allHandlers = [{"handler":"index.handler","router":"/api/test","pureRouter":"/api/test","level":2},{"handler":"render.handler","router":"/*","pureRouter":"/","level":1}];
return runtime.asyncEvent(async (ctx) => {
let handler = null;
let ctxPath = ctx && ctx.path || '';
if (ctxPath) {
handler = allHandlers.find(handler => {
return ctxPath.indexOf(handler.path) === 0;
return picomatch.isMatch(ctxPath, handler.router)
});
}

if (!handler) {
handler = allHandlers[allHandlers.length - 1];
}

if (handler) {
return starter.handleInvokeWrapper(handler.handler)(ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const initializeMethod = async (initializeContext = {}) => {
runtime = await start({
layers: []
});
starter = new FaaSStarter({ baseDir: __dirname, initializeContext });
starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime });

await starter.start();
inited = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const initializeMethod = async (initializeContext = {}) => {
runtime = await start({
layers: []
});
starter = new FaaSStarter({ baseDir: __dirname, initializeContext });
starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime });

await starter.start();
inited = true;
Expand Down

0 comments on commit 37dd34f

Please sign in to comment.