-
Notifications
You must be signed in to change notification settings - Fork 26
/
cli.js
executable file
·70 lines (58 loc) · 1.88 KB
/
cli.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
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
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const grpcGateway = require('./index.js')
const yargs = require('yargs')
const express = require('express')
const bodyParser = require('body-parser')
const grpc = require('grpc')
const argv = yargs.usage('Usage: $0 [options] DEFINITION.proto [DEFINITION2.proto...]')
.help('?')
.alias('?', 'help')
.alias('?', 'h')
.default('port', process.env.PORT || 8080)
.describe('port', 'The port to serve your JSON proxy on')
.alias('port', 'p')
.default('grpc', process.env.GRPC_HOST || 'localhost:50051')
.describe('grpc', 'The host & port to connect to, where your gprc-server is running')
.alias('grpc', 'g')
.describe('I', 'Path to resolve imports from')
.alias('I', 'include')
.describe('ca', 'SSL CA cert for gRPC')
.describe('key', 'SSL client key for gRPC')
.describe('cert', 'SSL client certificate for gRPC')
.default('mountpoint', '/')
.describe('mountpoint', 'URL to mount server on')
.alias('mountpoint', 'm')
.boolean('quiet')
.describe('quiet', 'Suppress logs')
.alias('quiet', 'q')
.argv
if (!argv._.length) {
yargs.showHelp()
process.exit(1)
}
let credentials
if (argv.ca || argv.key || argv.cert) {
if (!(argv.ca && argv.key && argv.cert)) {
console.log('SSL requires --ca, --key, & --cert\n')
yargs.showHelp()
process.exit(1)
}
credentials = grpc.credentials.createSsl(
fs.readFileSync(argv.ca),
fs.readFileSync(argv.key),
fs.readFileSync(argv.cert)
)
} else {
credentials = grpc.credentials.createInsecure()
}
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(argv.mountpoint, grpcGateway(argv._, argv.grpc, credentials, !argv.quiet, argv.include))
app.listen(argv.port, () => {
if (!argv.quiet) {
console.log(`Listening on http://localhost:${argv.port}, proxying to gRPC on ${argv.grpc}`)
}
})