Skip to content

Commit

Permalink
Only add some methods to router handler
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Dec 4, 2020
1 parent d4e5226 commit 1f5385d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
6 changes: 5 additions & 1 deletion router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (e *HandlerError) Unwrap() error {
return e.err
}

func newHandler(h interface{}) *handler {
func newHandler(h interface{}, methods map[string]bool) *handler {
handtype := reflect.TypeOf(h)
handval := reflect.ValueOf(h)
name := reflect.Indirect(handval).Type().Name()
Expand All @@ -35,6 +35,10 @@ func newHandler(h interface{}) *handler {
for i := 0; i < handtype.NumMethod(); i++ {
m := handtype.Method(i)

if methods != nil && !methods[m.Name] {
continue
}

e := getEndpoint(m)
if e == nil {
continue
Expand Down
11 changes: 8 additions & 3 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var ErrMalformedPath = errors.New("malformed request path")
var ErrEndpointNotFound = errors.New("endpoint not found")

type Router interface {
AddHandler(h interface{})
AddHandler(h interface{}, methods []string)
Handle(path string, data []byte) ([]byte, error)
}

Expand All @@ -33,8 +33,13 @@ func NewRouter(cod codec.Codec, log logger.Logger) Router {
}
}

func (s *router) AddHandler(h interface{}) {
hdl := newHandler(h)
func (s *router) AddHandler(h interface{}, methods []string) {
metmap := make(map[string]bool, len(methods))
for _, m := range methods {
metmap[m] = true
}

hdl := newHandler(h, metmap)
s.handlers[hdl.Name] = hdl

for k := range hdl.Endpoints {
Expand Down
6 changes: 3 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type Server interface {
Start() error
AddHandler(h interface{})
AddHandler(h interface{}, methods ...string)
Publish(ctx context.Context, topic string, data interface{}) error
}

Expand Down Expand Up @@ -45,8 +45,8 @@ func (s *server) Start() error {
return nil
}

func (s *server) AddHandler(h interface{}) {
s.opts.Router.AddHandler(h)
func (s *server) AddHandler(h interface{}, methods ...string) {
s.opts.Router.AddHandler(h, methods)
}

func (s *server) handle(soc transport.Socket) {
Expand Down

0 comments on commit 1f5385d

Please sign in to comment.