Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/env node |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 2 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 3 | /* |
| 4 | * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | * or more contributor license agreements. See the NOTICE file |
| 6 | * distributed with this work for additional information |
| 7 | * regarding copyright ownership. The ASF licenses this file |
| 8 | * to you under the Apache License, Version 2.0 (the |
| 9 | * 'License'); you may not use this file except in compliance |
| 10 | * with the License. You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, |
| 15 | * software distributed under the License is distributed on an |
| 16 | * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | * KIND, either express or implied. See the License for the |
| 18 | * specific language governing permissions and limitations |
| 19 | * under the License. |
| 20 | */ |
| 21 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 22 | const fs = require("fs"); |
| 23 | const path = require("path"); |
| 24 | const thrift = require("../lib/thrift"); |
| 25 | const program = require("commander"); |
| 26 | const helpers = require("./helpers"); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 27 | |
| 28 | program |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 29 | .option( |
| 30 | "-p, --protocol <protocol>", |
| 31 | "Set thrift protocol (binary|compact|json)", |
| 32 | "binary" |
| 33 | ) |
| 34 | .option( |
| 35 | "-t, --transport <transport>", |
| 36 | "Set thrift transport (buffered|framed|http)", |
| 37 | "buffered" |
| 38 | ) |
| 39 | .option("--ssl", "use ssl transport") |
| 40 | .option("--port <port>", "Set thrift server port", 9090) |
| 41 | .option("--domain-socket <path>", "Set thift server unix domain socket") |
| 42 | .option( |
| 43 | "-t, --type <type>", |
| 44 | "Select server type (http|multiplex|tcp|websocket)", |
| 45 | "tcp" |
| 46 | ) |
| 47 | .option("--callback", "test with callback style functions") |
| 48 | .option("--es6", "Use es6 code") |
| 49 | .option("--es5", "Use es5 code") |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 50 | .parse(process.argv); |
| 51 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 52 | const ThriftTest = require(`./${helpers.genPath}/ThriftTest`); |
| 53 | const SecondService = require(`./${helpers.genPath}/SecondService`); |
| 54 | const { ThriftTestHandler } = require("./test_handler"); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 55 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 56 | const port = program.port; |
| 57 | const domainSocket = program.domainSocket; |
| 58 | const ssl = program.ssl; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 59 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 60 | let type = program.type; |
| 61 | if (program.transport === "http") { |
| 62 | program.transport = "buffered"; |
| 63 | type = "http"; |
penenin | 1ab096c | 2020-05-18 12:27:31 -0700 | [diff] [blame] | 64 | } else if (program.transport === "websocket") { |
| 65 | program.transport = "buffered"; |
| 66 | type = "websocket"; |
James E. King, III | 375bfee | 2017-10-26 00:09:34 -0400 | [diff] [blame] | 67 | } |
| 68 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 69 | let options = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 70 | transport: helpers.transports[program.transport], |
| 71 | protocol: helpers.protocols[program.protocol] |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 72 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 73 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 74 | if (type === "http" || type === "websocket") { |
| 75 | options.handler = ThriftTestHandler; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 76 | options.processor = ThriftTest; |
| 77 | |
| 78 | options = { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 79 | services: { "/test": options }, |
| 80 | cors: { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 81 | "*": true |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 82 | } |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 83 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 84 | } |
| 85 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 86 | let processor; |
| 87 | if (type === "multiplex") { |
| 88 | const SecondServiceHandler = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 89 | secondtestString: function(thing, result) { |
James E. King, III | 58402ff | 2017-11-17 14:41:46 -0500 | [diff] [blame] | 90 | console.log('testString("' + thing + '")'); |
| 91 | result(null, 'testString("' + thing + '")'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 92 | } |
| 93 | }; |
| 94 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 95 | processor = new thrift.MultiplexedProcessor(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 96 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 97 | processor.registerProcessor( |
| 98 | "ThriftTest", |
| 99 | new ThriftTest.Processor(ThriftTestHandler) |
| 100 | ); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 101 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 102 | processor.registerProcessor( |
| 103 | "SecondService", |
| 104 | new SecondService.Processor(SecondServiceHandler) |
| 105 | ); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (ssl) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 109 | if ( |
| 110 | type === "tcp" || |
| 111 | type === "multiplex" || |
| 112 | type === "http" || |
| 113 | type === "websocket" |
| 114 | ) { |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 115 | options.tls = { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 116 | key: fs.readFileSync(path.resolve(__dirname, "server.key")), |
| 117 | cert: fs.readFileSync(path.resolve(__dirname, "server.crt")) |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 118 | }; |
| 119 | } |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 120 | } |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 121 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 122 | let server; |
| 123 | if (type === "tcp") { |
| 124 | server = thrift.createServer(ThriftTest, ThriftTestHandler, options); |
| 125 | } else if (type === "multiplex") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 126 | server = thrift.createMultiplexServer(processor, options); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 127 | } else if (type === "http" || type === "websocket") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 128 | server = thrift.createWebServer(options); |
| 129 | } |
| 130 | |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 131 | if (domainSocket) { |
| 132 | server.listen(domainSocket); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 133 | } else if ( |
| 134 | type === "tcp" || |
| 135 | type === "multiplex" || |
| 136 | type === "http" || |
| 137 | type === "websocket" |
| 138 | ) { |
Daniel Shih | e41de0f | 2018-03-21 08:28:38 +0800 | [diff] [blame] | 139 | server.listen(port); |
| 140 | } |