blob: dc380b2810f7e41d889fb8219a4a8d0561827752 [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
40func StartServer(
41 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,
Jens Geyerf4598682014-05-08 23:18:44 +020048 handler thrifttest.ThriftTest) (srv *thrift.TSimpleServer, err error) {
49
50 hostPort := fmt.Sprintf("%s:%d", host, port)
51
52 var protocolFactory thrift.TProtocolFactory
53 switch protocol {
54 case "compact":
55 protocolFactory = thrift.NewTCompactProtocolFactory()
56 case "simplejson":
57 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
58 case "json":
59 protocolFactory = thrift.NewTJSONProtocolFactory()
60 case "binary":
61 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
62 default:
63 return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
64 }
65 if debugServerProtocol {
66 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
67 }
68
69 var serverTransport thrift.TServerTransport
70 if ssl {
71 cfg := new(tls.Config)
72 if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
73 return nil, err
74 } else {
75 cfg.Certificates = append(cfg.Certificates, cert)
76 }
77 serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg)
78 } else {
79 if domain_socket != "" {
80 serverTransport, err = thrift.NewTServerSocket(domain_socket)
81 } else {
82 serverTransport, err = thrift.NewTServerSocket(hostPort)
83 }
84 }
85 if err != nil {
86 return nil, err
87 }
88
89 var transportFactory thrift.TTransportFactory
90
91 switch transport {
92 case "http":
93 return nil, fmt.Errorf("Http server transport is not supported")
94 // trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
95 // if err != nil {
96 // return nil, err
97 // }
98 case "framed":
99 transportFactory = thrift.NewTTransportFactory()
100 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
101 case "buffered":
102 transportFactory = thrift.NewTBufferedTransportFactory(8192)
Jens Geyerc6b991f2015-08-07 23:41:09 +0200103 case "zlib":
104 transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
Jens Geyerf4598682014-05-08 23:18:44 +0200105 case "":
106 transportFactory = thrift.NewTTransportFactory()
107 default:
108 return nil, fmt.Errorf("Invalid transport specified %s", transport)
109 }
110 processor := thrifttest.NewThriftTestProcessor(handler)
111 server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
112 if err = server.Listen(); err != nil {
113 return
114 }
115 go server.AcceptLoop()
116 return server, nil
117}