blob: 677839aea2bf9195fab21658152155e1f2c427a5 [file] [log] [blame]
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -08001#!/usr/bin/env node
Roger Meier32f39822014-06-18 22:43:17 +02002
Roger Meier8909cbd2014-01-26 11:44:27 +01003/*
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
bforbisda1169d2018-10-28 11:27:38 -040022const fs = require("fs");
23const path = require("path");
24const thrift = require("../lib/thrift");
25const program = require("commander");
26const helpers = require("./helpers");
Roger Meier8909cbd2014-01-26 11:44:27 +010027
28program
bforbisda1169d2018-10-28 11:27:38 -040029 .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 Meier8909cbd2014-01-26 11:44:27 +010050 .parse(process.argv);
51
bforbisda1169d2018-10-28 11:27:38 -040052const ThriftTest = require(`./${helpers.genPath}/ThriftTest`);
53const SecondService = require(`./${helpers.genPath}/SecondService`);
54const { ThriftTestHandler } = require("./test_handler");
Roger Meier8909cbd2014-01-26 11:44:27 +010055
bforbisda1169d2018-10-28 11:27:38 -040056const port = program.port;
57const domainSocket = program.domainSocket;
58const ssl = program.ssl;
henrique31236232014-02-23 20:16:44 +010059
bforbisda1169d2018-10-28 11:27:38 -040060let type = program.type;
61if (program.transport === "http") {
62 program.transport = "buffered";
63 type = "http";
penenin1ab096c2020-05-18 12:27:31 -070064} else if (program.transport === "websocket") {
65 program.transport = "buffered";
66 type = "websocket";
James E. King, III375bfee2017-10-26 00:09:34 -040067}
68
bforbisda1169d2018-10-28 11:27:38 -040069let options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080070 transport: helpers.transports[program.transport],
71 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010072};
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080073
bforbisda1169d2018-10-28 11:27:38 -040074if (type === "http" || type === "websocket") {
75 options.handler = ThriftTestHandler;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080076 options.processor = ThriftTest;
77
78 options = {
Randy Abernethyd8187c52015-02-16 01:25:53 -080079 services: { "/test": options },
80 cors: {
bforbisda1169d2018-10-28 11:27:38 -040081 "*": true
Randy Abernethyd8187c52015-02-16 01:25:53 -080082 }
bforbisda1169d2018-10-28 11:27:38 -040083 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080084}
85
bforbisda1169d2018-10-28 11:27:38 -040086let processor;
87if (type === "multiplex") {
88 const SecondServiceHandler = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080089 secondtestString: function(thing, result) {
James E. King, III58402ff2017-11-17 14:41:46 -050090 console.log('testString("' + thing + '")');
91 result(null, 'testString("' + thing + '")');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080092 }
93 };
94
bforbisda1169d2018-10-28 11:27:38 -040095 processor = new thrift.MultiplexedProcessor();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080096
bforbisda1169d2018-10-28 11:27:38 -040097 processor.registerProcessor(
98 "ThriftTest",
99 new ThriftTest.Processor(ThriftTestHandler)
100 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800101
bforbisda1169d2018-10-28 11:27:38 -0400102 processor.registerProcessor(
103 "SecondService",
104 new SecondService.Processor(SecondServiceHandler)
105 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800106}
107
108if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -0400109 if (
110 type === "tcp" ||
111 type === "multiplex" ||
112 type === "http" ||
113 type === "websocket"
114 ) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800115 options.tls = {
bforbisda1169d2018-10-28 11:27:38 -0400116 key: fs.readFileSync(path.resolve(__dirname, "server.key")),
117 cert: fs.readFileSync(path.resolve(__dirname, "server.crt"))
Daniel Shihe41de0f2018-03-21 08:28:38 +0800118 };
119 }
Roger Meier57b354b2014-02-22 01:01:58 +0100120}
Randy Abernethyd60f9782014-03-28 10:36:38 -0700121
bforbisda1169d2018-10-28 11:27:38 -0400122let server;
123if (type === "tcp") {
124 server = thrift.createServer(ThriftTest, ThriftTestHandler, options);
125} else if (type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800126 server = thrift.createMultiplexServer(processor, options);
bforbisda1169d2018-10-28 11:27:38 -0400127} else if (type === "http" || type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800128 server = thrift.createWebServer(options);
129}
130
Daniel Shihe41de0f2018-03-21 08:28:38 +0800131if (domainSocket) {
132 server.listen(domainSocket);
bforbisda1169d2018-10-28 11:27:38 -0400133} else if (
134 type === "tcp" ||
135 type === "multiplex" ||
136 type === "http" ||
137 type === "websocket"
138) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800139 server.listen(port);
140}