]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/go/arrow/flight/example_flight_server_test.go
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / go / arrow / flight / example_flight_server_test.go
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package flight_test
18
19import (
20 "context"
21 "fmt"
22 "io"
23 "log"
24
25 "github.com/apache/arrow/go/v6/arrow/flight"
26 "google.golang.org/grpc"
27 "google.golang.org/grpc/codes"
28 "google.golang.org/grpc/status"
29)
30
31type serverAuth struct{}
32
33func (sa *serverAuth) Authenticate(c flight.AuthConn) error {
34 in, err := c.Read()
35 if err == io.EOF {
36 return status.Error(codes.Unauthenticated, "no auth info provided")
37 }
38
39 if err != nil {
40 return status.Error(codes.FailedPrecondition, "error reading auth handshake")
41 }
42
43 // do something with in....
44 fmt.Println(string(in))
45
46 // send auth token back
47 return c.Send([]byte("foobar"))
48}
49
50func (sa *serverAuth) IsValid(token string) (interface{}, error) {
51 if token == "foobar" {
52 return "foo", nil
53 }
54 return "", status.Error(codes.PermissionDenied, "invalid auth token")
55}
56
57func Example_server() {
58 server := flight.NewFlightServer(&serverAuth{})
59 server.Init("localhost:0")
60 server.RegisterFlightService(&flight.FlightServiceService{})
61
62 go server.Serve()
63 defer server.Shutdown()
64
65 conn, err := grpc.Dial(server.Addr().String(), grpc.WithInsecure())
66 if err != nil {
67 log.Fatal(err)
68 }
69 defer conn.Close()
70
71 client := flight.NewFlightServiceClient(conn)
72 stream, err := client.Handshake(context.Background())
73 if err != nil {
74 log.Fatal(err)
75 }
76
77 // ignore error handling here for brevity
78 stream.Send(&flight.HandshakeRequest{Payload: []byte("baz")})
79
80 resp, _ := stream.Recv()
81 fmt.Println(string(resp.Payload))
82
83 // Output:
84 // baz
85 // foobar
86}