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