blob: 5ac44000298c5f82fe1a2fc4292ab06748a74b2e [file] [log] [blame]
Jens Geyerf4598682014-05-08 23:18:44 +02001/*
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
20package common
21
22import (
Jens Geyerc6b991f2015-08-07 23:41:09 +020023 "compress/zlib"
Jens Geyerf4598682014-05-08 23:18:44 +020024 "crypto/tls"
25 "flag"
26 "fmt"
27 "gen/thrifttest"
28 "thrift"
29)
30
31var (
32 debugServerProtocol bool
33 certPath string
34)
35
36func init() {
37 flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on")
38}
39
claudemirof8ca0552016-01-10 23:31:30 -020040func GetServerParams(
Jens Geyerf4598682014-05-08 23:18:44 +020041 host string,
42 port int64,
43 domain_socket string,
44 transport string,
45 protocol string,
46 ssl bool,
Roger Meier41ad4342015-03-24 22:30:40 +010047 certPath string,
claudemirof8ca0552016-01-10 23:31:30 -020048 handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {
Jens Geyerf4598682014-05-08 23:18:44 +020049
claudemirof8ca0552016-01-10 23:31:30 -020050 var err error
Jens Geyerf4598682014-05-08 23:18:44 +020051 hostPort := fmt.Sprintf("%s:%d", host, port)
52
53 var protocolFactory thrift.TProtocolFactory
54 switch protocol {
55 case "compact":
56 protocolFactory = thrift.NewTCompactProtocolFactory()
57 case "simplejson":
58 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
59 case "json":
60 protocolFactory = thrift.NewTJSONProtocolFactory()
61 case "binary":
62 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
63 default:
claudemirof8ca0552016-01-10 23:31:30 -020064 return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020065 }
66 if debugServerProtocol {
67 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
68 }
69
70 var serverTransport thrift.TServerTransport
71 if ssl {
72 cfg := new(tls.Config)
73 if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
claudemirof8ca0552016-01-10 23:31:30 -020074 return nil, nil, nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020075 } else {
76 cfg.Certificates = append(cfg.Certificates, cert)
77 }
78 serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg)
79 } else {
80 if domain_socket != "" {
81 serverTransport, err = thrift.NewTServerSocket(domain_socket)
82 } else {
83 serverTransport, err = thrift.NewTServerSocket(hostPort)
84 }
85 }
86 if err != nil {
claudemirof8ca0552016-01-10 23:31:30 -020087 return nil, nil, nil, nil, err
Jens Geyerf4598682014-05-08 23:18:44 +020088 }
89
90 var transportFactory thrift.TTransportFactory
91
92 switch transport {
93 case "http":
claudemirof8ca0552016-01-10 23:31:30 -020094 // there is no such factory, and we don't need any
95 transportFactory = nil
Jens Geyerf4598682014-05-08 23:18:44 +020096 case "framed":
97 transportFactory = thrift.NewTTransportFactory()
98 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
99 case "buffered":
100 transportFactory = thrift.NewTBufferedTransportFactory(8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +0200101 case "zlib":
102 transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +0200103 case "":
104 transportFactory = thrift.NewTTransportFactory()
105 default:
claudemirof8ca0552016-01-10 23:31:30 -0200106 return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200107 }
108 processor := thrifttest.NewThriftTestProcessor(handler)
claudemirof8ca0552016-01-10 23:31:30 -0200109
110 return processor, serverTransport, transportFactory, protocolFactory, nil
Jens Geyerf4598682014-05-08 23:18:44 +0200111}