diff --git a/README.md b/README.md index 1724f7b8..fbd5b82a 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,9 @@ const helloFunction = func }); app.addFunctions(helloFunction); -app.register(expressPlugin()); + +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); ``` diff --git a/examples/azure-functions-minimal/src/main.ts b/examples/azure-functions-minimal/src/main.ts index 229645e1..f9e845e7 100644 --- a/examples/azure-functions-minimal/src/main.ts +++ b/examples/azure-functions-minimal/src/main.ts @@ -1,20 +1,22 @@ -import { AzureFunctionsAdapter, initNammatham, expressPlugin } from "nammatham"; +import { AzureFunctionsAdapter, initNammatham, expressPlugin } from 'nammatham'; const n = initNammatham.create(new AzureFunctionsAdapter()); const func = n.func; const app = n.app; -const helloFunction = func - .httpGet('hello', { - route: 'hello-world', - }) - .handler(async ({trigger, context}) => { - context.log('HTTP trigger function processed a request.'); - context.debug(`Http function processed request for url "${trigger.url}"`); - const name = trigger.query.get('name') || (await trigger.text()) || 'world'; - return { body: `Hello, ${name}!` }; - }); +app.addFunctions( + func + .httpGet('hello', { + route: 'hello-world', + }) + .handler(async ({ trigger, context }) => { + context.log('HTTP trigger function processed a request.'); + context.debug(`Http function processed request for url "${trigger.url}"`); + const name = trigger.query.get('name') || (await trigger.text()) || 'world'; + return { body: `Hello, ${name}!` }; + }) +); -app.addFunctions(helloFunction); -app.register(expressPlugin()); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/examples/azure-functions-timer-trigger/src/main.ts b/examples/azure-functions-timer-trigger/src/main.ts index e989672b..463657e6 100644 --- a/examples/azure-functions-timer-trigger/src/main.ts +++ b/examples/azure-functions-timer-trigger/src/main.ts @@ -4,8 +4,11 @@ import { app } from './nammatham'; app.addFunctions(simpleTimer); +const dev = process.env.NODE_ENV === 'development'; + app.register( expressPlugin({ + dev, allowAllFunctionsAccessByHttp: true, }) ); diff --git a/examples/azure-functions-with-inversify/src/main.ts b/examples/azure-functions-with-inversify/src/main.ts index b455628e..a116f49b 100644 --- a/examples/azure-functions-with-inversify/src/main.ts +++ b/examples/azure-functions-with-inversify/src/main.ts @@ -5,5 +5,6 @@ import { app } from './nammatham'; app.addFunctions(hello); -app.register(expressPlugin()); +const dev =process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/examples/azure-functions-with-test/src/main.ts b/examples/azure-functions-with-test/src/main.ts index 9857a2e1..46c7f2ca 100644 --- a/examples/azure-functions-with-test/src/main.ts +++ b/examples/azure-functions-with-test/src/main.ts @@ -4,5 +4,6 @@ import { app } from './nammatham'; app.addFunctions(hello); -app.register(expressPlugin()); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/examples/azure-functions/src/main.ts b/examples/azure-functions/src/main.ts index 02c03d4c..f01344ea 100644 --- a/examples/azure-functions/src/main.ts +++ b/examples/azure-functions/src/main.ts @@ -5,5 +5,6 @@ import { app } from './nammatham'; app.addFunctions(blob, hello); -app.register(expressPlugin()); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/packages/azure-functions/src/handler-resolver.ts b/packages/azure-functions/src/handler-resolver.ts index c62bad12..1ffe3273 100644 --- a/packages/azure-functions/src/handler-resolver.ts +++ b/packages/azure-functions/src/handler-resolver.ts @@ -149,7 +149,7 @@ export class AzureFunctionsHandlerResolver extends BaseHandlerResolver { logger.debug(`Running with runtime: ${app.runtime}`); logger.debug(`runtime: ${app.runtime}, isDevelopment: ${app.isDevelopment}`); - if (app.runtime === 'express' && process.env.NODE_ENV !== 'development') { + if (app.runtime === 'express' && app.isDevelopment === false) { throw new Error( `expressPlugin will not start express server in production mode for Azure Functions Adapter, because Azure Functions will start the server for us. Please make set isDevelopment to be 'false' when use expressPlugin in production mode.` diff --git a/packages/core/src/nammatham-app.ts b/packages/core/src/nammatham-app.ts index a7fbfee8..dbaeb672 100644 --- a/packages/core/src/nammatham-app.ts +++ b/packages/core/src/nammatham-app.ts @@ -24,7 +24,7 @@ export class NammathamApp { * For example, expressPlugin will not start express server in production mode for Azure Functions Adapter, * because Azure Functions will start the server for us. */ - private _isDevelopment: boolean | undefined; + private _isDevelopment = false; public readonly startTime = performance.now(); constructor(public readonly handlerResolver: BaseHandlerResolver) {} @@ -37,7 +37,7 @@ export class NammathamApp { logger.debug('Registering functions...'); await this.handlerResolver.resolveRegisterHandler(this); logger.debug('All functions registered'); - console.log(`${logo()} \n`); + console.log(`${await logo()} \n`); } addEndpoint(func: NammamthamEndpoint) { diff --git a/packages/express/src/express-server.ts b/packages/express/src/express-server.ts index ba9c7d29..b8dcccc4 100644 --- a/packages/express/src/express-server.ts +++ b/packages/express/src/express-server.ts @@ -12,7 +12,7 @@ export interface ExpressServerOption { prefix?: string; port?: number; expressApp?: express.Express; - isDevelopment?: boolean; + dev?: boolean; allowAllFunctionsAccessByHttp?: boolean; } @@ -21,13 +21,16 @@ export interface ExpressServerOption { */ export function expressPlugin(option?: ExpressServerOption) { return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => { - const isDevelopment = option?.isDevelopment ?? process.env.NODE_ENV === 'development'; - if (!isDevelopment) { - logger.debug('Skipping express server'); + const isDevelopment = option?.dev ?? false; + app.setDevelopment(isDevelopment); + console.log(app.runtime, 'runtime') + console.log(isDevelopment, 'isDevelopment') + if (isDevelopment === false && app.runtime === 'azure-functions') { return; + } else { + logger.info('Starting express server in development mode'); } app.setRuntime('express'); - app.setDevelopment(isDevelopment); logger.debug(`Using plugin: expressPlugin`); startExpress( { diff --git a/packages/trpc-azure-functions/src/trpc-azure-functions.ts b/packages/trpc-azure-functions/src/trpc-azure-functions.ts index b420700a..71184768 100644 --- a/packages/trpc-azure-functions/src/trpc-azure-functions.ts +++ b/packages/trpc-azure-functions/src/trpc-azure-functions.ts @@ -45,9 +45,9 @@ export function unstable__tRpcAzureFunctionsPlugin( }); app.addFunction(trpcFunction); - const isDevelopment = option?.expressPluginOption?.isDevelopment ?? process.env.NODE_ENV === 'development'; + const isDevelopment = option?.expressPluginOption?.dev ?? false; if (!isDevelopment) { - logger.debug('Skipping express server in development mode (trpc)'); + logger.debug('Skipping express server in production mode (trpc)'); return; }