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

Commit

Permalink
fix: support apigw
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar authored and czy88840616 committed Apr 14, 2020
1 parent d2a30ca commit 2321c08
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
35 changes: 35 additions & 0 deletions packages/faas-cli-plugin-invoke/test/apigw.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { invoke } from '../src/index';
import { join } from 'path';
import * as assert from 'assert';
import { remove } from 'fs-extra';
describe('/test/apigw.test.ts', () => {
it('invoke', async () => {
const baseDir = join(__dirname, 'fixtures/http');
await remove(join(baseDir, './.faas_debug_tmp'));
const result: any = await invoke({
functionDir: baseDir,
functionName: 'http',
trigger: 'apigw',
data: [
{
headers: {
'Content-Type': 'text/json'
},
method: 'POST',
query: {
q: 'testq'
},
pathParameters: {
id: 'id'
},
path: '/test',
body: {
name: 'test'
}
}
]
});
const resultBody = JSON.parse(result.body);
assert(resultBody.headers['Content-Type'] === 'text/json' && resultBody.method === 'POST' && resultBody.path === '/test' && resultBody.body.name === 'test' && resultBody.pathParameters.id === 'id');
});
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { FaaSContext, func, inject, provide } from '@midwayjs/faas';
import { func, inject, provide } from '@midwayjs/faas';

@provide()
@func('http.handler')
export class HelloHttpService {

@inject()
ctx: FaaSContext; // context
ctx; // context

async handler() {
return {
headers: this.ctx.request.headers,
method: this.ctx.request.method,
path: this.ctx.request.path,
body: this.ctx.request.body
body: this.ctx.request.body,
pathParameters: this.ctx.request.pathParameters
}
}
}
8 changes: 8 additions & 0 deletions packages/serverless-fc-starter/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class Request {
return this[EVENT].path;
}

get pathParameters() {
return this[EVENT].pathParameters;
}

get method() {
return this[EVENT].method;
}
Expand Down Expand Up @@ -152,6 +156,10 @@ export class Context {
return this.req.path;
}

get pathParameters() {
return this.req.pathParameters;
}

get query() {
return this.req.query;
}
Expand Down
54 changes: 54 additions & 0 deletions packages/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export class FCRuntime extends ServerlessLightRuntime {
try {
event = JSON.parse(event);
} catch (_err) {}
if (event && event.headers && event.headers['X-Ca-Api-Gateway']) {
return this.wrapperApiGwEventInvoke(handler, event, context);
}
}
args.push(event);
}
Expand All @@ -133,4 +136,55 @@ export class FCRuntime extends ServerlessLightRuntime {
async beforeInvokeHandler(context) {}

async afterInvokeHandler(err, result, context) {}

async wrapperApiGwEventInvoke(handler, req, context) {
const ctx: any = new Context({
...req,
method: req.httpMethod,
query: req.queryParameters,
}, {}, context);
ctx.EventType = 'fc_apigw';
const args = [ctx];
return this.invokeHandlerWrapper(ctx, async () => {
if (!handler) {
return this.defaultInvokeHandler.apply(this, args);
}
return handler.apply(handler, args);
}).then((result) => {
if (result) {
ctx.body = result;
}
const data = ctx.body;
let encoded = false;
if (typeof data === 'string') {
if (!ctx.type) {
ctx.type = 'text/plain';
}
ctx.body = data;
} else if (Buffer.isBuffer(data)) {
encoded = true;
if (!ctx.type) {
ctx.type = 'application/octet-stream';
}
ctx.body = data.toString('base64');
} else if (typeof data === 'object') {
if (!ctx.type) {
ctx.type = 'application/json';
}
ctx.body = JSON.stringify(data);
} else {
// 阿里云网关必须返回字符串
if (!ctx.type) {
ctx.type = 'text/plain';
}
ctx.body = data + '';
}
return {
isBase64Encoded: encoded,
statusCode: ctx.status,
headers: ctx.res.headers,
body: ctx.body,
};
});
}
}
21 changes: 10 additions & 11 deletions packages/serverless-fc-trigger/src/apiGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ export class ApiGatewayTrigger extends FCBaseTrigger {
handler;

async toArgs(): Promise<any[]> {
const event = Object.assign(
const event = Buffer.from(JSON.stringify(Object.assign(
{
path: 'api request path',
httpMethod: 'request method name',
headers: {},
queryParameters: {},
pathParameters: {},
body: '',
isBase64Encoded: 'false',
},
this.triggerOptions
);
path: this.triggerOptions.path || 'api request path',
httpMethod: this.triggerOptions.method || 'request method name',
headers: Object.assign({'X-Ca-Api-Gateway': 'test-id'}, this.triggerOptions.headers),
queryParameters: this.triggerOptions.query || {},
pathParameters: this.triggerOptions.pathParameters || {},
body: this.triggerOptions.body || '',
isBase64Encoded: false,
}
)));

return [event, this.createContext()];
}
Expand Down

0 comments on commit 2321c08

Please sign in to comment.