Skip to content

Commit

Permalink
Explicitly use of dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Jan 21, 2024
1 parent 168a7ec commit e0675d5
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

Expand Down
28 changes: 15 additions & 13 deletions examples/azure-functions-minimal/src/main.ts
Original file line number Diff line number Diff line change
@@ -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();
3 changes: 3 additions & 0 deletions examples/azure-functions-timer-trigger/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { app } from './nammatham';

app.addFunctions(simpleTimer);

const dev = process.env.NODE_ENV === 'development';

app.register(
expressPlugin({
dev,
allowAllFunctionsAccessByHttp: true,
})
);
Expand Down
3 changes: 2 additions & 1 deletion examples/azure-functions-with-inversify/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
3 changes: 2 additions & 1 deletion examples/azure-functions-with-test/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
3 changes: 2 additions & 1 deletion examples/azure-functions/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion packages/azure-functions/src/handler-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 152 in packages/azure-functions/src/handler-resolver.ts

View check run for this annotation

Codecov / codecov/patch

packages/azure-functions/src/handler-resolver.ts#L152

Added line #L152 was not covered by tests
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.`
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/nammatham-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning on line 27 in packages/core/src/nammatham-app.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/nammatham-app.ts#L27

Added line #L27 was not covered by tests
public readonly startTime = performance.now();

constructor(public readonly handlerResolver: BaseHandlerResolver) {}
Expand All @@ -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`);

Check warning on line 40 in packages/core/src/nammatham-app.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/nammatham-app.ts#L40

Added line #L40 was not covered by tests
}

addEndpoint(func: NammamthamEndpoint) {
Expand Down
13 changes: 8 additions & 5 deletions packages/express/src/express-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ExpressServerOption {
prefix?: string;
port?: number;
expressApp?: express.Express;
isDevelopment?: boolean;
dev?: boolean;

Check warning on line 15 in packages/express/src/express-server.ts

View check run for this annotation

Codecov / codecov/patch

packages/express/src/express-server.ts#L15

Added line #L15 was not covered by tests
allowAllFunctionsAccessByHttp?: boolean;
}

Expand All @@ -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') {

Check warning on line 28 in packages/express/src/express-server.ts

View check run for this annotation

Codecov / codecov/patch

packages/express/src/express-server.ts#L24-L28

Added lines #L24 - L28 were not covered by tests
return;
} else {
logger.info('Starting express server in development mode');

Check warning on line 31 in packages/express/src/express-server.ts

View check run for this annotation

Codecov / codecov/patch

packages/express/src/express-server.ts#L30-L31

Added lines #L30 - L31 were not covered by tests
}
app.setRuntime('express');
app.setDevelopment(isDevelopment);
logger.debug(`Using plugin: expressPlugin`);
startExpress(
{
Expand Down
4 changes: 2 additions & 2 deletions packages/trpc-azure-functions/src/trpc-azure-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export function unstable__tRpcAzureFunctionsPlugin<TRouter extends AnyRouter>(
});
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;
}

Expand Down

0 comments on commit e0675d5

Please sign in to comment.