|
28 | 28 | // unmarshalled from an incoming request. |
29 | 29 |
|
30 | 30 | import * as minimist from 'minimist'; |
| 31 | + |
31 | 32 | import { |
32 | 33 | ErrorHandler, |
33 | | - FunctionSignatureType, |
| 34 | + SignatureType, |
34 | 35 | getServer, |
35 | 36 | getUserFunction, |
36 | 37 | } from './invoker'; |
37 | 38 |
|
| 39 | +// Supported command-line flags |
| 40 | +const FLAG = { |
| 41 | + PORT: 'port', |
| 42 | + TARGET: 'target', |
| 43 | + SIGNATURE_TYPE: 'signature-type', // dash |
| 44 | +}; |
| 45 | + |
| 46 | +// Supported environment variables |
| 47 | +const ENV = { |
| 48 | + PORT: 'PORT', |
| 49 | + TARGET: 'FUNCTION_TARGET', |
| 50 | + SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore |
| 51 | +}; |
| 52 | + |
38 | 53 | enum NodeEnv { |
39 | 54 | PRODUCTION = 'production', |
40 | 55 | } |
41 | 56 |
|
42 | 57 | const argv = minimist(process.argv, { |
43 | | - string: ['port', 'function-target', 'function-signature-type'], |
| 58 | + string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE], |
44 | 59 | }); |
45 | 60 |
|
46 | 61 | const CODE_LOCATION = process.cwd(); |
47 | | -const PORT = argv['port'] || process.env.PORT || '8080'; |
48 | | -const FUNCTION_TARGET = |
49 | | - argv['function-target'] || process.env.FUNCTION_TARGET || 'function'; |
| 62 | +const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080'; |
| 63 | +const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function'; |
50 | 64 |
|
51 | | -const FUNCTION_SIGNATURE_TYPE_STRING = |
52 | | - argv['function-signature-type'] || |
53 | | - process.env.FUNCTION_SIGNATURE_TYPE || |
54 | | - 'http'; |
55 | | -const FUNCTION_SIGNATURE_TYPE = |
56 | | - FunctionSignatureType[ |
57 | | - FUNCTION_SIGNATURE_TYPE_STRING.toUpperCase() as keyof typeof FunctionSignatureType |
| 65 | +const SIGNATURE_TYPE_STRING = |
| 66 | + argv[FLAG.SIGNATURE_TYPE] || process.env[ENV.SIGNATURE_TYPE] || 'http'; |
| 67 | +const SIGNATURE_TYPE = |
| 68 | + SignatureType[ |
| 69 | + SIGNATURE_TYPE_STRING.toUpperCase() as keyof typeof SignatureType |
58 | 70 | ]; |
59 | | -if (FUNCTION_SIGNATURE_TYPE === undefined) { |
60 | | - console.error(`FUNCTION_SIGNATURE_TYPE must be one of 'http' or 'event'.`); |
| 71 | +if (SIGNATURE_TYPE === undefined) { |
| 72 | + console.error(`Function signature type must be one of 'http' or 'event'.`); |
61 | 73 | process.exit(1); |
62 | 74 | } |
63 | 75 |
|
64 | | -const USER_FUNCTION = getUserFunction(CODE_LOCATION, FUNCTION_TARGET); |
| 76 | +const USER_FUNCTION = getUserFunction(CODE_LOCATION, TARGET); |
65 | 77 | if (!USER_FUNCTION) { |
66 | 78 | console.error('Could not load the function, shutting down.'); |
67 | 79 | process.exit(1); |
68 | 80 | } |
69 | 81 |
|
70 | | -const SERVER = getServer(USER_FUNCTION!, FUNCTION_SIGNATURE_TYPE!); |
| 82 | +const SERVER = getServer(USER_FUNCTION!, SIGNATURE_TYPE!); |
71 | 83 | const ERROR_HANDLER = new ErrorHandler(SERVER); |
72 | 84 | SERVER.listen(PORT, () => { |
73 | 85 | ERROR_HANDLER.register(); |
74 | 86 | if (process.env.NODE_ENV !== NodeEnv.PRODUCTION) { |
75 | 87 | console.log('Serving function...'); |
76 | | - console.log(`Function: ${FUNCTION_TARGET}`); |
| 88 | + console.log(`Function: ${TARGET}`); |
77 | 89 | console.log(`URL: http://localhost:${PORT}/`); |
78 | 90 | } |
79 | 91 | }).setTimeout(0); // Disable automatic timeout on incoming connections. |
0 commit comments