Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * 'License'); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 20 | var fs = require('fs'); |
| 21 | var path = require('path'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 22 | var thrift = require('thrift'); |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 23 | var ThriftTest = require('./gen-nodejs/ThriftTest'); |
| 24 | var ThriftTestHandler = require('./test_handler').ThriftTestHandler; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 25 | var ThriftTestHandlerPromise = require('./test_handler_promise').ThriftTestHandler; |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 26 | |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 27 | var program = require('commander'); |
| 28 | |
| 29 | program |
| 30 | .option('-p, --protocol <protocol>', 'Set thift protocol (binary|json) [protocol]') |
| 31 | .option('-t, --transport <transport>', 'Set thift transport (buffered|framed) [transport]') |
Roger Meier | f8c1c98 | 2014-05-28 00:04:32 +0200 | [diff] [blame] | 32 | .option('--port <port>', 'Set thift server port') |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 33 | .option('--ssl', 'use ssl transport') |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 34 | .option('--promise', 'test with promise style functions') |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 35 | .parse(process.argv); |
| 36 | |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 37 | var transport = thrift.TBufferedTransport; |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 38 | if (program.transport === "framed") { |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 39 | transport = thrift.TFramedTransport; |
| 40 | } |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 41 | |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 42 | var protocol = thrift.TBinaryProtocol; |
| 43 | if (program.protocol === "json") { |
| 44 | protocol = thrift.TJSONProtocol; |
ra | 20aeba3 | 2014-05-11 00:25:01 -0700 | [diff] [blame] | 45 | } else if (program.protocol === "compact") { |
| 46 | protocol = thrift.TCompactProtocol; |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Roger Meier | f8c1c98 | 2014-05-28 00:04:32 +0200 | [diff] [blame] | 49 | var port = 9090; |
| 50 | if (String(program.port) === "undefined"){ |
| 51 | } else { |
| 52 | port = program.port; |
| 53 | } |
| 54 | |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 55 | var handler = ThriftTestHandler; |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 56 | if (program.promise) { |
| 57 | handler = ThriftTestHandlerPromise; |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 58 | } |
henrique | 3123623 | 2014-02-23 20:16:44 +0100 | [diff] [blame] | 59 | |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 60 | var options = { |
Roger Meier | 8909cbd | 2014-01-26 11:44:27 +0100 | [diff] [blame] | 61 | protocol: protocol, |
| 62 | transport: transport |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 63 | }; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 64 | if (program.ssl) { |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 65 | options.tls = { |
| 66 | key: fs.readFileSync(path.resolve(__dirname, 'server.key')), |
| 67 | cert: fs.readFileSync(path.resolve(__dirname, 'server.crt')) |
ra | 779b9ac | 2014-04-23 20:04:23 -0700 | [diff] [blame] | 68 | }; |
Roger Meier | 57b354b | 2014-02-22 01:01:58 +0100 | [diff] [blame] | 69 | } |
Roger Meier | f8c1c98 | 2014-05-28 00:04:32 +0200 | [diff] [blame] | 70 | thrift.createServer(ThriftTest, handler, options).listen(port); |
Randy Abernethy | d60f978 | 2014-03-28 10:36:38 -0700 | [diff] [blame] | 71 | |