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

Commit

Permalink
fix: set default env to ctx.env
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Apr 13, 2020
1 parent a0d2e1b commit ef732e0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
21 changes: 14 additions & 7 deletions packages/faas/src/starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class FaaSStarter implements IFaaSStarter {

this.loader = new ContainerLoader({
baseDir: this.baseDir,
isTsMode: true, // 用 midway-faas 只能是 ts
isTsMode: true, // 用 midway-faas 只能是 ts
preloadModules: options.preloadModules || [],
});
this.loader.initialize();
Expand Down Expand Up @@ -118,14 +118,15 @@ export class FaaSStarter implements IFaaSStarter {
}

const context: FaaSContext = this.getContext(args.shift());

if (funOptions && funOptions.mod) {
// invoke middleware, just for http
let fnMiddlewere = [];
fnMiddlewere = fnMiddlewere.concat(this.globalMiddleware);
fnMiddlewere = fnMiddlewere.concat(funOptions.middleware);
if (fnMiddlewere.length) {
const mw: any[] = await this.formatMiddlewares(fnMiddlewere);
mw.push(async ctx => {
mw.push(async (ctx) => {
// invoke handler
const result = this.invokeHandler(funOptions, ctx, args);
if (!ctx.body) {
Expand Down Expand Up @@ -173,8 +174,9 @@ export class FaaSStarter implements IFaaSStarter {
return handlerMethod;
}
throw new Error(
`no handler setup on ${target.name}#${method ||
this.defaultHandlerMethod}`
`no handler setup on ${target.name}#${
method || this.defaultHandlerMethod
}`
);
}

Expand Down Expand Up @@ -225,12 +227,12 @@ export class FaaSStarter implements IFaaSStarter {
// or @Func({ handler })
opts.funHandler
: // else use ClassName.mehtod as handler key
covertId(funModule.name, opts.key);
covertId(funModule.name, opts.key);
this.funMappingStore.set(handlerName, {
middleware: opts.middleware || [],
mod: funModule,
method: opts.key,
descriptor: opts.descriptor
descriptor: opts.descriptor,
});
});
}
Expand All @@ -255,9 +257,14 @@ export class FaaSStarter implements IFaaSStarter {
if (!context.requestContext) {
context.requestContext = new MidwayRequestContainer(
context,
this.loader.getApplicationContext()
this.getApplicationContext()
);
}
if (!context.env) {
context.env = this.getApplicationContext()
.getEnvironmentService()
.getCurrentEnvironment();
}
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class HelloService implements FunctionHandler {
'userManager.getUser should be ok'
);

return this.allConfig + event.text + (await this.articleManager.getOne());
return (
this.allConfig +
event.text +
((await this.articleManager.getOne()) + this.ctx.env)
);
}
}
2 changes: 1 addition & 1 deletion packages/faas/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('test/index.test.ts', () => {
{},
{ text: 'ab' }
);
assert(data === '5321abone articlereplace manager');
assert(data === '5321abone articlereplace managerprod');
mm.restore();
});
});

0 comments on commit ef732e0

Please sign in to comment.