blob: b56bea7add59b4956a44446ec68f5495455785be [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");
Cameron Martinfdaca5e2025-01-07 15:25:15 +000025const { program } = require("commander");
bforbisda1169d2018-10-28 11:27:38 -040026const 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)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010032 "binary",
bforbisda1169d2018-10-28 11:27:38 -040033 )
34 .option(
35 "-t, --transport <transport>",
36 "Set thrift transport (buffered|framed|http)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010037 "buffered",
bforbisda1169d2018-10-28 11:27:38 -040038 )
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(
Cameron Martinfdaca5e2025-01-07 15:25:15 +000043 "--type <type>",
bforbisda1169d2018-10-28 11:27:38 -040044 "Select server type (http|multiplex|tcp|websocket)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010045 "tcp",
bforbisda1169d2018-10-28 11:27:38 -040046 )
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
Cameron Martinfdaca5e2025-01-07 15:25:15 +000056const opts = program.opts();
57const port = opts.port;
58const domainSocket = opts.domainSocket;
59const ssl = opts.ssl;
henrique31236232014-02-23 20:16:44 +010060
Cameron Martinfdaca5e2025-01-07 15:25:15 +000061let type = opts.type;
62if (opts.transport === "http") {
63 opts.transport = "buffered";
bforbisda1169d2018-10-28 11:27:38 -040064 type = "http";
Cameron Martinfdaca5e2025-01-07 15:25:15 +000065} else if (opts.transport === "websocket") {
66 opts.transport = "buffered";
penenin1ab096c2020-05-18 12:27:31 -070067 type = "websocket";
James E. King, III375bfee2017-10-26 00:09:34 -040068}
69
bforbisda1169d2018-10-28 11:27:38 -040070let options = {
Cameron Martinfdaca5e2025-01-07 15:25:15 +000071 transport: helpers.transports[opts.transport],
72 protocol: helpers.protocols[opts.protocol],
Roger Meier57b354b2014-02-22 01:01:58 +010073};
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080074
bforbisda1169d2018-10-28 11:27:38 -040075if (type === "http" || type === "websocket") {
76 options.handler = ThriftTestHandler;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080077 options.processor = ThriftTest;
78
79 options = {
Randy Abernethyd8187c52015-02-16 01:25:53 -080080 services: { "/test": options },
81 cors: {
Cameron Martincaef0ed2025-01-15 11:58:39 +010082 "*": true,
83 },
bforbisda1169d2018-10-28 11:27:38 -040084 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080085}
86
bforbisda1169d2018-10-28 11:27:38 -040087let processor;
88if (type === "multiplex") {
89 const SecondServiceHandler = {
Cameron Martincaef0ed2025-01-15 11:58:39 +010090 secondtestString: function (thing, result) {
James E. King, III58402ff2017-11-17 14:41:46 -050091 console.log('testString("' + thing + '")');
92 result(null, 'testString("' + thing + '")');
Cameron Martincaef0ed2025-01-15 11:58:39 +010093 },
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080094 };
95
bforbisda1169d2018-10-28 11:27:38 -040096 processor = new thrift.MultiplexedProcessor();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080097
bforbisda1169d2018-10-28 11:27:38 -040098 processor.registerProcessor(
99 "ThriftTest",
Cameron Martincaef0ed2025-01-15 11:58:39 +0100100 new ThriftTest.Processor(ThriftTestHandler),
bforbisda1169d2018-10-28 11:27:38 -0400101 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800102
bforbisda1169d2018-10-28 11:27:38 -0400103 processor.registerProcessor(
104 "SecondService",
Cameron Martincaef0ed2025-01-15 11:58:39 +0100105 new SecondService.Processor(SecondServiceHandler),
bforbisda1169d2018-10-28 11:27:38 -0400106 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800107}
108
109if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -0400110 if (
111 type === "tcp" ||
112 type === "multiplex" ||
113 type === "http" ||
114 type === "websocket"
115 ) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800116 options.tls = {
bforbisda1169d2018-10-28 11:27:38 -0400117 key: fs.readFileSync(path.resolve(__dirname, "server.key")),
Cameron Martincaef0ed2025-01-15 11:58:39 +0100118 cert: fs.readFileSync(path.resolve(__dirname, "server.crt")),
Daniel Shihe41de0f2018-03-21 08:28:38 +0800119 };
120 }
Roger Meier57b354b2014-02-22 01:01:58 +0100121}
Randy Abernethyd60f9782014-03-28 10:36:38 -0700122
bforbisda1169d2018-10-28 11:27:38 -0400123let server;
124if (type === "tcp") {
125 server = thrift.createServer(ThriftTest, ThriftTestHandler, options);
126} else if (type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800127 server = thrift.createMultiplexServer(processor, options);
bforbisda1169d2018-10-28 11:27:38 -0400128} else if (type === "http" || type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800129 server = thrift.createWebServer(options);
130}
131
Daniel Shihe41de0f2018-03-21 08:28:38 +0800132if (domainSocket) {
133 server.listen(domainSocket);
bforbisda1169d2018-10-28 11:27:38 -0400134} else if (
135 type === "tcp" ||
136 type === "multiplex" ||
137 type === "http" ||
138 type === "websocket"
139) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800140 server.listen(port);
141}