blob: 7402094bc5115ad2448dca75cca4878dc1610012 [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";
James E. King, III375bfee2017-10-26 00:09:34 -040064}
65
bforbisda1169d2018-10-28 11:27:38 -040066let options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080067 transport: helpers.transports[program.transport],
68 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010069};
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080070
bforbisda1169d2018-10-28 11:27:38 -040071if (type === "http" || type === "websocket") {
72 options.handler = ThriftTestHandler;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080073 options.processor = ThriftTest;
74
75 options = {
Randy Abernethyd8187c52015-02-16 01:25:53 -080076 services: { "/test": options },
77 cors: {
bforbisda1169d2018-10-28 11:27:38 -040078 "*": true
Randy Abernethyd8187c52015-02-16 01:25:53 -080079 }
bforbisda1169d2018-10-28 11:27:38 -040080 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080081}
82
bforbisda1169d2018-10-28 11:27:38 -040083let processor;
84if (type === "multiplex") {
85 const SecondServiceHandler = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080086 secondtestString: function(thing, result) {
James E. King, III58402ff2017-11-17 14:41:46 -050087 console.log('testString("' + thing + '")');
88 result(null, 'testString("' + thing + '")');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080089 }
90 };
91
bforbisda1169d2018-10-28 11:27:38 -040092 processor = new thrift.MultiplexedProcessor();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080093
bforbisda1169d2018-10-28 11:27:38 -040094 processor.registerProcessor(
95 "ThriftTest",
96 new ThriftTest.Processor(ThriftTestHandler)
97 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080098
bforbisda1169d2018-10-28 11:27:38 -040099 processor.registerProcessor(
100 "SecondService",
101 new SecondService.Processor(SecondServiceHandler)
102 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800103}
104
105if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -0400106 if (
107 type === "tcp" ||
108 type === "multiplex" ||
109 type === "http" ||
110 type === "websocket"
111 ) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800112 options.tls = {
bforbisda1169d2018-10-28 11:27:38 -0400113 key: fs.readFileSync(path.resolve(__dirname, "server.key")),
114 cert: fs.readFileSync(path.resolve(__dirname, "server.crt"))
Daniel Shihe41de0f2018-03-21 08:28:38 +0800115 };
116 }
Roger Meier57b354b2014-02-22 01:01:58 +0100117}
Randy Abernethyd60f9782014-03-28 10:36:38 -0700118
bforbisda1169d2018-10-28 11:27:38 -0400119let server;
120if (type === "tcp") {
121 server = thrift.createServer(ThriftTest, ThriftTestHandler, options);
122} else if (type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800123 server = thrift.createMultiplexServer(processor, options);
bforbisda1169d2018-10-28 11:27:38 -0400124} else if (type === "http" || type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800125 server = thrift.createWebServer(options);
126}
127
Daniel Shihe41de0f2018-03-21 08:28:38 +0800128if (domainSocket) {
129 server.listen(domainSocket);
bforbisda1169d2018-10-28 11:27:38 -0400130} else if (
131 type === "tcp" ||
132 type === "multiplex" ||
133 type === "http" ||
134 type === "websocket"
135) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800136 server.listen(port);
137}