From cd85be76db6189fbe4a69e9d4517d7ae78ea8085 Mon Sep 17 00:00:00 2001 From: Jeff Wendling Date: Thu, 22 Sep 2022 10:31:38 -0400 Subject: [PATCH] examples: fix some small bugs - route before calling Run instead of concurrently - fix the http client parsing of json response Fixes #28 Change-Id: Iafe341cf67984abedd440eb1f13b72c12f081a97 --- examples/drpc_and_http/http_client/main.go | 21 ++++----------------- examples/drpc_and_http/server/main.go | 10 ++++++---- examples/grpc_and_drpc/server/main.go | 10 ++++++---- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/examples/drpc_and_http/http_client/main.go b/examples/drpc_and_http/http_client/main.go index 83859e7..b045d17 100644 --- a/examples/drpc_and_http/http_client/main.go +++ b/examples/drpc_and_http/http_client/main.go @@ -36,29 +36,16 @@ func Main(ctx context.Context) error { // parse the response var data struct { - Status string `json:"status"` - - Response struct { - Cookie struct { - Type string `json:"type"` - } `json:"cookie"` - } `json:"response"` - - Error string `json:"error"` - Code int `json:"code"` + Cookie struct { + Type string `json:"type"` + } `json:"cookie"` } err = json.NewDecoder(resp.Body).Decode(&data) if err != nil { return err } - // confirm the rpc layer worked okay - if data.Status != "ok" { - return fmt.Errorf("unexpected rpc status %q %q %q", - data.Status, data.Code, data.Error) - } - // check the results - _, err = fmt.Println(data.Response.Cookie.Type) + _, err = fmt.Println(data.Cookie.Type) return err } diff --git a/examples/drpc_and_http/server/main.go b/examples/drpc_and_http/server/main.go index ae3a458..fc652f1 100644 --- a/examples/drpc_and_http/server/main.go +++ b/examples/drpc_and_http/server/main.go @@ -9,6 +9,7 @@ import ( "net/http" "golang.org/x/sync/errgroup" + "storj.io/drpc/drpchttp" "storj.io/drpc/drpcmigrate" "storj.io/drpc/drpcmux" @@ -58,6 +59,10 @@ func Main(ctx context.Context) error { // create a listen mux that evalutes enough bytes to recognize the DRPC header lisMux := drpcmigrate.NewListenMux(lis, len(drpcmigrate.DRPCHeader)) + // grap the listen mux route for the DRPC Header and default handler + drpcLis := lisMux.Route(drpcmigrate.DRPCHeader) + httpLis := lisMux.Default() + // we're going to run the different protocol servers in parallel, so // make an errgroup var group errgroup.Group @@ -67,9 +72,6 @@ func Main(ctx context.Context) error { // create a drpc server s := drpcserver.New(m) - // grap the listen mux route for the DRPC Header - drpcLis := lisMux.Route(drpcmigrate.DRPCHeader) - // run the server // N.B.: if you want TLS, you need to wrap the drpcLis net.Listener // with TLS before passing to Serve here. @@ -82,7 +84,7 @@ func Main(ctx context.Context) error { s := http.Server{Handler: drpchttp.New(m)} // run the server - return s.Serve(lisMux.Default()) + return s.Serve(httpLis) }) // run the listen mux diff --git a/examples/grpc_and_drpc/server/main.go b/examples/grpc_and_drpc/server/main.go index ea68502..16c2c7a 100644 --- a/examples/grpc_and_drpc/server/main.go +++ b/examples/grpc_and_drpc/server/main.go @@ -9,6 +9,7 @@ import ( "golang.org/x/sync/errgroup" "google.golang.org/grpc" + "storj.io/drpc/drpcmigrate" "storj.io/drpc/drpcmux" "storj.io/drpc/drpcserver" @@ -48,6 +49,10 @@ func Main(ctx context.Context) error { // create a listen mux that evalutes enough bytes to recognize the DRPC header lisMux := drpcmigrate.NewListenMux(lis, len(drpcmigrate.DRPCHeader)) + // grap the listen mux route for the DRPC Header and default listener + drpcLis := lisMux.Route(drpcmigrate.DRPCHeader) + grpcLis := lisMux.Default() + // we're going to run the different protocol servers in parallel, so // make an errgroup var group errgroup.Group @@ -61,7 +66,7 @@ func Main(ctx context.Context) error { pb.RegisterCookieMonsterServer(s, cookieMonster) // run the server - return s.Serve(lisMux.Default()) + return s.Serve(grpcLis) }) // drpc handling @@ -78,9 +83,6 @@ func Main(ctx context.Context) error { // create a drpc server s := drpcserver.New(m) - // grap the listen mux route for the DRPC Header - drpcLis := lisMux.Route(drpcmigrate.DRPCHeader) - // run the server // N.B.: if you want TLS, you need to wrap the drpcLis net.Listener // with TLS before passing to Serve here.