Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [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 | |
| 20 | package common |
| 21 | |
| 22 | import ( |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 23 | "compress/zlib" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 24 | "crypto/tls" |
| 25 | "flag" |
| 26 | "fmt" |
Yuxuan 'fishy' Wang | b71f11e | 2021-03-22 15:01:00 -0700 | [diff] [blame] | 27 | |
| 28 | "github.com/apache/thrift/lib/go/thrift" |
| 29 | "github.com/apache/thrift/test/go/src/gen/thrifttest" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | debugServerProtocol bool |
| 34 | certPath string |
| 35 | ) |
| 36 | |
| 37 | func init() { |
| 38 | flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on") |
| 39 | } |
| 40 | |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 41 | func GetServerParams( |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 42 | host string, |
| 43 | port int64, |
| 44 | domain_socket string, |
| 45 | transport string, |
| 46 | protocol string, |
| 47 | ssl bool, |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 48 | certPath string, |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 49 | handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 50 | |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 51 | var err error |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 52 | hostPort := fmt.Sprintf("%s:%d", host, port) |
| 53 | |
| 54 | var protocolFactory thrift.TProtocolFactory |
| 55 | switch protocol { |
| 56 | case "compact": |
| 57 | protocolFactory = thrift.NewTCompactProtocolFactory() |
| 58 | case "simplejson": |
| 59 | protocolFactory = thrift.NewTSimpleJSONProtocolFactory() |
| 60 | case "json": |
| 61 | protocolFactory = thrift.NewTJSONProtocolFactory() |
| 62 | case "binary": |
| 63 | protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 64 | case "header": |
| 65 | protocolFactory = thrift.NewTHeaderProtocolFactory() |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 66 | default: |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 67 | return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 68 | } |
| 69 | if debugServerProtocol { |
| 70 | protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:") |
| 71 | } |
| 72 | |
| 73 | var serverTransport thrift.TServerTransport |
| 74 | if ssl { |
| 75 | cfg := new(tls.Config) |
| 76 | if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil { |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 77 | return nil, nil, nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 78 | } else { |
| 79 | cfg.Certificates = append(cfg.Certificates, cert) |
| 80 | } |
| 81 | serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg) |
| 82 | } else { |
| 83 | if domain_socket != "" { |
| 84 | serverTransport, err = thrift.NewTServerSocket(domain_socket) |
| 85 | } else { |
| 86 | serverTransport, err = thrift.NewTServerSocket(hostPort) |
| 87 | } |
| 88 | } |
| 89 | if err != nil { |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 90 | return nil, nil, nil, nil, err |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | var transportFactory thrift.TTransportFactory |
| 94 | |
| 95 | switch transport { |
| 96 | case "http": |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 97 | // there is no such factory, and we don't need any |
| 98 | transportFactory = nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 99 | case "framed": |
| 100 | transportFactory = thrift.NewTTransportFactory() |
| 101 | transportFactory = thrift.NewTFramedTransportFactory(transportFactory) |
| 102 | case "buffered": |
| 103 | transportFactory = thrift.NewTBufferedTransportFactory(8192) |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 104 | case "zlib": |
| 105 | transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 106 | case "": |
| 107 | transportFactory = thrift.NewTTransportFactory() |
| 108 | default: |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 109 | return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 110 | } |
| 111 | processor := thrifttest.NewThriftTestProcessor(handler) |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 112 | |
| 113 | return processor, serverTransport, transportFactory, protocolFactory, nil |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 114 | } |