Skip to content

feat: added new option to change the server host #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,11 @@ ignored.
| Command-line flag | Environment variable | Description |
| ------------------ | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080` |
| `--host` | `HOST` | The host on which the Functions Framework listens for requests. Default: `''` [Check node documentation for the default behavior](https://nodejs.org/api/net.html#serverlistenport-host-backlog-callback) |
| `--target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function` |
| `--signature-type` | `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Default: `http`; accepted values: `http` or `event` or `cloudevent` |
| `--source` | `FUNCTION_SOURCE` | The path to the directory of your function. Default: `cwd` (the current working directory) |
| `--log-execution-id`| `LOG_EXECUTION_ID` | Enables execution IDs in logs, either `true` or `false`. When not specified, default to disable. Requires Node.js 13.0.0 or later. |
| `--log-execution-id`| `LOG_EXECUTION_ID` | Enables execution IDs in logs, either `true` or `false`. When not specified, default to disable. Requires Node.js 13.0.0 or later. |

You can set command-line flags in your `package.json` via the `start` script.
For example:
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ export const main = async () => {
options.signatureType = signatureType;
const server = getServer(userFunction!, options);
const errorHandler = new ErrorHandler(server);
const {host, port} = options;

const serverOptions = {
port,
...(host && {host}),
};
server
.listen(options.port, () => {
.listen(serverOptions, () => {
errorHandler.register();
if (process.env.NODE_ENV !== 'production') {
console.log('Serving function...');
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface FrameworkOptions {
* The port on which this server listens to all HTTP requests.
*/
port: string;
/**
* The host on which this server listens for all HTTP requests.
*/
host: string;
/**
* The name of the function within user's node module to execute. If such a
* function is not defined, then falls back to 'function' name.
Expand Down Expand Up @@ -92,6 +96,7 @@ class ConfigurableOption<T> {
}

const PortOption = new ConfigurableOption('port', 'PORT', '8080');
const HostOption = new ConfigurableOption('host', 'HOST', '');
const FunctionTargetOption = new ConfigurableOption(
'target',
'FUNCTION_TARGET',
Expand Down Expand Up @@ -185,6 +190,7 @@ export const parseOptions = (
});
return {
port: PortOption.parse(argv, envVars),
host: HostOption.parse(argv, envVars),
target: FunctionTargetOption.parse(argv, envVars),
sourceLocation: SourceLocationOption.parse(argv, envVars),
signatureType: SignatureOption.parse(argv, envVars),
Expand Down
1 change: 1 addition & 0 deletions src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const getTestServer = (functionName: string): Server => {
enableExecutionId: false,
timeoutMilliseconds: 0,
port: '0',
host: '',
target: '',
sourceLocation: '',
printHelp: false,
Expand Down
1 change: 1 addition & 0 deletions test/integration/legacy_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const testOptions = {
enableExecutionId: false,
timeoutMilliseconds: 0,
port: '0',
host: '',
target: '',
sourceLocation: '',
printHelp: false,
Expand Down
10 changes: 10 additions & 0 deletions test/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('parseOptions', () => {
envVars: {},
expectedOptions: {
port: '8080',
host: '',
target: 'function',
sourceLocation: resolve(''),
signatureType: 'http',
Expand All @@ -75,10 +76,13 @@ describe('parseOptions', () => {
'--source=/source',
'--timeout',
'6',
'--host',
'0.0.0.0',
],
envVars: {},
expectedOptions: {
port: '1234',
host: '0.0.0.0',
target: 'helloWorld',
sourceLocation: resolve('/source'),
signatureType: 'cloudevent',
Expand All @@ -92,13 +96,15 @@ describe('parseOptions', () => {
cliOpts: ['bin/node', '/index.js'],
envVars: {
PORT: '1234',
HOST: '0.0.0.0',
FUNCTION_TARGET: 'helloWorld',
FUNCTION_SIGNATURE_TYPE: 'cloudevent',
FUNCTION_SOURCE: '/source',
CLOUD_RUN_TIMEOUT_SECONDS: '2',
},
expectedOptions: {
port: '1234',
host: '0.0.0.0',
target: 'helloWorld',
sourceLocation: resolve('/source'),
signatureType: 'cloudevent',
Expand All @@ -119,16 +125,20 @@ describe('parseOptions', () => {
'cloudevent',
'--source=/source',
'--timeout=3',
'--host',
'0.0.0.0',
],
envVars: {
PORT: '4567',
HOST: '127.0.0.1',
FUNCTION_TARGET: 'fooBar',
FUNCTION_SIGNATURE_TYPE: 'event',
FUNCTION_SOURCE: '/somewhere/else',
CLOUD_RUN_TIMEOUT_SECONDS: '5',
},
expectedOptions: {
port: '1234',
host: '0.0.0.0',
target: 'helloWorld',
sourceLocation: resolve('/source'),
signatureType: 'cloudevent',
Expand Down