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 main |
| 21 | |
| 22 | import ( |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 23 | "flag" |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 24 | "fmt" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 25 | "log" |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 26 | "net/http" |
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/common" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var host = flag.String("host", "localhost", "Host to connect") |
| 33 | var port = flag.Int64("port", 9090, "Port number to connect") |
| 34 | var domain_socket = flag.String("domain-socket", "", "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port") |
Jens Geyer | c6b991f | 2015-08-07 23:41:09 +0200 | [diff] [blame] | 35 | var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http, zlib") |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 36 | var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json, header") |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 37 | var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") |
James E. King III | b2b767e | 2018-09-15 20:32:04 +0000 | [diff] [blame] | 38 | var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib") |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 39 | var certPath = flag.String("certPath", "keys", "Directory that contains SSL certificates") |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 40 | |
| 41 | func main() { |
| 42 | flag.Parse() |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 43 | |
| 44 | processor, serverTransport, transportFactory, protocolFactory, err := common.GetServerParams(*host, *port, *domain_socket, *transport, *protocol, *ssl, *certPath, common.PrintingHandler) |
| 45 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 46 | if err != nil { |
Yuxuan 'fishy' Wang | 4d46c11 | 2019-06-07 20:47:18 +0800 | [diff] [blame] | 47 | log.Fatalf("Unable to process server params: %v", err) |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 48 | } |
claudemiro | f8ca055 | 2016-01-10 23:31:30 -0200 | [diff] [blame] | 49 | |
| 50 | if *transport == "http" { |
| 51 | http.HandleFunc("/", thrift.NewThriftHandlerFunc(processor, protocolFactory, protocolFactory)) |
| 52 | |
| 53 | if *ssl { |
| 54 | err := http.ListenAndServeTLS(fmt.Sprintf(":%d", *port), |
| 55 | *certPath+"/server.pem", *certPath+"/server.key", nil) |
| 56 | |
| 57 | if err != nil { |
| 58 | fmt.Println(err) |
| 59 | return |
| 60 | } |
| 61 | } else { |
| 62 | http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) |
| 63 | } |
| 64 | } else { |
| 65 | server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) |
| 66 | if err = server.Listen(); err != nil { |
| 67 | return |
| 68 | } |
| 69 | go server.AcceptLoop() |
| 70 | server.Serve() |
| 71 | } |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 72 | } |