blob: d354b320622aff531314cf2efc10c47534e860e4 [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 (
23 "crypto/tls"
24 "flag"
25 "fmt"
26 "gen/thrifttest"
27 "thrift"
28)
29
30var (
31 debugServerProtocol bool
32 certPath string
33)
34
35func init() {
36 flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on")
37}
38
39func StartServer(
40 host string,
41 port int64,
42 domain_socket string,
43 transport string,
44 protocol string,
45 ssl bool,
Roger Meier41ad4342015-03-24 22:30:40 +010046 certPath string,
Jens Geyerf4598682014-05-08 23:18:44 +020047 handler thrifttest.ThriftTest) (srv *thrift.TSimpleServer, err error) {
48
49 hostPort := fmt.Sprintf("%s:%d", host, port)
50
51 var protocolFactory thrift.TProtocolFactory
52 switch protocol {
53 case "compact":
54 protocolFactory = thrift.NewTCompactProtocolFactory()
55 case "simplejson":
56 protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
57 case "json":
58 protocolFactory = thrift.NewTJSONProtocolFactory()
59 case "binary":
60 protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
61 default:
62 return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
63 }
64 if debugServerProtocol {
65 protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
66 }
67
68 var serverTransport thrift.TServerTransport
69 if ssl {
70 cfg := new(tls.Config)
71 if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
72 return nil, err
73 } else {
74 cfg.Certificates = append(cfg.Certificates, cert)
75 }
76 serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg)
77 } else {
78 if domain_socket != "" {
79 serverTransport, err = thrift.NewTServerSocket(domain_socket)
80 } else {
81 serverTransport, err = thrift.NewTServerSocket(hostPort)
82 }
83 }
84 if err != nil {
85 return nil, err
86 }
87
88 var transportFactory thrift.TTransportFactory
89
90 switch transport {
91 case "http":
92 return nil, fmt.Errorf("Http server transport is not supported")
93 // trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
94 // if err != nil {
95 // return nil, err
96 // }
97 case "framed":
98 transportFactory = thrift.NewTTransportFactory()
99 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
100 case "buffered":
101 transportFactory = thrift.NewTBufferedTransportFactory(8192)
102 case "":
103 transportFactory = thrift.NewTTransportFactory()
104 default:
105 return nil, fmt.Errorf("Invalid transport specified %s", transport)
106 }
107 processor := thrifttest.NewThriftTestProcessor(handler)
108 server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
109 if err = server.Listen(); err != nil {
110 return
111 }
112 go server.AcceptLoop()
113 return server, nil
114}