Skip to content

Commit

Permalink
examples: fix some small bugs
Browse files Browse the repository at this point in the history
- route before calling Run instead of concurrently
- fix the http client parsing of json response

Fixes #28

Change-Id: Iafe341cf67984abedd440eb1f13b72c12f081a97
  • Loading branch information
zeebo committed Sep 22, 2022
1 parent 04b0ec3 commit cd85be7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
21 changes: 4 additions & 17 deletions examples/drpc_and_http/http_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 6 additions & 4 deletions examples/drpc_and_http/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"

"golang.org/x/sync/errgroup"

"storj.io/drpc/drpchttp"
"storj.io/drpc/drpcmigrate"
"storj.io/drpc/drpcmux"
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions examples/grpc_and_drpc/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit cd85be7

Please sign in to comment.