|
16 | 16 |
|
17 | 17 | // Functions framework entry point that configures and starts Node.js server |
18 | 18 | // that runs user's code on HTTP request. |
19 | | -// The following environment variables can be set to configure the framework: |
20 | | -// - PORT - defines the port on which this server listens to all HTTP |
21 | | -// requests. |
22 | | -// - FUNCTION_TARGET - defines the name of the function within user's |
23 | | -// node module to execute. If such a function is not defined, |
24 | | -// then falls back to 'function' name. |
25 | | -// - FUNCTION_SIGNATURE_TYPE - defines the type of the client function |
26 | | -// signature: |
27 | | -// - 'http' for function signature with HTTP request and HTTP |
28 | | -// response arguments, |
29 | | -// - 'event' for function signature with arguments |
30 | | -// unmarshalled from an incoming request, |
31 | | -// - 'cloudevent' for function signature with arguments |
32 | | -// unmarshalled as CloudEvents from an incoming request. |
33 | | - |
34 | | -import * as minimist from 'minimist'; |
35 | | -import {resolve} from 'path'; |
36 | 19 | import {getUserFunction} from './loader'; |
37 | 20 | import {ErrorHandler} from './invoker'; |
38 | 21 | import {getServer} from './server'; |
39 | | -import {SignatureType, isValidSignatureType} from './types'; |
40 | | - |
41 | | -// Supported command-line flags |
42 | | -const FLAG = { |
43 | | - PORT: 'port', |
44 | | - TARGET: 'target', |
45 | | - SIGNATURE_TYPE: 'signature-type', // dash |
46 | | - SOURCE: 'source', |
47 | | -}; |
48 | | - |
49 | | -// Supported environment variables |
50 | | -const ENV = { |
51 | | - PORT: 'PORT', |
52 | | - TARGET: 'FUNCTION_TARGET', |
53 | | - SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore |
54 | | - SOURCE: 'FUNCTION_SOURCE', |
55 | | -}; |
56 | | - |
57 | | -const argv = minimist(process.argv, { |
58 | | - string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE], |
59 | | -}); |
60 | | - |
61 | | -const CODE_LOCATION = resolve( |
62 | | - argv[FLAG.SOURCE] || process.env[ENV.SOURCE] || '' |
63 | | -); |
64 | | -const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080'; |
65 | | -const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function'; |
66 | | - |
67 | | -const SIGNATURE_TYPE = ( |
68 | | - argv[FLAG.SIGNATURE_TYPE] || |
69 | | - process.env[ENV.SIGNATURE_TYPE] || |
70 | | - 'http' |
71 | | -).toLowerCase(); |
72 | | -if (!isValidSignatureType(SIGNATURE_TYPE)) { |
73 | | - console.error( |
74 | | - `Function signature type must be one of: ${SignatureType.join(', ')}.` |
75 | | - ); |
76 | | - // eslint-disable-next-line no-process-exit |
77 | | - process.exit(1); |
78 | | -} |
| 22 | +import {parseOptions, helpText, OptionsError} from './options'; |
79 | 23 |
|
80 | | -// CLI Help Flag |
81 | | -if (process.argv[2] === '-h' || process.argv[2] === '--help') { |
82 | | - console.error( |
83 | | - `Example usage: |
84 | | - functions-framework --target=helloWorld --port=8080 |
85 | | -Documentation: |
86 | | - https://github.com/GoogleCloudPlatform/functions-framework-nodejs` |
87 | | - ); |
88 | | - // eslint-disable-next-line no-process-exit |
89 | | - process.exit(0); |
90 | | -} |
| 24 | +(async () => { |
| 25 | + try { |
| 26 | + const options = parseOptions(); |
91 | 27 |
|
92 | | -getUserFunction(CODE_LOCATION, TARGET).then(userFunction => { |
93 | | - if (!userFunction) { |
94 | | - console.error('Could not load the function, shutting down.'); |
95 | | - // eslint-disable-next-line no-process-exit |
96 | | - process.exit(1); |
97 | | - } |
98 | | - |
99 | | - const SERVER = getServer(userFunction!, SIGNATURE_TYPE!); |
100 | | - const ERROR_HANDLER = new ErrorHandler(SERVER); |
101 | | - |
102 | | - SERVER.listen(PORT, () => { |
103 | | - ERROR_HANDLER.register(); |
104 | | - if (process.env.NODE_ENV !== 'production') { |
105 | | - console.log('Serving function...'); |
106 | | - console.log(`Function: ${TARGET}`); |
107 | | - console.log(`Signature type: ${SIGNATURE_TYPE}`); |
108 | | - console.log(`URL: http://localhost:${PORT}/`); |
| 28 | + if (options.printHelp) { |
| 29 | + console.error(helpText); |
| 30 | + return; |
109 | 31 | } |
110 | | - }).setTimeout(0); // Disable automatic timeout on incoming connections. |
111 | | -}); |
| 32 | + const userFunction = await getUserFunction( |
| 33 | + options.sourceLocation, |
| 34 | + options.target |
| 35 | + ); |
| 36 | + if (!userFunction) { |
| 37 | + console.error('Could not load the function, shutting down.'); |
| 38 | + // eslint-disable-next-line no-process-exit |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | + const server = getServer(userFunction!, options.signatureType); |
| 42 | + const errorHandler = new ErrorHandler(server); |
| 43 | + server |
| 44 | + .listen(options.port, () => { |
| 45 | + errorHandler.register(); |
| 46 | + if (process.env.NODE_ENV !== 'production') { |
| 47 | + console.log('Serving function...'); |
| 48 | + console.log(`Function: ${options.target}`); |
| 49 | + console.log(`Signature type: ${options.signatureType}`); |
| 50 | + console.log(`URL: http://localhost:${options.port}/`); |
| 51 | + } |
| 52 | + }) |
| 53 | + .setTimeout(0); // Disable automatic timeout on incoming connections. |
| 54 | + } catch (e) { |
| 55 | + if (e instanceof OptionsError) { |
| 56 | + console.error(e.message); |
| 57 | + // eslint-disable-next-line no-process-exit |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + throw e; |
| 61 | + } |
| 62 | +})(); |
0 commit comments