Skip to content

Commit

Permalink
feat: add hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Jan 22, 2024
1 parent 70630da commit 4ac0236
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/azure-functions/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe(`Test utils: ${utils.getFullUrl.name}`, () => {
} as any;

// Act
const result = utils.getFullUrl(func, 3000);
const result = utils.getFullUrl(func, 'localhost', 3000);

// Assert
expect(result).toEqual('http://localhost:3000/api/test');
Expand All @@ -58,7 +58,7 @@ describe(`Test utils: ${utils.getFullUrl.name}`, () => {
} as any;

// Act
const result = utils.getFullUrl(func, 3000);
const result = utils.getFullUrl(func, 'localhost', 3000);

// Assert
expect(result).toEqual('http://localhost:3000/api/test');
Expand All @@ -71,7 +71,7 @@ describe(`Test utils: ${utils.getFullUrl.name}`, () => {
} as any;

// Act
const result = utils.getFullUrl(func, 3000);
const result = utils.getFullUrl(func, '', 3000);

// Assert
expect(result).toEqual('');
Expand Down
10 changes: 5 additions & 5 deletions packages/azure-functions/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AfterServerStartedMetadata, NammathamApp } from '@nammatham/core';

import { gray, yellow } from 'colorette';
import { logger, trimSlash } from '@nammatham/core';
import { trimSlash } from '@nammatham/core';

import type { AzureFunctionsEndpoint } from './types';

Expand All @@ -13,10 +13,10 @@ export function getMethods(func: AzureFunctionsEndpoint<unknown, unknown>): stri
return methods;
}

export function getFullUrl(func: AzureFunctionsEndpoint<unknown, unknown>, port?: number): string {
export function getFullUrl(func: AzureFunctionsEndpoint<unknown, unknown>, hostname: string, port?: number): string {
const endpoint = func.endpointOption?.route ?? func.name;
if (typeof endpoint !== 'string') return '';
return `http://localhost${port ? `:${port}` : ''}/api/${trimSlash(endpoint)}`;
return `http://${hostname}${port ? `:${port}` : ''}/api/${trimSlash(endpoint)}`;
}

export const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
Expand All @@ -32,7 +32,7 @@ export async function printRegisteredFunctions(
console.log(`\n${yellow('Functions:')}\n`);
for (const func of azureFunctions) {
const methods = `[${getMethods(func).join(',')}]`;
console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.port))}`);
console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.hostname, option.port))}`);
}
return azureFunctions;
}
Expand All @@ -51,7 +51,7 @@ export async function printRegisteredNonHttpFunctions(
console.log(`\n${yellow('Non-HTTP Functions, accessed by HTTP (dev mode):')}\n`);
for (const func of azureFunctions) {
const type = `[${func.endpointOption?.type ?? 'generic'}]`;
console.log(` - ${func.name} ${gray(type)} ${gray(getFullUrl(func, option.port))}`);
console.log(` - ${func.name} ${gray(type)} ${gray(getFullUrl(func, option.hostname, option.port))}`);
}
return azureFunctions;
}
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export interface GenericEndpointOption extends EndpointOptionBase, Record<string
type: 'generic';
}

export type UnknownEndpointOption = EndpointOptionBase & Record<string, unknown>;
// export type UnknownEndpointOption = EndpointOptionBase & Record<string, unknown>;

export type EndpointOption = HttpEndpointOption | GenericEndpointOption | UnknownEndpointOption;
export type EndpointOption = HttpEndpointOption | GenericEndpointOption;

export type WithEndpointOption = { endpointOption?: EndpointOption };

Expand Down
19 changes: 12 additions & 7 deletions packages/express/src/express-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { createExpressMiddleware } from './middleware';
export interface ExpressServerOption {
prefix?: string;
port?: number;
/**
* @default localhost
*/
hostname?: string;
expressApp?: express.Express;
dev?: boolean;
allowAllFunctionsAccessByHttp?: boolean;
Expand All @@ -23,8 +27,6 @@ export function expressPlugin(option?: ExpressServerOption) {
return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => {
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 {
Expand All @@ -49,6 +51,7 @@ export function startExpress(
logger.debug('Starting express server');
const expressApp = expressOption?.expressApp ?? express();
const port = expressOption?.port ?? 3000;
const hostname = expressOption?.hostname ?? 'localhost';
const prefix = expressOption?.prefix ?? '/api';
const allowAllFunctionsAccessByHttp = expressOption?.allowAllFunctionsAccessByHttp ?? false;

Expand All @@ -66,17 +69,19 @@ export function startExpress(
})
);

expressApp.listen(port, async () => {
expressApp.listen(port, hostname, async () => {
console.clear();
const endTime = performance.now();
const durationMs = Math.floor(endTime - app.startTime);
logger.debug(`Server started at http://localhost:${port}`);
const hostType = hostname === 'localhost' ? 'Local' : 'Host';
// const host = hostname === 'localhost' ? gray('Not Available') : greenBright(`http://${hostname}:${port}`);
logger.debug(`Server started at http://${hostname}:${port}`);
console.log(`${await logo()} ${gray(`ready in ${durationMs} ms`)}\n`);
console.log(`\n${blue('Express server started')}\n`);
console.log(` ┃ Local ${greenBright(`http://localhost:${port}`)}`);
console.log(` ┃ Host ${gray('Not Available')} \n`);
// console.log(` ┃ Local ${greenBright(`http://localhost:${port}`)}`);
console.log(` ┃ ${hostType} ${greenBright(`http://${hostname}:${port}`)} \n`);

await handlerResolver.afterServerStarted(app, { port, allowAllFunctionsAccessByHttp });
await handlerResolver.afterServerStarted(app, { port, hostname, allowAllFunctionsAccessByHttp });
console.log('\n');
});
}

0 comments on commit 4ac0236

Please sign in to comment.