diff --git a/addsvc/thrift/compile.sh b/addsvc/thrift/compile.sh index 2ecce5b29..293e68e0e 100755 --- a/addsvc/thrift/compile.sh +++ b/addsvc/thrift/compile.sh @@ -1,5 +1,18 @@ #!/usr/bin/env sh -# See also https://thrift.apache.org/tutorial/go +# See also https://thrift.apache.org/tutorial/go. +# +# An old version can be obtained via `brew install thrift`. +# For the latest, here's the annoying dance: +# +# brew install automake bison pkg-config openssl +# ln -s /usr/local/opt/openssl/include/openssl /usr/local/include # if it isn't already +# git clone git@github.com:apache/thrift +# ./bootstrap.sh +# bash +# export PATH=/usr/local/Cellar/bison/*/bin:$PATH +# ./configure ./configure --without-qt4 --without-qt5 --without-c_glib --without-csharp --without-java --without-erlang --without-nodejs --without-lua --without-python --without-perl --without-php --without-php_extension --without-dart --without-ruby --without-haskell --without-rs --without-cl --without-haxe --without-dotnetcore --without-d +# make +# sudo make install thrift -r --gen "go:package_prefix=github.com/go-kit/kit/examples/addsvc/thrift/gen-go/,thrift_import=github.com/apache/thrift/lib/go/thrift" addsvc.thrift diff --git a/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go b/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go index 8ef18eafd..9e6800e72 100755 --- a/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go +++ b/addsvc/thrift/gen-go/addsvc/add_service-remote/add_service-remote.go @@ -28,6 +28,22 @@ func Usage() { os.Exit(0) } +type httpHeaders map[string]string + +func (h httpHeaders) String() string { + var m map[string]string = h + return fmt.Sprintf("%s", m) +} + +func (h httpHeaders) Set(value string) error { + parts := strings.Split(value, ": ") + if len(parts) != 2 { + return fmt.Errorf("header should be of format 'Key: Value'") + } + h[parts[0]] = parts[1] + return nil +} + func main() { flag.Usage = Usage var host string @@ -36,7 +52,8 @@ func main() { var urlString string var framed bool var useHttp bool - var parsedUrl url.URL + headers := make(httpHeaders) + var parsedUrl *url.URL var trans thrift.TTransport _ = strconv.Atoi _ = math.Abs @@ -47,16 +64,18 @@ func main() { flag.StringVar(&urlString, "u", "", "Specify the url") flag.BoolVar(&framed, "framed", false, "Use framed transport") flag.BoolVar(&useHttp, "http", false, "Use http") + flag.Var(headers, "H", "Headers to set on the http(s) request (e.g. -H \"Key: Value\")") flag.Parse() if len(urlString) > 0 { - parsedUrl, err := url.Parse(urlString) + var err error + parsedUrl, err = url.Parse(urlString) if err != nil { fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) flag.Usage() } host = parsedUrl.Host - useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" + useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" || parsedUrl.Scheme == "https" } else if useHttp { _, err := url.Parse(fmt.Sprint("http://", host, ":", port)) if err != nil { @@ -69,6 +88,12 @@ func main() { var err error if useHttp { trans, err = thrift.NewTHttpClient(parsedUrl.String()) + if len(headers) > 0 { + httptrans := trans.(*thrift.THttpClient) + for key, value := range headers { + httptrans.SetHeader(key, value) + } + } } else { portStr := fmt.Sprint(port) if strings.Contains(host, ":") { diff --git a/addsvc/thrift/gen-go/addsvc/addsvc-consts.go b/addsvc/thrift/gen-go/addsvc/addsvc-consts.go index 3222f5103..eea54fe19 100644 --- a/addsvc/thrift/gen-go/addsvc/addsvc-consts.go +++ b/addsvc/thrift/gen-go/addsvc/addsvc-consts.go @@ -5,8 +5,8 @@ package addsvc import ( "bytes" - "reflect" "context" + "reflect" "fmt" "github.com/apache/thrift/lib/go/thrift" ) diff --git a/addsvc/thrift/gen-go/addsvc/addsvc.go b/addsvc/thrift/gen-go/addsvc/addsvc.go index 729ad6226..14a87e6d2 100644 --- a/addsvc/thrift/gen-go/addsvc/addsvc.go +++ b/addsvc/thrift/gen-go/addsvc/addsvc.go @@ -5,8 +5,8 @@ package addsvc import ( "bytes" - "reflect" "context" + "reflect" "fmt" "github.com/apache/thrift/lib/go/thrift" ) @@ -287,14 +287,12 @@ type AddServiceClient struct { c thrift.TClient } -// Deprecated: Use NewAddService instead func NewAddServiceClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *AddServiceClient { return &AddServiceClient{ c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)), } } -// Deprecated: Use NewAddService instead func NewAddServiceClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *AddServiceClient { return &AddServiceClient{ c: thrift.NewTStandardClient(iprot, oprot), @@ -307,6 +305,9 @@ func NewAddServiceClient(c thrift.TClient) *AddServiceClient { } } +func (p *AddServiceClient) Client_() thrift.TClient { + return p.c +} // Parameters: // - A // - B @@ -315,7 +316,7 @@ func (p *AddServiceClient) Sum(ctx context.Context, a int64, b int64) (r *SumRep _args0.A = a _args0.B = b var _result1 AddServiceSumResult - if err = p.c.Call(ctx, "Sum", &_args0, &_result1); err != nil { + if err = p.Client_().Call(ctx, "Sum", &_args0, &_result1); err != nil { return } return _result1.GetSuccess(), nil @@ -329,7 +330,7 @@ func (p *AddServiceClient) Concat(ctx context.Context, a string, b string) (r *C _args2.A = a _args2.B = b var _result3 AddServiceConcatResult - if err = p.c.Call(ctx, "Concat", &_args2, &_result3); err != nil { + if err = p.Client_().Call(ctx, "Concat", &_args2, &_result3); err != nil { return } return _result3.GetSuccess(), nil @@ -373,7 +374,7 @@ func (p *AddServiceProcessor) Process(ctx context.Context, iprot, oprot thrift.T oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId) x5.Write(oprot) oprot.WriteMessageEnd() - oprot.Flush() + oprot.Flush(ctx) return false, x5 } @@ -390,7 +391,7 @@ func (p *addServiceProcessorSum) Process(ctx context.Context, seqId int32, iprot oprot.WriteMessageBegin("Sum", thrift.EXCEPTION, seqId) x.Write(oprot) oprot.WriteMessageEnd() - oprot.Flush() + oprot.Flush(ctx) return false, err } @@ -403,7 +404,7 @@ var retval *SumReply oprot.WriteMessageBegin("Sum", thrift.EXCEPTION, seqId) x.Write(oprot) oprot.WriteMessageEnd() - oprot.Flush() + oprot.Flush(ctx) return true, err2 } else { result.Success = retval @@ -417,7 +418,7 @@ var retval *SumReply if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { err = err2 } - if err2 = oprot.Flush(); err == nil && err2 != nil { + if err2 = oprot.Flush(ctx); err == nil && err2 != nil { err = err2 } if err != nil { @@ -438,7 +439,7 @@ func (p *addServiceProcessorConcat) Process(ctx context.Context, seqId int32, ip oprot.WriteMessageBegin("Concat", thrift.EXCEPTION, seqId) x.Write(oprot) oprot.WriteMessageEnd() - oprot.Flush() + oprot.Flush(ctx) return false, err } @@ -451,7 +452,7 @@ var retval *ConcatReply oprot.WriteMessageBegin("Concat", thrift.EXCEPTION, seqId) x.Write(oprot) oprot.WriteMessageEnd() - oprot.Flush() + oprot.Flush(ctx) return true, err2 } else { result.Success = retval @@ -465,7 +466,7 @@ var retval *ConcatReply if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { err = err2 } - if err2 = oprot.Flush(); err == nil && err2 != nil { + if err2 = oprot.Flush(ctx); err == nil && err2 != nil { err = err2 } if err != nil {