blob: 030d28b38a7b9e35e438878192d8ab13640826f9 [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
Roger Meier57b354b2014-02-22 01:01:58 +010022var fs = require('fs');
23var path = require('path');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080024var thrift = require('../lib/thrift');
Roger Meier8909cbd2014-01-26 11:44:27 +010025var program = require('commander');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080026var helpers = require('./helpers');
27
28var ThriftTest = require('./gen-nodejs/ThriftTest');
29var SecondService = require('./gen-nodejs/SecondService');
30var ThriftTestHandler = require('./test_handler').AsyncThriftTestHandler;
31var ThriftTestHandlerPromise = require('./test_handler').SyncThriftTestHandler;
32var ttypes = require('./gen-nodejs/ThriftTest_types');
Roger Meier8909cbd2014-01-26 11:44:27 +010033
34program
James E. King, III375bfee2017-10-26 00:09:34 -040035 .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|compact|json)', 'binary')
36 .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed|http)', 'buffered')
Roger Meier57b354b2014-02-22 01:01:58 +010037 .option('--ssl', 'use ssl transport')
Roger Meier47accfc2015-03-24 21:01:46 +010038 .option('--port <port>', 'Set thrift server port', 9090)
Daniel Shihe41de0f2018-03-21 08:28:38 +080039 .option('--domain-socket <path>', 'Set thift server unix domain socket')
henrique31236232014-02-23 20:16:44 +010040 .option('--promise', 'test with promise style functions')
James E. King, III375bfee2017-10-26 00:09:34 -040041 .option('-t, --type <type>', 'Select server type (http|multiplex|tcp|websocket)', 'tcp')
Roger Meier8909cbd2014-01-26 11:44:27 +010042 .parse(process.argv);
43
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080044var port = program.port;
Daniel Shihe41de0f2018-03-21 08:28:38 +080045var domainSocket = program.domainSocket;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080046var type = program.type;
47var ssl = program.ssl;
48var promise = program.promise;
Roger Meier8909cbd2014-01-26 11:44:27 +010049
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080050var handler = program.promise ? ThriftTestHandler : ThriftTestHandlerPromise;
henrique31236232014-02-23 20:16:44 +010051
James E. King, III375bfee2017-10-26 00:09:34 -040052if (program.transport === 'http') {
53 program.transport = 'buffered';
54 type = 'http';
55}
56
Roger Meier57b354b2014-02-22 01:01:58 +010057var options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080058 transport: helpers.transports[program.transport],
59 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010060};
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080061
62if (type === 'http' || type ==='websocket') {
63 options.handler = handler;
64 options.processor = ThriftTest;
65
66 options = {
Randy Abernethyd8187c52015-02-16 01:25:53 -080067 services: { "/test": options },
68 cors: {
69 '*': true
70 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080071 }
72}
73
74if (type === 'multiplex') {
75 var SecondServiceHandler = {
76 secondtestString: function(thing, result) {
James E. King, III58402ff2017-11-17 14:41:46 -050077 console.log('testString("' + thing + '")');
78 result(null, 'testString("' + thing + '")');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080079 }
80 };
81
82 var processor = new thrift.MultiplexedProcessor();
83
84 processor.registerProcessor("ThriftTest",
85 new ThriftTest.Processor(ThriftTestHandler));
86
87 processor.registerProcessor("SecondService",
88 new SecondService.Processor(SecondServiceHandler));
89
90}
91
92if (ssl) {
Daniel Shihe41de0f2018-03-21 08:28:38 +080093 if (type === 'tcp' || type === 'multiplex' || type === 'http' || type === 'websocket') {
94 options.tls = {
95 key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
96 cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
97 };
98 }
Roger Meier57b354b2014-02-22 01:01:58 +010099}
Randy Abernethyd60f9782014-03-28 10:36:38 -0700100
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800101var server;
102if (type === 'tcp') {
103 server = thrift.createServer(ThriftTest, handler, options);
104} else if (type === 'multiplex') {
105 server = thrift.createMultiplexServer(processor, options);
106} else if (type === 'http' || type === 'websocket') {
107 server = thrift.createWebServer(options);
108}
109
Daniel Shihe41de0f2018-03-21 08:28:38 +0800110if (domainSocket) {
111 server.listen(domainSocket);
112} else if (type === 'tcp' || type === 'multiplex' || type === 'http' || type === 'websocket') {
113 server.listen(port);
114}