blob: 0af2702f04eb12d6f0029c4f93f4f40e247d7711 [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"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070027
28 "github.com/apache/thrift/lib/go/thrift"
29 "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020030)
31
32var (
33 debugServerProtocol bool
Jens Geyerf4598682014-05-08 23:18:44 +020034)
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,
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070048 handler thrifttest.ThriftTest,
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070049) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, string /* addr */, error) {
Jens Geyerf4598682014-05-08 23:18:44 +020050
claudemirof8ca0552016-01-10 23:31:30 -020051 var err error
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070052 hostPort := fmt.Sprintf("%s:0", host)
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070053 var cfg *thrift.TConfiguration = nil
Jens Geyerf4598682014-05-08 23:18:44 +020054
55 var protocolFactory thrift.TProtocolFactory
56 switch protocol {
57 case "compact":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070058 protocolFactory = thrift.NewTCompactProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020059 case "simplejson":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070060 protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(cfg)
Jens Geyerf4598682014-05-08 23:18:44 +020061 case "json":
62 protocolFactory = thrift.NewTJSONProtocolFactory()
63 case "binary":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070064 protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
Yuxuan 'fishy' Wang4d46c112019-06-07 20:47:18 +080065 case "header":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070066 protocolFactory = thrift.NewTHeaderProtocolFactoryConf(nil)
Jens Geyerf4598682014-05-08 23:18:44 +020067 default:
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070068 return nil, nil, nil, nil, "", fmt.Errorf("invalid protocol specified %s", protocol)
Jens Geyerf4598682014-05-08 23:18:44 +020069 }
70 if debugServerProtocol {
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070071 protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "server:", thrift.StdLogger(nil))
Jens Geyerf4598682014-05-08 23:18:44 +020072 }
73
74 var serverTransport thrift.TServerTransport
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070075 var addr string
Jens Geyerf4598682014-05-08 23:18:44 +020076 if ssl {
77 cfg := new(tls.Config)
78 if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070079 return nil, nil, nil, nil, "", err
Jens Geyerf4598682014-05-08 23:18:44 +020080 } else {
81 cfg.Certificates = append(cfg.Certificates, cert)
82 }
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070083 transport, transportErr := thrift.NewTSSLServerSocket(hostPort, cfg)
84 if transportErr == nil {
85 listenErr := transport.Listen()
86 if listenErr == nil {
87 serverTransport = transport
88 addr = transport.Addr().String()
89 } else {
90 err = listenErr
91 }
92 } else {
93 err = transportErr
94 }
Jens Geyerf4598682014-05-08 23:18:44 +020095 } else {
96 if domain_socket != "" {
97 serverTransport, err = thrift.NewTServerSocket(domain_socket)
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070098 addr = domain_socket
Jens Geyerf4598682014-05-08 23:18:44 +020099 } else {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700100 transport, transportErr := thrift.NewTServerSocket(hostPort)
101 if transportErr == nil {
102 listenErr := transport.Listen()
103 if listenErr == nil {
104 serverTransport = transport
105 addr = transport.Addr().String()
106 } else {
107 err = listenErr
108 }
109 } else {
110 err = transportErr
111 }
Jens Geyerf4598682014-05-08 23:18:44 +0200112 }
113 }
114 if err != nil {
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700115 return nil, nil, nil, nil, "", err
Jens Geyerf4598682014-05-08 23:18:44 +0200116 }
117
118 var transportFactory thrift.TTransportFactory
119
120 switch transport {
121 case "http":
claudemirof8ca0552016-01-10 23:31:30 -0200122 // there is no such factory, and we don't need any
123 transportFactory = nil
Jens Geyerf4598682014-05-08 23:18:44 +0200124 case "framed":
125 transportFactory = thrift.NewTTransportFactory()
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700126 transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, nil)
Jens Geyerf4598682014-05-08 23:18:44 +0200127 case "buffered":
128 transportFactory = thrift.NewTBufferedTransportFactory(8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +0200129 case "zlib":
130 transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +0200131 case "":
132 transportFactory = thrift.NewTTransportFactory()
133 default:
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700134 return nil, nil, nil, nil, "", fmt.Errorf("invalid transport specified %s", transport)
Jens Geyerf4598682014-05-08 23:18:44 +0200135 }
136 processor := thrifttest.NewThriftTestProcessor(handler)
claudemirof8ca0552016-01-10 23:31:30 -0200137
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -0700138 return processor, serverTransport, transportFactory, protocolFactory, addr, nil
Jens Geyerf4598682014-05-08 23:18:44 +0200139}