...
1 package gateway
2
3 import (
4 "context"
5 "fmt"
6 "net"
7 "net/http"
8 "time"
9
10 "github.com/grpc-ecosystem/grpc-gateway/examples/internal/proto/examplepb"
11 gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
12 "google.golang.org/grpc"
13 )
14
15
16 func newGateway(ctx context.Context, conn *grpc.ClientConn, opts []gwruntime.ServeMuxOption) (http.Handler, error) {
17
18 mux := gwruntime.NewServeMux(opts...)
19
20 for _, f := range []func(context.Context, *gwruntime.ServeMux, *grpc.ClientConn) error{
21 examplepb.RegisterEchoServiceHandler,
22 examplepb.RegisterStreamServiceHandler,
23 examplepb.RegisterABitOfEverythingServiceHandler,
24 examplepb.RegisterFlowCombinationHandler,
25 examplepb.RegisterNonStandardServiceHandler,
26 examplepb.RegisterResponseBodyServiceHandler,
27 } {
28 if err := f(ctx, mux, conn); err != nil {
29 return nil, err
30 }
31 }
32 return mux, nil
33 }
34
35 func dial(ctx context.Context, network, addr string) (*grpc.ClientConn, error) {
36 switch network {
37 case "tcp":
38 return dialTCP(ctx, addr)
39 case "unix":
40 return dialUnix(ctx, addr)
41 default:
42 return nil, fmt.Errorf("unsupported network type %q", network)
43 }
44 }
45
46
47
48 func dialTCP(ctx context.Context, addr string) (*grpc.ClientConn, error) {
49 return grpc.DialContext(ctx, addr, grpc.WithInsecure())
50 }
51
52
53
54 func dialUnix(ctx context.Context, addr string) (*grpc.ClientConn, error) {
55 d := func(addr string, timeout time.Duration) (net.Conn, error) {
56 return net.DialTimeout("unix", addr, timeout)
57 }
58 return grpc.DialContext(ctx, addr, grpc.WithInsecure(), grpc.WithDialer(d))
59 }
60
View as plain text