Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 1 | #!/usr/bin/env node |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 2 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 3 | /* |
| 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 Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 22 | var fs = require('fs'); |
| 23 | var path = require('path'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 24 | var thrift = require('../lib/thrift'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 25 | var program = require('commander'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 26 | var helpers = require('./helpers'); |
| 27 | |
| 28 | var ThriftTest = require('./gen-nodejs/ThriftTest'); |
| 29 | var SecondService = require('./gen-nodejs/SecondService'); |
| 30 | var ThriftTestHandler = require('./test_handler').AsyncThriftTestHandler; |
| 31 | var ThriftTestHandlerPromise = require('./test_handler').SyncThriftTestHandler; |
| 32 | var ttypes = require('./gen-nodejs/ThriftTest_types'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 33 | |
| 34 | program |
Roger Meier | 47accfc | 2015-03-24 21:01:46 +0100 | [diff] [blame^] | 35 | .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|json|compact)', 'binary') |
| 36 | .option('-t, --transport <transport>', 'Set thrift transport (buffered|framed)', 'buffered') |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 37 | .option('--ssl', 'use ssl transport') |
Roger Meier | 47accfc | 2015-03-24 21:01:46 +0100 | [diff] [blame^] | 38 | .option('--port <port>', 'Set thrift server port', 9090) |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 39 | .option('--promise', 'test with promise style functions') |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 40 | .option('-t, --type <type>', 'Select server type (tcp|multiplex|http)', 'tcp') |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 41 | .parse(process.argv); |
| 42 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 43 | var port = program.port; |
| 44 | var type = program.type; |
| 45 | var ssl = program.ssl; |
| 46 | var promise = program.promise; |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 47 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 48 | var handler = program.promise ? ThriftTestHandler : ThriftTestHandlerPromise; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 49 | |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 50 | var options = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 51 | transport: helpers.transports[program.transport], |
| 52 | protocol: helpers.protocols[program.protocol] |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 53 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 54 | |
| 55 | if (type === 'http' || type ==='websocket') { |
| 56 | options.handler = handler; |
| 57 | options.processor = ThriftTest; |
| 58 | |
| 59 | options = { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 60 | services: { "/test": options }, |
| 61 | cors: { |
| 62 | '*': true |
| 63 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | if (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 | |
| 85 | if (ssl) { |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 86 | options.tls = { |
| 87 | key: fs.readFileSync(path.resolve(__dirname, 'server.key')), |
| 88 | cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')) |
ra | 779b9ac | 2014-04-23 20:04:23 -0700 | [diff] [blame] | 89 | }; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 90 | } |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 91 | |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 92 | var server; |
| 93 | if (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 | |
| 101 | server.listen(port); |