diff --git a/README.md b/README.md index ac94300c..f896ecc7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/main.ts b/src/main.ts index 2f1684c7..d0fe0a6e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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...'); diff --git a/src/options.ts b/src/options.ts index 96de71c8..a602b9c6 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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. @@ -92,6 +96,7 @@ class ConfigurableOption { } const PortOption = new ConfigurableOption('port', 'PORT', '8080'); +const HostOption = new ConfigurableOption('host', 'HOST', ''); const FunctionTargetOption = new ConfigurableOption( 'target', 'FUNCTION_TARGET', @@ -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), diff --git a/src/testing.ts b/src/testing.ts index 9cb62c9d..2e3ac258 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -53,6 +53,7 @@ export const getTestServer = (functionName: string): Server => { enableExecutionId: false, timeoutMilliseconds: 0, port: '0', + host: '', target: '', sourceLocation: '', printHelp: false, diff --git a/test/integration/legacy_event.ts b/test/integration/legacy_event.ts index a25250e6..0b7d4ce3 100644 --- a/test/integration/legacy_event.ts +++ b/test/integration/legacy_event.ts @@ -37,6 +37,7 @@ const testOptions = { enableExecutionId: false, timeoutMilliseconds: 0, port: '0', + host: '', target: '', sourceLocation: '', printHelp: false, diff --git a/test/options.ts b/test/options.ts index bf1619d0..f7556a9c 100644 --- a/test/options.ts +++ b/test/options.ts @@ -54,6 +54,7 @@ describe('parseOptions', () => { envVars: {}, expectedOptions: { port: '8080', + host: '', target: 'function', sourceLocation: resolve(''), signatureType: 'http', @@ -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', @@ -92,6 +96,7 @@ 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', @@ -99,6 +104,7 @@ describe('parseOptions', () => { }, expectedOptions: { port: '1234', + host: '0.0.0.0', target: 'helloWorld', sourceLocation: resolve('/source'), signatureType: 'cloudevent', @@ -119,9 +125,12 @@ 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', @@ -129,6 +138,7 @@ describe('parseOptions', () => { }, expectedOptions: { port: '1234', + host: '0.0.0.0', target: 'helloWorld', sourceLocation: resolve('/source'), signatureType: 'cloudevent',