forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
118 lines (105 loc) · 3.84 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Functions framework entry point that configures and starts Node.js server
// that runs user's code on HTTP request.
// The following environment variables can be set to configure the framework:
// - PORT - defines the port on which this server listens to all HTTP
// requests.
// - FUNCTION_TARGET - defines 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.
// - FUNCTION_SIGNATURE_TYPE - defines the type of the client function
// signature:
// - 'http' for function signature with HTTP request and HTTP
// response arguments,
// - 'event' for function signature with arguments
// unmarshalled from an incoming request,
// - 'cloudevent' for function signature with arguments
// unmarshalled as CloudEvents from an incoming request.
import * as minimist from 'minimist';
import {resolve} from 'path';
import {getUserFunction} from './loader';
import {ErrorHandler} from './invoker';
import {getServer} from './server';
import {SignatureType} from './types';
// Supported command-line flags
const FLAG = {
PORT: 'port',
TARGET: 'target',
SIGNATURE_TYPE: 'signature-type', // dash
SOURCE: 'source',
};
// Supported environment variables
const ENV = {
PORT: 'PORT',
TARGET: 'FUNCTION_TARGET',
SIGNATURE_TYPE: 'FUNCTION_SIGNATURE_TYPE', // underscore
SOURCE: 'FUNCTION_SOURCE',
};
enum NodeEnv {
PRODUCTION = 'production',
}
const argv = minimist(process.argv, {
string: [FLAG.PORT, FLAG.TARGET, FLAG.SIGNATURE_TYPE],
});
const CODE_LOCATION = resolve(
argv[FLAG.SOURCE] || process.env[ENV.SOURCE] || ''
);
const PORT = argv[FLAG.PORT] || process.env[ENV.PORT] || '8080';
const TARGET = argv[FLAG.TARGET] || process.env[ENV.TARGET] || 'function';
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 (SIGNATURE_TYPE === undefined) {
console.error(
`Function signature type must be one of: ${Object.values(
SignatureType
).join(', ')}.`
);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
// CLI Help Flag
if (process.argv[2] === '-h' || process.argv[2] === '--help') {
console.error(
`Example usage:
functions-framework --target=helloWorld --port=8080
Documentation:
https://github.com/GoogleCloudPlatform/functions-framework-nodejs`
);
// eslint-disable-next-line no-process-exit
process.exit(0);
}
getUserFunction(CODE_LOCATION, TARGET).then(USER_FUNCTION => {
if (!USER_FUNCTION) {
console.error('Could not load the function, shutting down.');
// eslint-disable-next-line no-process-exit
process.exit(1);
}
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: ${TARGET}`);
console.log(`Signature type: ${SIGNATURE_TYPE}`);
console.log(`URL: http://localhost:${PORT}/`);
}
}).setTimeout(0); // Disable automatic timeout on incoming connections.
});