Skip to content

Commit

Permalink
Fixed ptr target for Options, better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
utrack committed Jul 5, 2018
1 parent 187a844 commit 4b2f5cb
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 15 deletions.
6 changes: 4 additions & 2 deletions cmd/protoc-gen-goclay/genhandler/tpl_servicedesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type {{ $svc.GetName }}Desc struct {
// New{{ $svc.GetName }}ServiceDesc creates new registrator for the {{ $svc.GetName }}Server.
// It implements httptransport.ConfigurableServiceDesc as well.
func New{{ $svc.GetName }}ServiceDesc(svc {{ $svc.GetName }}Server) *{{ $svc.GetName }}Desc {
return &{{ $svc.GetName }}Desc{svc:svc}
return &{{ $svc.GetName }}Desc{
svc:svc,
}
}
// RegisterGRPC implements service registrator interface.
Expand All @@ -25,7 +27,7 @@ func (d *{{ $svc.GetName }}Desc) RegisterGRPC(s *{{ pkg "grpc" }}Server) {
// Apply applies passed options.
func (d *{{ $svc.GetName }}Desc) Apply(oo ... {{ pkg "transport" }}DescOption) {
for _,o := range oo {
o.Apply(d.opts)
o.Apply(&d.opts)
}
}
Expand Down
1 change: 1 addition & 0 deletions doc/example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/utrack/clay/doc/example
replace github.com/utrack/clay/v2 v2.0.0 => ../../

require (
github.com/davecgh/go-spew v1.1.0
github.com/rakyll/statik v0.1.1
github.com/utrack/clay/v2 v2.0.0
golang.org/x/crypto v0.0.0-20180621125126-a49355c7e3f8
Expand Down
7 changes: 2 additions & 5 deletions doc/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/utrack/clay/v2/log"
"github.com/utrack/clay/v2/transport"
"github.com/utrack/clay/v2/transport/middlewares/mwgrpc"
"github.com/utrack/clay/v2/transport/middlewares/mwhttp"
"github.com/utrack/clay/v2/transport/server"
"golang.org/x/net/context"

Expand Down Expand Up @@ -60,10 +59,8 @@ func main() {
12345,
// Pass our mux with Swagger UI
server.WithHTTPMux(hmux),
// Recover from HTTP panics
server.WithHTTPMiddlewares(mwhttp.Recover(log.Default), mwhttp.CloseNotifier()),
// Recover from gRPC panics
server.WithGRPCUnaryMiddlewares(mwgrpc.UnaryPanicHandler(log.Default)),
// Recover from both HTTP and gRPC panics and use our own middleware
server.WithGRPCUnaryMiddlewares(mwgrpc.UnaryPanicHandler(log.Default), logmw),
)
err = srv.Run(impl)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions doc/example/pb/sum.pb.goclay.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/middlewares/mwgrpc/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func UnaryPanicHandler(logger interface{}) grpc.UnaryServerInterceptor {
defer func() {
if r := recover(); r != nil {
err = grpc.Errorf(codes.Internal, "panic: %v", r)
logFunc(ctx, fmt.Sprintf("recovered from panic: %v, %v ", r, string(debug.Stack())))
logFunc(ctx, fmt.Sprintf("recovered from panic: %v,\n%v ", r, string(debug.Stack())))
}
}()
return handler(ctx, req)
Expand Down
7 changes: 5 additions & 2 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type serverOpts struct {

HTTPMiddlewares []func(http.Handler) http.Handler

GRPCOpts []grpc.ServerOption
GRPCOpts []grpc.ServerOption
GRPCUnaryInterceptor grpc.UnaryServerInterceptor
}

func defaultServerOpts(mainPort int) *serverOpts {
Expand Down Expand Up @@ -61,8 +62,10 @@ func WithHTTPMiddlewares(mws ...mwhttp.Middleware) Option {

// WithGRPCUnaryMiddlewares sets up unary middlewares for gRPC server.
func WithGRPCUnaryMiddlewares(mws ...grpc.UnaryServerInterceptor) Option {
mw := grpc_middleware.ChainUnaryServer(mws...)
return func(o *serverOpts) {
o.GRPCOpts = append(o.GRPCOpts, grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(mws...)))
o.GRPCOpts = append(o.GRPCOpts, grpc.UnaryInterceptor(mw))
o.GRPCUnaryInterceptor = mw
}
}

Expand Down
5 changes: 5 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (s *Server) Run(svc transport.Service) error {
io.Copy(w, bytes.NewReader(desc.SwaggerDef()))
})

// apply gRPC interceptor
if d, ok := desc.(transport.ConfigurableServiceDesc); ok {
d.Apply(transport.WithUnaryInterceptor(s.opts.GRPCUnaryInterceptor))
}

// Register everything
desc.RegisterHTTP(s.srv.http)
desc.RegisterGRPC(s.srv.grpc)
Expand Down
4 changes: 2 additions & 2 deletions transport/httptransport/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type OptionUnaryInterceptor struct {
}

// Apply implements transport.DescOption.
func (o OptionUnaryInterceptor) Apply(oo DescOptions) {
func (o OptionUnaryInterceptor) Apply(oo *DescOptions) {
if oo.UnaryInterceptor != nil {
// Chaining can be done via mwitkow/grpc-middleware for example.
panic("UnaryInterceptor is already applied, can't apply twice")
Expand All @@ -31,6 +31,6 @@ type OptionSwaggerOpts struct {
}

// Apply implements transport.DescOption.
func (o OptionSwaggerOpts) Apply(oo DescOptions) {
func (o OptionSwaggerOpts) Apply(oo *DescOptions) {
oo.SwaggerDefaultOpts = append(oo.SwaggerDefaultOpts, o.Options...)
}
2 changes: 1 addition & 1 deletion transport/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// DescOption modifies the ServiceDesc's behaviour.
type DescOption interface {
Apply(httptransport.DescOptions)
Apply(*httptransport.DescOptions)
}

// WithUnaryInterceptor sets up the interceptor for incoming calls.
Expand Down

0 comments on commit 4b2f5cb

Please sign in to comment.