This repository was archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·43 lines (34 loc) · 1.47 KB
/
index.js
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
#!/usr/bin/env node
import { program, Option } from 'commander'
import { getUserFunction } from './src/loader.js'
import { FUNCTION_SOURCE } from './src/const.js'
import { getServer } from './src/server.js'
const { HTTP, CLOUDEVENT, OPENFUNCTION } = FUNCTION_SOURCE
program
.requiredOption('-t, --target <target>', 'function target name, support nested function name')
.addOption(new Option('-p, --port <port>', 'the exposed port of the function server').default('8080'))
.addOption(new Option('-s, --source <type>', 'function source type')
.default(OPENFUNCTION).choices([HTTP, CLOUDEVENT, OPENFUNCTION]))
program.parse(process.argv)
const options = program.opts()
const SERVER_PORT = options.port
const FUNCTION_TARGET = options.target
const SIGNATURE_TYPE = options.source
const CODE_LOCATION = process.cwd() + '/index.js'
let userFunction
getUserFunction(CODE_LOCATION, FUNCTION_TARGET).then(fn => {
if (fn === null) {
console.error('Could not load the function, shutting down.')
process.exit(1)
}
userFunction = fn
// To catch unhandled exceptions thrown by user code async callbacks,
// these exceptions cannot be catched by try-catch in user function invocation code below
process.on('uncaughtException', (err) => {
console.error(`Caught exception: ${err}`)
})
const app = getServer(userFunction, SIGNATURE_TYPE)
app.listen(SERVER_PORT, () => {
console.log(`Openfunction functions framework listening at http://localhost:${SERVER_PORT}`)
})
})