blob: 6e5cdfa7a617ef5acfda03257bd2967ded67d427 [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
Roger Meier47accfc2015-03-24 21:01:46 +010035 .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json|compact)', 'binary')
36 .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed)', '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)
henrique31236232014-02-23 20:16:44 +010039 .option('--promise', 'test with promise style functions')
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080040 .option('-t, --type <type>', 'Select server type (tcp|multiplex|http)', 'tcp')
Roger Meier8909cbd2014-01-26 11:44:27 +010041 .parse(process.argv);
42
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080043var port = program.port;
44var type = program.type;
45var ssl = program.ssl;
46var promise = program.promise;
Roger Meier8909cbd2014-01-26 11:44:27 +010047
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080048var handler = program.promise ? ThriftTestHandler : ThriftTestHandlerPromise;
henrique31236232014-02-23 20:16:44 +010049
Roger Meier57b354b2014-02-22 01:01:58 +010050var options = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080051 transport: helpers.transports[program.transport],
52 protocol: helpers.protocols[program.protocol]
Roger Meier57b354b2014-02-22 01:01:58 +010053};
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080054
55if (type === 'http' || type ==='websocket') {
56 options.handler = handler;
57 options.processor = ThriftTest;
58
59 options = {
Randy Abernethyd8187c52015-02-16 01:25:53 -080060 services: { "/test": options },
61 cors: {
62 '*': true
63 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080064 }
65}
66
67if (type === 'multiplex') {
68 var SecondServiceHandler = {
69 secondtestString: function(thing, result) {
70 console.log('testString(\'' + thing + '\')');
71 result(null, thing);
72 }
73 };
74
75 var processor = new thrift.MultiplexedProcessor();
76
77 processor.registerProcessor("ThriftTest",
78 new ThriftTest.Processor(ThriftTestHandler));
79
80 processor.registerProcessor("SecondService",
81 new SecondService.Processor(SecondServiceHandler));
82
83}
84
85if (ssl) {
Randy Abernethyd60f9782014-03-28 10:36:38 -070086 options.tls = {
87 key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
88 cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
ra779b9ac2014-04-23 20:04:23 -070089 };
Roger Meier57b354b2014-02-22 01:01:58 +010090}
Randy Abernethyd60f9782014-03-28 10:36:38 -070091
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080092var server;
93if (type === 'tcp') {
94 server = thrift.createServer(ThriftTest, handler, options);
95} else if (type === 'multiplex') {
96 server = thrift.createMultiplexServer(processor, options);
97} else if (type === 'http' || type === 'websocket') {
98 server = thrift.createWebServer(options);
99}
100
101server.listen(port);