Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame^] | 1 | #!/usr/bin/env nodejs |
| 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'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 24 | var thrift = require('thrift'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 25 | var ThriftTest = require('./gen-nodejs/ThriftTest'); |
| 26 | var ThriftTestHandler = require('./test_handler').ThriftTestHandler; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 27 | var ThriftTestHandlerPromise = require('./test_handler_promise').ThriftTestHandler; |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 28 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 29 | var program = require('commander'); |
| 30 | |
| 31 | program |
| 32 | .option('-p, --protocol <protocol>', 'Set thift protocol (binary|json) [protocol]') |
| 33 | .option('-t, --transport <transport>', 'Set thift transport (buffered|framed) [transport]') |
Roger Meier | f8c1c98 | 2014-05-28 00:04:32 +0200 | [diff] [blame] | 34 | .option('--port <port>', 'Set thift server port') |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 35 | .option('--ssl', 'use ssl transport') |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 36 | .option('--promise', 'test with promise style functions') |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 37 | .parse(process.argv); |
| 38 | |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 39 | var transport = thrift.TBufferedTransport; |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 40 | if (program.transport === "framed") { |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 41 | transport = thrift.TFramedTransport; |
| 42 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 43 | |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 44 | var protocol = thrift.TBinaryProtocol; |
| 45 | if (program.protocol === "json") { |
| 46 | protocol = thrift.TJSONProtocol; |
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame] | 47 | } else if (program.protocol === "compact") { |
| 48 | protocol = thrift.TCompactProtocol; |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Roger Meier | f8c1c98 | 2014-05-28 00:04:32 +0200 | [diff] [blame] | 51 | var port = 9090; |
| 52 | if (String(program.port) === "undefined"){ |
| 53 | } else { |
| 54 | port = program.port; |
| 55 | } |
| 56 | |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 57 | var handler = ThriftTestHandler; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 58 | if (program.promise) { |
| 59 | handler = ThriftTestHandlerPromise; |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 60 | } |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 61 | |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 62 | var options = { |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 63 | protocol: protocol, |
| 64 | transport: transport |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 65 | }; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 66 | if (program.ssl) { |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 67 | options.tls = { |
| 68 | key: fs.readFileSync(path.resolve(__dirname, 'server.key')), |
| 69 | cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')) |
ra | 779b9ac | 2014-04-23 20:04:23 -0700 | [diff] [blame] | 70 | }; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 71 | } |
Roger Meier | f8c1c98 | 2014-05-28 00:04:32 +0200 | [diff] [blame] | 72 | thrift.createServer(ThriftTest, handler, options).listen(port); |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 73 | |