Skip to content

Commit

Permalink
fix #4: Removes 'function' prefix from CLI args (#16)
Browse files Browse the repository at this point in the history
* fix #4: Removes 'function' prefix from CLI arg and env vars

* fix: Re-add 'FUNCTION_' to env var prefix

* fix: Fix flag/env setup

* fix: docs comment

* fix: Update FUNCTION_ prefix for env var but not consts
  • Loading branch information
grant authored and swalkowski committed Apr 17, 2019
1 parent 29354fb commit 76f134b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ exports.helloWorld = (req, res) => {
Run the following command:

```sh
npx @google-cloud/functions-framework --function-target=helloWorld
npx @google-cloud/functions-framework --target=helloWorld
```

Open http://localhost:8080/ in your browser and see *Hello, World*.
Expand Down Expand Up @@ -91,7 +91,7 @@ command-line arguments:

```js
"scripts": {
"start": "functions-framework --function-target=helloWorld"
"start": "functions-framework --target=helloWorld"
}
```

Expand Down Expand Up @@ -145,15 +145,15 @@ ignored.
Command-line flag | Environment variable | Description
------------------------- | ------------------------- | -----------
`--port` | `PORT` | The port on which the Functions Framework listens for requests. Default: `8080`
`--function-target` | `FUNCTION_TARGET` | The name of the exported function to be invoked in response to requests. Default: `function`
`--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`
`--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`

You can set command-line flags in your `package.json` via the `start` script.
For example:

```js
"scripts": {
"start": "functions-framework --function-target=helloWorld"
"start": "functions-framework --target=helloWorld"
}
```

Expand Down
46 changes: 29 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,64 @@
// unmarshalled from an incoming request.

import * as minimist from 'minimist';

import {
ErrorHandler,
FunctionSignatureType,
SignatureType,
getServer,
getUserFunction,
} from './invoker';

// Supported command-line flags
const FLAG = {
PORT: 'port',
TARGET: 'target',
SIGNATURE_TYPE: 'signature-type', // dash
};

// Supported environment variables
const ENV = {
PORT: 'PORT',
TARGET: 'FUNCTION_TARGET',
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
};

enum NodeEnv {
PRODUCTION = 'production',
}

const argv = minimist(process.argv, {
string: ['port', 'function-target', 'function-signature-type'],
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
});

const CODE_LOCATION = process.cwd();
const PORT = argv['port'] || process.env.PORT || '8080';
const FUNCTION_TARGET =
argv['function-target'] || process.env.FUNCTION_TARGET || 'function';
const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080';
const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function';

const FUNCTION_SIGNATURE_TYPE_STRING =
argv['function-signature-type'] ||
process.env.FUNCTION_SIGNATURE_TYPE ||
'http';
const FUNCTION_SIGNATURE_TYPE =
FunctionSignatureType[
FUNCTION_SIGNATURE_TYPE_STRING.toUpperCase() as keyof typeof FunctionSignatureType
const SIGNATURE_TYPE_STRING =
argv[FLAG.SIGNATURE_TYPE] || process.env[ENV.SIGNATURE_TYPE] || 'http';
const SIGNATURE_TYPE =
SignatureType[
SIGNATURE_TYPE_STRING.toUpperCase() as keyof typeof SignatureType
];
if (FUNCTION_SIGNATURE_TYPE === undefined) {
console.error(`FUNCTION_SIGNATURE_TYPE must be one of 'http' or 'event'.`);
if (SIGNATURE_TYPE === undefined) {
console.error(`Function signature type must be one of 'http' or 'event'.`);
process.exit(1);
}

const USER_FUNCTION = getUserFunction(CODE_LOCATION, FUNCTION_TARGET);
const USER_FUNCTION = getUserFunction(CODE_LOCATION, TARGET);
if (!USER_FUNCTION) {
console.error('Could not load the function, shutting down.');
process.exit(1);
}

const SERVER = getServer(USER_FUNCTION!, FUNCTION_SIGNATURE_TYPE!);
const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!);
const ERROR_HANDLER = new ErrorHandler(SERVER);
SERVER.listen(PORT, () => {
ERROR_HANDLER.register();
if (process.env.NODE_ENV !== NodeEnv.PRODUCTION) {
console.log('Serving function...');
console.log(`Function: ${FUNCTION_TARGET}`);
console.log(`Function: ${TARGET}`);
console.log(`URL: http://localhost:${PORT}/`);
}
}).setTimeout(0); // Disable automatic timeout on incoming connections.
12 changes: 6 additions & 6 deletions src/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import * as bodyParser from 'body-parser';
import * as domain from 'domain';
import * as express from 'express';
import * as onFinished from 'on-finished';
import * as http from 'http';
import * as onFinished from 'on-finished';

// HTTP header field that is added to Worker response to signalize problems with
// executing the client function.
Expand Down Expand Up @@ -123,7 +123,7 @@ declare global {
}
}

export enum FunctionSignatureType {
export enum SignatureType {
HTTP,
EVENT,
}
Expand All @@ -136,9 +136,9 @@ export enum FunctionSignatureType {
*/
function isHttpFunction(
fn: HandlerFunction,
functionSignatureType: FunctionSignatureType
functionSignatureType: SignatureType
): fn is HttpFunction {
return functionSignatureType === FunctionSignatureType.HTTP;
return functionSignatureType === SignatureType.HTTP;
}

/**
Expand Down Expand Up @@ -477,7 +477,7 @@ function wrapEventFunction(
function registerFunctionRoutes(
app: express.Application,
userFunction: HandlerFunction,
functionSignatureType: FunctionSignatureType
functionSignatureType: SignatureType
) {
if (isHttpFunction(userFunction!, functionSignatureType)) {
app.use('/*', (req, res, next) => {
Expand Down Expand Up @@ -548,7 +548,7 @@ export class ErrorHandler {
*/
export function getServer(
userFunction: HandlerFunction,
functionSignatureType: FunctionSignatureType
functionSignatureType: SignatureType
): http.Server {
// App to use for function executions.
const app = express();
Expand Down
9 changes: 4 additions & 5 deletions test/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
// limitations under the License.

import * as assert from 'assert';
import * as supertest from 'supertest';
import * as express from 'express';

import * as invoker from '../src/invoker';
import * as supertest from 'supertest';

describe('loading function', () => {
it('should load the function', () => {
Expand All @@ -35,7 +34,7 @@ describe('request to HTTP function', () => {
(req: express.Request, res: express.Response) => {
res.send(req.body.text.toUpperCase());
},
invoker.FunctionSignatureType.HTTP
invoker.SignatureType.HTTP
);
return supertest(server)
.post('/')
Expand Down Expand Up @@ -106,7 +105,7 @@ describe('GCF event request to event function', () => {
const server = invoker.getServer((data: {}, context: invoker.Context) => {
receivedData = data;
receivedContext = context as invoker.CloudFunctionsContext;
}, invoker.FunctionSignatureType.EVENT);
}, invoker.SignatureType.EVENT);
await supertest(server)
.post('/')
.send(test.body)
Expand Down Expand Up @@ -169,7 +168,7 @@ describe('CloudEvents request to event function', () => {
const server = invoker.getServer((data: {}, context: invoker.Context) => {
receivedData = data;
receivedContext = context as invoker.CloudEventsContext;
}, invoker.FunctionSignatureType.EVENT);
}, invoker.SignatureType.EVENT);
await supertest(server)
.post('/')
.set(test.headers)
Expand Down

0 comments on commit 76f134b

Please sign in to comment.